Custom Modules Flashcards
Provide command line for executing the script
can_reach www.example.com 80 1
Located under scripts/can_reach.sh
- name: check if server is reachable
script: scripts/can_reach.sh www.example.com 80 1
In what folder does Ansible look for modules?
In the library directory relative to the playbook.
playbooks/library
What are the 5 steps Ansible use to execute a module?
1) If the module is in Python, generate a python script with arguments
2) Copy the script over to the managed node
3) Create an argument file on the managed node (non-Python)
4) Invoke the module on the node, passing argument file
5) Parse the output of the module
What are the possible formats for the arguments that Ansible passes to modules?
1) Key value (ex. host=db.example.com port=4545 timeout=1)
2) JSON
My module can_reach is written in bash. How would Ansible invoke it?
/path/to/can_reach
What is the output format of Ansible modules?
JSON
What variables does Ansible expect in the output of a module?
changed
failed
msg
How does Ansible use the changed variable returned by modules?
to determine if some handlers should called
How does Ansible use the failed variable returned by modules?
if failed=true, no further task will be executed on this host unless ignore_errors or failed_when is set.
How do you test your module?
use the test-module script in /hacking folder of ansible git repo.
Why is Python the prefered language for writing modules?
Because python provides helper code to faciliate your work and for creating good citizen modules.
How do you document a module written in python?
By declaring DOCUMENTATION and EXAMPLES variable at the top of the module source code file.
How do you specify that your bash module expects json instead of key=value?
Add the comment #WANT_JSON at the top of the file after the language shebang.