Writing Modules For Ansible
Writing Modules For Ansible
Magnus Glantz
Senior Solution Architect
Red Hat
About Me
● Worked with Puppet for 5 years but is now starting
to regret those years
GitHub/freenode: mglantz
http://github.com/mglantz/presentations
About Me
● Worked with Puppet for 5 years but is now starting
to regret those years
GitHub/freenode: mglantz
http://github.com/mglantz/presentations
What Are Modules?
What Are Modules? HOW DOES IT WORK?
Public / Private
Cloud CMDB
ANSIBLE AUTOMATION
Hosts
Networking
Modules Projects
Projects
What Are Modules? HOW DOES IT WORK?
Public / Private
Cloud CMDB Playbooks
● Written in YAML
● Tasks are executed in sequence
● Tasks call on Ansible modules
ANSIBLE AUTOMATION
Hosts
Networking
Modules Projects
Projects
What Are Modules? HOW DOES IT WORK?
Public / Private
Cloud CMDB
Networking
Modules Projects
Projects
What Are Modules?
Complexity:
● You must register your app with the base station.
● The API is REST based and expects JSON for updates.
● The API returns JSON, which is complex to parse and
validate in a playbook.
● Some actions require multiple API calls, which translate
into individual tasks in a playbook (even when looping).
● RGB colors are not used by the API - you must use
Hue/Saturation/Luminance or one of two other options.
How Modules Work
How Modules Work
● If not, a file containing the module args will be uploaded, and the path to that file
is the first argument to the module. If your module contains the string
WANT_JSON, that args file will be formatted as JSON (otherwise they’re
key=value pairs).
./library/my_new_test_module ./test.yml
PLAY ***************************************************************************
Pros:
● Easy to write, low learning curve.
● Protects users from complexity
Cons:
● Output/results have to be scraped out of the CLI output, which is
very fragile and prone to error
cli_command|awk ‘{ print $6 ‘}|cut -d’/’ -f2|sed ‘s/old/new/'
● Depending on use-case - only slightly more useful than using
command/shell modules.
How to Write Modules
Pros:
● Also very easy to get started with, since someone else has done
the hard work for you.
Cons:
● Extra dependencies for users running your module remotely (the
library must be installed everywhere you run the module).
● Modules may not cover API features you need (especially new
features).
● Bugs and abandonment (don’t forget to evaluate!)...
How to Write Modules
Pros:
● No extra dependencies (Ansible provides helper code in
module_utils/urls.py to make HTTP calls).
● New features are accessible immediately without having to
wait.
Cons:
● Having to know the API and maintain the module.
Module Testing and Debugging
Module Testing and Debugging
$ hacking/test-module --help
Useful Options:
-m MODULE_PATH, --module-path=MODULE_PATH
-a MODULE_ARGS, --args=MODULE_ARGS
-I INTERPRETER_TYPE=INTERPRETER_PATH,
--interpreter=INTERPRETER_TYPE=INTERPRETER_PATH
/home/user/.ansible/tmp/ansible-tmp-1455631745.37-113407429292636/hue explode
Best Practices
Best Practices
1. http://docs.ansible.com/ansible/developing_modules.html
2. http://docs.ansible.com/ansible/latest/dev_guide/developing_
modules_best_practices.html
Thank you
Thank you