Top Banner
Debugging custom Ansible modules
35
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Debugging ansible modules

Debugging custom Ansible modules

Page 2: Debugging 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

Page 3: Debugging ansible modules

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!

Page 4: Debugging ansible modules

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?

Page 5: Debugging ansible modules

Structure

Page 6: Debugging ansible modules

Playbook

Page 7: Debugging ansible modules

Be verbose, be very verbose-vvv

Page 8: Debugging ansible modules

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

Boto has a debug setting, use it!

Page 9: Debugging ansible modules

Boto debug

HTTP POST output

Caution! Credentials are shown in clear text

Page 10: Debugging ansible modules

Custom moduleec2_describe.py

Page 11: Debugging ansible modules

Custom moduleec2_describe.py

Page 12: Debugging ansible modules

Custom modulelist ec2 instance ids

but 'print' won't work >:(

Page 13: Debugging ansible modules

Custom module (run)

Page 14: Debugging ansible modules

Custom module (run)

Page 15: Debugging ansible modules

Some output

not great, but something

Page 16: Debugging ansible modules

at least we now know the correct class

Some more output

to check for attributes, etc.

Page 17: Debugging ansible modules

IDEs really help to find the correct

VIM vs IDE vs (other)

class(es) quickly, but use whatever you like

Page 18: Debugging ansible modules

Fixed code

and the correct method used

... this time ;)

Page 19: Debugging ansible modules

Final play

Page 20: Debugging ansible modules

Final play (output)

Page 21: Debugging ansible modules

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

Page 22: Debugging ansible modules

Use python logging

" p r i n t "

Page 23: Debugging ansible modules

Use python loggingSetup logging in the main() function

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

Page 24: Debugging ansible modules

Finally.. we get some output!

Page 25: Debugging ansible modules

Bonus points - more boto debug!

Page 26: Debugging ansible modules

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

Page 27: Debugging ansible modules

A q(uick) exampleOutput

Decorator

Page 28: Debugging ansible modules

A q(uick) example (output)Output

Page 29: Debugging ansible modules

A q(uick) example (output)Decorator

Page 30: Debugging ansible modules

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

Page 31: Debugging ansible modules

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"}'

Page 32: Debugging ansible modules

Use pdb (python debugger)... to step through the code.

~/.ansible/tmp/ansible-tmp-1434809169.56-151563844546363 $ python -m pdb ec2_describe

Page 33: Debugging ansible modules

Cleanup after yourselfLeaving debug logs around is dangerous, so don't forget!

rm -vf /tmp/ansible_debug.log $TMPDIR/q

Page 34: Debugging ansible modules

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

Page 35: Debugging ansible modules

Thank you!