Debugging the task

Let's run the playbook and see what happens:

$ ansible-playbook playbook.yml

The first problem is that we are not passing a host inventory file, so there will be warnings that only the localhost is available; this is fine, as we want to run the say module only on our Ansible Controller anyway:

[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'

Next, Ansible runs the play itself; this should result in a fatal error:

PLAY [localhost] ***********************************************************************************

TASK [Say a message on your Ansible host] **********************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'massage' is undefined The error appears to have been in '/Users/russ/Documents/Code/learn-ansible-fundamentals-of-ansible-2x/chapter17/say/playbook.yml': line 12, column 7, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: tasks: - name: Say a message on your Ansible host ^ here "}

Typically, the playbook run will stop, and you will be returned to your shell; however, because we have instructed Ansible to drop into the interactive debugger, we now see the following prompt:

[localhost] TASK: Say a message on your Ansible host (debug)>

From here, we can start to look into the problem a little more; for example, we can review the error by typing the following command:

p result._result

Once you hit the Enter key, the results of the failed task will be returned:

[localhost] TASK: Say a message on your Ansible host (debug)> p result._result
{'failed': True,
'msg': u"The task includes an option with an undefined variable. The error was: 'massage' is undefined The error appears to have been in '/Users/russ/Documents/Code/learn-ansible-fundamentals-of-ansible-2x/chapter17/say/playbook.yml': line 12, column 7, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: tasks: - name: Say a message on your Ansible host ^ here "}
[localhost] TASK: Say a message on your Ansible host (debug)>

Let's take a closer look at the variables used in the task by typing the following:

p task.args

This will return the two arguments we are using in the task:

[localhost] TASK: Say a message on your Ansible host (debug)> p task.args
{u'msg': u'{{ massage }}', u'voice': u'{{ voice }}'}
[localhost] TASK: Say a message on your Ansible host (debug)>

Now, let's take a look at the variables that are available to the task using the following:

p task_vars

You may have noted that we instructed Ansible to execute the setup module as part of the playbook run; that is to keep this list of available variables to a minimum:

[localhost] TASK: Say a message on your Ansible host (debug)> p task_vars
{'ansible_check_mode': False,
'ansible_connection': 'local',
'ansible_current_hosts': [u'localhost'],
'ansible_diff_mode': False,
'ansible_facts': {},
'ansible_failed_hosts': [],
'ansible_forks': 5,
'ansible_inventory_sources': [u'/etc/ansible/hosts'],
'ansible_play_batch': [],
'ansible_play_hosts': [u'localhost'],
'ansible_play_hosts_all': [u'localhost'],
'ansible_playbook_python': '/usr/bin/python',
'ansible_python_interpreter': '/usr/bin/python',
'ansible_run_tags': [u'all'],
'ansible_skip_tags': [],
'ansible_version': {'full': '2.5.5',
'major': 2,
'minor': 5,
'revision': 5,
'string': '2.5.5'},
'environment': [],
'group_names': [],
'groups': {'all': [], 'ungrouped': []},
'hostvars': {},
'inventory_hostname': u'localhost',
'inventory_hostname_short': u'localhost',
u'message': u'The task has completed and all is well',
'omit': '__omit_place_holder__0529a2749315462e1ae1a0d261987dedea3bfdad',
'play_hosts': [],
'playbook_dir': u'/Users/russ/Documents/Code/learn-ansible-fundamentals-of-ansible-2x/chapter17/say',
u'voice': u'Daniel'}
[localhost] TASK: Say a message on your Ansible host (debug)>

As you can see, there is a lot of information there about the environment our playbook is being executed on. In the list of variables, you will notice that two of them are prefixed with a u: they are voice and message. We can find out more about these by running:

p task_vars['message']
p task_vars['voice']

This will display the contents of the variable:

[localhost] TASK: Say a message on your Ansible host (debug)> p task_vars['message']
u'The task has completed and all is well'
[localhost] TASK: Say a message on your Ansible host (debug)> p task_vars['voice']
u'Daniel'
[localhost] TASK: Say a message on your Ansible host (debug)>

We know that we are passing a misspelt variable to the msg argument, so we make some changes on the fly and continue the playbook run. To do this, we are going to run the following command:

task.args['msg'] = '{{ message }}'

This will update the argument to use the correct variable meaning, so that we can rerun the task by running the following:

redo

This will immediately rerun the task with the correct argument and, with any luck, you should hear The task has completed, and all is well:

[localhost] TASK: Say a message on your Ansible host (debug)> task.args['msg'] = '{{ message }}'
[localhost] TASK: Say a message on your Ansible host (debug)> redo
changed: [localhost]

PLAY RECAP ************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0

As you can see from the preceding output, because we only have a single task, the playbook completed. If we had more, then it would carry on from where it left off. You can now update your playbook with the correct spelling and proceed with the rest of your day.

Also, if we wanted to, we could have typed either continue or quit to proceed or stop respectively.

..................Content has been hidden....................

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