”;
The most common strategies for debugging Ansible playbooks are using the modules given below −
Debug and Register
These two are the modules available in Ansible. For debugging purpose, we need to use the two modules judiciously. Examples are demonstrated below.
Use Verbosity
With the Ansible command, one can provide the verbosity level. You can run the commands with verbosity level one (-v) or two (-vv).
Important Points
In this section, we will go through a few examples to understand a few concepts.
If you are not quoting an argument that starts with a variable. For example,
vars: age_path: {{vivek.name}}/demo/ {{vivek.name}}
This will throw an error.
Solution
vars: age_path: "{{vivek.name}}/demo/" – marked in yellow is the fix. How to use register -> Copy this code into a yml file say test.yml and run it --- #Tsting - hosts: tomcat-node tasks: - shell: /usr/bin/uptime register: myvar - name: Just debugging usage debug: var = myvar
When I run this code via the command Ansible-playbook -i hosts test.yml, I get the output as shown below.
If you see the yaml , we have registered the output of a command into a variable – myvar and just printed the output.
The text marked yellow, tells us about property of the variable –myvar that can be used for further flow control. This way we can find out about the properties that are exposed of a particular variable. The following debug command helps in this.
$ ansible-playbook -i hosts test.yml PLAY [tomcat-node] *************************************************************** **************** **************************************************************** *************** ****************************** TASK [Gathering Facts] ***************************************************************** ************** ***************************************************************** ************** ************************** Monday 05 February 2018 17:33:14 +0530 (0:00:00.051) 0:00:00.051 ******* ok: [server1] TASK [command] ****************************************************************** ************* ****************************************************************** ************* ********************************** Monday 05 February 2018 17:33:16 +0530 (0:00:01.697) 0:00:01.748 ******* changed: [server1] TASK [Just debugging usage] ****************************************************************** ************* ****************************************************************** ************* ********************* Monday 05 February 2018 17:33:16 +0530 (0:00:00.226) 0:00:01.974 ******* ok: [server1] => { "myvar": { "changed": true, "cmd": "/usr/bin/uptime", "delta": "0:00:00.011306", "end": "2018-02-05 17:33:16.424647", "rc": 0, "start": "2018-02-05 17:33:16.413341", "stderr": "", "stderr_lines": [], "stdout": " 17:33:16 up 7 days, 35 min, 1 user, load average: 0.18, 0.15, 0.14", "stdout_lines": [ " 17:33:16 up 7 days, 35 min, 1 user, load average: 0.18, 0.15, 0.14" ] } } PLAY RECAP **************************************************************************** ********************************************************************************** ************************************** server1 : ok = 3 changed = 1 unreachable = 0 failed = 0
Common Playbook Issues
In this section, we will learn about the a few common playbook issues. The issues are −
- Quoting
- Indentation
Playbook is written in yaml format and the above two are the most common issues in yaml/playbook.
Yaml does not support tab based indentation and supports space based indentation, so one needs to be careful about the same.
Note − once you are done with writing the yaml , open this site(https://editor.swagger.io/) and copy paste your yaml on the left hand side to ensure that the yaml compiles properly. This is just a tip.
Swagger qualifies errors in warning as well as error.
”;