Debugging ansible modules

35
Debugging custom Ansible modules

Transcript of Debugging ansible modules

Debugging custom Ansible modules

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?

Structure

Playbook

Be verbose, be very verbose-vvv

Using AWS (who isn't) ?~/.boto

Boto has a debug setting, use it!

Boto debug

HTTP POST output

Caution! Credentials are shown in clear text

Custom moduleec2_describe.py

Custom moduleec2_describe.py

Custom modulelist ec2 instance ids

but 'print' won't work >:(

Custom module (run)

Custom module (run)

Some output

not great, but something

at least we now know the correct class

Some more output

to check for attributes, etc.

IDEs really help to find the correct

VIM vs IDE vs (other)

class(es) quickly, but use whatever you like

Fixed code

and the correct method used

... this time ;)

Final play

Final play (output)

But wait...... what about print("hello world: {0}").format(x) ?

Use python logging

" p r i n t "

Use python loggingSetup logging in the main() function

CAUTION!This will also print boto debug output including your plain-text keys!

Finally.. we get some output!

Bonus points - more boto debug!

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

A q(uick) exampleOutput

Decorator

A q(uick) example (output)Output

A q(uick) example (output)Decorator

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

Thank you!