Using Ansible How Do You Search The Contents of a File on Linux for “{{” or “}}”?

Problem scenario
You want to see if an Ansible playbook is injecting (or leaving) two braces, “{{” or “}}”, in a specific file on Linux.  These symbols can be injected accidentally with little notification.  Variable substitution could fail during the course of a playbook run.  This is a subtle problem that may have no alert.  You want some debugging to check for these symbols.  How do you know a file on a managed node does not have a “{{” or “}}”?

How Do You Troubleshoot the Ansible Problem “ERROR! Syntax Error while loading YAML. mapping values are not allowed here”?

Problem scenario
You are trying to run an Ansible playbook with the “become: yes” stanza.  You get this error after your ansible-playbook command:

“ERROR! Syntax Error while loading YAML.
  mapping values are not allowed here

The error appears to have been in ‘/home/cooluser/good.yaml’: line 5, column 10, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be…”

What should you do?

How Do You Troubleshoot the Ansible Message “ERROR! the role was not found”?

Problem scenario
You run ansible-playbook on a playbook (e.g., .yaml file) that uses a role.  It fails with the message “ERROR! the role was not found”.  How do you correct this problem?

Solution
Find the ansible.cfg file.  Look at the roles_path stanza.  Add or modify the path that you want Ansible to look at for a role every time the Ansible playbook runs.

How Do You Write an Ansible Playbook to Transfer a Compressed File and Unzip It without Using Linux Utilities?

Problem scenario
You have installed Ansible and configured it to work with other servers (managed nodes).  You want to write an Ansible playbook that will work with Linux managed nodes with three requirements:

1.  Transfer a compressed file from a web sever without wget.
2.  Uncompress a .zip file without unzip.
3.  Work without passwordless SSH authentication to the managed nodes.

How do you accomplish this with using Ansible-supported features?

What Command Do You Run to See the Host Variables That Ansible Natively Has Access to on the Servers In /etc/ansible/hosts File?

Problem scenario
You want to see the different variables that Ansible can leverage (e.g., for playbooks to inject host-specific data into files).  How do you find out what Ansible has access to on each server in /etc/ansible/hosts?

Solution
Run this command:
ansible all -m setup –tree /tmp/facts

How Do You Troubleshoot an Ansible Problem about Missing Quotes when You Have Quotes around Each Variable?

Problem scenario
You run an Ansible playbook but you get an error about “this one looks like it might be an issue with missing quotes.”  Your Ansible playbook compares two variables with each other.  

After running ansible-playbook on the .yaml file, you get this error:

“ERROR! Syntax Error while loading YAML.
  expected <block end>, but found ‘<scalar>’

The error appears to have been in ‘/home/cooluser/good.yaml’: line 12,

How Do You Troubleshoot the Ansible Error “Unsupported parameters for (copy) module: become Supported parameters”?

Problem scenario
You are trying to run an Ansible playbook with the “become: yes” stanza.  You get this error after your ansible-playbook command:

fatal: [instance-11]: FAILED! => {“changed”: false, “checksum”: “da39a3ee5e6b4b0d3255bfef95601890afd80709”, “msg”: “Unsupported parameters for (copy) module: become Supported parameters include: _original_basename, attributes, backup, checksum, content, delimiter, dest, directory_mode, follow, force, group, local_follow, mode, owner, regexp, remote_src, selevel, serole, setype, seuser, src, unsafe_writes, validate”}

Solution
Unindent the “become: yes” stanza so it is at the same level of its corresponding module.

How Do You Iterate through a List of Servers When Running an Ansible Playbook?

Problem scenario
You want an Ansible playbook to run on specific managed nodes.  You can create a list of the servers you want the playbook to apply to.  How do you provide a list of specific hostnames and have the Ansible playbook run on each server?

Prerequisites
This assumes that you have installed Ansible and have it working with the managed nodes you want to run a playbook on. 

How Do You Get Variables That Are Assigned a Value in an Ansible Playbook (i.e., a .yaml file) to Be Assigned from a Different File?

Problem scenario
You have a complex Ansible playbook (a .yaml file) that you want to be more modular with discrete component files.  You want to assign variables in a different file.  This will make your Ansible playbook have fewer lines.  Your playbook must  read in the variables from a separate file and inject them into the playbook when it is run.  You appreciate Unix philosophy of having things be modular.

What Command Would Test If sudo Was Working without Making Any Changes?

Problem scenario
To see if Ansible can run commands with sudo without being prompted, you want to run a test.

You want to see if sudo works for certain on your Linux server.  But you do not want to destroy anything or change any files.  What should you do for a test that would tell you if sudo is working or not?

Solution
Run a command such as this:  sudo ls /root

The above would not ordinarily work for a regular Linux user.