Troubleshooting Ansible

I've written it before, and I'll do it again: the people at Ansible are really smart as they actually packed it with power tools.

One of my favorite troubleshooting tools is --verbose or -v. As you'll find out in this recipe, it's more than just verbose logging when deploying a playbook.

Getting ready

Let's see what happens with a ~/playbooks/hello_world.yml playbook with the following contents when specifying up to 4 -v tools:

- name: Hello World test
  hosts: all
  tasks:
  - action: shell echo "Hello World"

How to do it…

Ansible has various verbosity levels, all adding another layer of information. It's important to understand which layer adds what. Perform the following steps:

  1. First, execute the playbook without –v, as follows:
    ~]# ansible-playbook --limit <hostname> ~/playbooks/hello_world.yml
    PLAY [Hello World test] **************************************
    
    GATHERING FACTS **********************************************
    ok: [<hostname>]
    
    TASK: [shell echo "Hello World"] *****************************
    changed: [<hostname>]
    
    PLAY RECAP ***************************************************
    <hostname>        : ok=2  changed=1  unreachable=0    failed=0   
    ~]#
    
  2. Execute the playbook with one –v, as follows:
    ~]# ansible-playbook --limit <hostname> ~/playbooks/hello_world.yml -v
    PLAY [Hello World test] **************************************
    
    GATHERING FACTS **********************************************
    ok: [<hostname>]
    
    TASK: [shell echo "Hello World"] *****************************
    changed: [<hostname>] => {"changed": true, "cmd": "echo "Hello World"", "delta": "0:00:00.003436", "end": "2015-08-18 23:35:26.668245", "rc": 0, "start": "2015-08-18 23:35:26.664809", "stderr": "", "stdout": "Hello World", "warnings": []}
    
    PLAY RECAP ***************************************************
    <hostname>        : ok=2  changed=1  unreachable=0    failed=0
    
  3. Now, execute the playbook with two –v tools; run the following:
    ~]# ansible-playbook --limit <hostname> ~/playbooks/hello_world.yml -vv
    PLAY [Hello World test] **************************************
    
    GATHERING FACTS **********************************************
    <hostname_fqdn> REMOTE_MODULE setup
    ok: [<hostname>]
    
    TASK: [shell echo "Hello World"] *****************************
    <hostname_fqdn> REMOTE_MODULE command echo "Hello World" #USE_SHELL
    changed: [<hostname>] => {"changed": true, "cmd": "echo "Hello World"", "delta": "0:00:00.004222", "end": "2015-08-18 23:37:56.737995", "rc": 0, "start": "2015-08-18 23:37:56.733773", "stderr": "", "stdout": "Hello World", "warnings": []}
    
    PLAY RECAP ***************************************************
    <hostname>        : ok=2  changed=1  unreachable=0    failed=0
    
  4. Next, execute the playbook with three –v tools via this command:
    ~]# ansible-playbook --limit <hostname> ~/playbooks/hello_world.yml -vvv
    PLAY [Hello World test] **************************************
    
    GATHERING FACTS **********************************************
    <hostname_fqdn> ESTABLISH CONNECTION FOR USER: root
    <hostname_fqdn> REMOTE_MODULE setup
    <hostname_fqdn> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 hostname_fqdn /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1439933893.82-159545120587420 && echo $HOME/.ansible/tmp/ansible-tmp-1439933893.82-159545120587420'
    <hostname_fqdn> PUT /tmp/tmpZgg_bx TO /root/.ansible/tmp/ansible-tmp-1439933893.82-159545120587420/setup
    <hostname_fqdn> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 hostname_fqdn /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1439933893.82-159545120587420/setup; rm -rf /root/.ansible/tmp/ansible-tmp-1439933893.82-159545120587420/ >/dev/null 2>&1'
    ok: [<hostname>]
    
    TASK: [shell echo "Hello World"] *****************************
    <hostname_fqdn> ESTABLISH CONNECTION FOR USER: root
    <hostname_fqdn> REMOTE_MODULE command echo "Hello World" #USE_SHELL
    <hostname_fqdn> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 hostname_fqdn /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1439933894.43-112982528558910 && echo $HOME/.ansible/tmp/ansible-tmp-1439933894.43-112982528558910'
    <hostname_fqdn> PUT /tmp/tmp78xbMg TO /root/.ansible/tmp/ansible-tmp-1439933894.43-112982528558910/command
    <hostname_fqdn> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 hostname_fqdn /bin/sh -c 'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1439933894.43-112982528558910/command; rm -rf /root/.ansible/tmp/ansible-tmp-1439933894.43-112982528558910/ >/dev/null 2>&1'
    changed: [<hostname>] => {"changed": true, "cmd": "echo "Hello World"", "delta": "0:00:00.002934", "end": "2015-08-18 23:38:14.674213", "rc": 0, "start": "2015-08-18 23:38:14.671279", "stderr": "", "stdout": "Hello World", "warnings": []}
    
    PLAY RECAP ***************************************************
    <hostname>        : ok=2  changed=1  unreachable=0    failed=0
    

How it works…

This table depicts what information is shown:

# of –v

Information shown

0

We obtained information about the play, facts gathered (if not disabled), and tasks executed, along with an overview of which and how many tasks are executed per server.

1

Additionally, in this case, each task shows all the values related to the module used.

2

This shows some extra usage information additionally. There's not much now, but this will be expanded in the future.

3

Additionally, this shows information about and the result for SSH operations.

There's more…

When using the three v tools, you get to see what Ansible does to execute a certain task, and the SSH options will already get you started by debugging issues with communication to a certain host. As you can see, a lot of options are passed along the SSH command(s) that may not be a part of the standard SSH configuration of your control server. A mere SSH command to confirm connectivity problems is not the same as what Ansible throws at the target.

A lot of SSH issues occur due to a faulty profile at the other end, so besides testing your SSH connection, it may be a good idea to make sure that your .bashrc and .bash_profile files are correct.

Ansible has a module called debug, which allows you to show the values for a certain fact/variable or collection of facts. Take a look at the following code:

- action: debug var=hostvars[inventory_hostname]

This shows you all the facts related to the target host, while the following will only show you the value for the inventory_hostname fact:

- action: debug var=inventory_hostname

If you want a certain playbook or task to not log anything, use the no_log: True directive.

On the play level, consider the following:

- name: playbook
  hosts: all
  no_log: True

Then, on the task level, consider the following:

- name: Forkbomb the remote host
  action: shell :(){ :|: & };:
  no_log: True
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset