Upload
aleonhardt
View
390
Download
0
Tags:
Embed Size (px)
Citation preview
Who am I
Alex LeonhardtAnsible user for ~2-3 months, previously Puppet and some SaltStackOps guy for a very long timeTwitter: @alex_leonhardt
Elsevier"Elsevier is an information solutions company with roots in publishing scientific, medical, and technical literature"
Publisher of scientific, medical and technical literatureScienceDirect.com, part of ElsevierAWS, Ansible, PackerPython / BASH + GoCD for operations/automationMicroservice architecutre (mostly Java)Know any/all of above? We're hiring!
Debugging Ansible modules
Ansible makes debugging somewhat problematic, expects valid json forany output.print('x has value: {0}').format(x) won't ever make it to the console.So how do we check the state of a variable, e.g. when looping over a list?
IDEs really help to find the correct
VIM vs IDE vs (other)
class(es) quickly, but use whatever you like
Use python loggingSetup logging in the main() function
CAUTION!This will also print boto debug output including your plain-text keys!
Use 'q'"Quick-and-dirty debugging output for tired programmers"
Install it with pip install q ( )Output is sent to $TMPDIR/qUse for normal output (replace 'print' with 'q')Use as a decorator (@q)
https://pypi.python.org/pypi/q
check out the temporary directory:
Mooaaarr debugging!
tell ansible to not clean-up after itself:$ export ANSIBLE_KEEP_REMOTE_FILES=1$ play -M modules/ -i inventory myplay_describe.yml -vvv
(in our case locally as we dont run against remote hosts)
$ cd ~/.ansible/tmp/ansible-tmp-1434809169.56-151563844546363$ ls -l-rw-r--r-- 1 ale staff 72655 20 Jun 15:06 ec2_describe
Mooaaarr debugging!ec2_describe
[...]state = module.params.get('state')
if state == 'describe': i_list, changed = get_instance_list(module, ec2)else: changed = False i_list = 'Failed'
module.exit_json(changed=changed, instances=i_list)
# import module snippets# This code is part of Ansible, but is an independent component.# This particular file snippet, and this file snippet only, is BSD licensed.[...]# == BEGIN DYNAMICALLY INSERTED CODE ==
ANSIBLE_VERSION = '1.9.1'
MODULE_ARGS = 'region=eu-west-1 'MODULE_COMPLEX_ARGS = '{"region": "eu-west-1"}'
Use pdb (python debugger)... to step through the code.
~/.ansible/tmp/ansible-tmp-1434809169.56-151563844546363 $ python -m pdb ec2_describe
Cleanup after yourselfLeaving debug logs around is dangerous, so don't forget!
rm -vf /tmp/ansible_debug.log $TMPDIR/q
Links / Resourceshttps://docs.python.org/2/library/logging.htmlhttps://pypi.python.org/pypi/qhttps://docs.python.org/2/library/pdb.htmlhttp://docs.ansible.com/ec2_module.htmlhttp://docs.ansible.com/developing_modules.html#testing-modules