8. ansible basic troubleshooting tools

Preview

In the Ansible for Cisco Network Engineers course we have seen how to use ansible ahdoc commands and also ansible playbook. Before we move on to more advanced topics in Ansible Playbook, there are a few simple troubleshooting tools that we can use to find and fix Playbook issues. In this section we look at these simple troubleshooting tools (ansible basic troubleshhooting tools).

Ansible basic troubleshooting tools

access ansible codes of this course in github

this is a video-based training therefore the output of running commands are not show in the text.

The first command is to check the YAML syntax to make sure that the YAML syntax does not contain any errors, specifically the number of spaces and the use of the tab instead of the space. It’s a good idea to check the syntax before running a playbook.

# ansible-playbook backup1.yml  --syntax-check

Add a extra space in YAML file and run –syntax-check to see the result of the command.

The second ansible troubleshooting tool I would like to introduce is to run ansible in check mode.

In check mode, Ansible runs playbook without making any changes on managed nodes.

Check mode is just a simulation. it is great for validating configuration of the playbook. To run a playbook in check mode run the following command.

# ansible-playbook backup1.yaml  --check

It can be a good idea to always run the playbook in check mode before final running it to make sure the configuration is correct and also to make sure what changes are being made in the managed node. To see what changes are being made in the managed node, you can use the register and debug command, which is our next troubleshooting tool.

You can use the register command to save the output of a command and store it in a variable. then you can use the debug command to print the content of the variable. This can be seen in a register_debug.yaml playbook that I have already prepared. In this playbook I run backup playbook once again but I register the result of backup command and store it in result variable and print it in the next task with debug command. So the exact name of the configuration file is shown in output.

root@debian:~/ansible-project1# cat register_debug.yaml
---
- hosts: csr
  gather_facts: false
  connection: network_cli
  vars:
    cli:
      host: "{{ inventory_hostname }}"
      username: rayka
      password: rayka-co.com

  tasks:
    - name: backup config
      ios_config:
        backup: yes
        provider: "{{ cli }}"
      register: result
    - name: print result
      debug:
        msg: "{{ result }}"

The next  ansible troubleshooting tool is to run ansible playbook with an increased verbosity level  with -v (or –verbose mode). Depending  on the number of v from one to four, causes more data to be printed to the Ansible log. in verbose mode all changes will be show in json format. per case you should read deug messages exactly to find exact error.

# ansible-playbook backup1.yaml  -v
# ansible-playbook backup1.yaml  -vvv
# ansible-playbook backup1.yaml  -vvvv

Another troubleshooting point that you need to know is the default behavior of ansible when a task fails. while running a playbook, if one task fails, the playbook will stop running and exit and the other task will not run. However, you can change this situation by setting the “ignore_errors” flag to true. I have already prepared a playbook called ignore_error.yaml. In this task, I purposely created an error in my command, but I ignored the error with the “ignore_erros” flag so that the playbook would not exit and the other task would continue. To make sure that the next task is executed, I created another task that prints a message, but when the result of the previous task fails. The result of previous tasks is registered in the result variable using a register command. To print a message when the result fails, I used a condition with the when command. We will have a special video to create conditions in Ansible.

root@debian:~/ansible-project1# cat ignore_error.yaml
---
- hosts: csr
  gather_facts: false
  connection: local

  tasks:
    - name: test ignore_errors flag
      ios_config:
        lines:
          - descriptiok test
          - shutdown
          - ip address 172.31.1.1 255.255.255.0
        parents: interface GigabitEthernet2
      ignore_errors: yes
      register: result

    - name: print the result of previous task
      debug:
        msg: previous task run not successfuly
      when: result is failed

anibsle ignore_error description in Ansible website

Back to: Ansible for Network Engineers > Ansible Troubleshooting

Leave a comment

Your email address will not be published. Required fields are marked *