Implementing Ansible modules with Python

Ansible comes packed with a lot of built-in modules (for almost all tasks), but for some custom tasks, you can write custom modules with Python.

For example, we can use the common Ansible Boilerplate module as we can see in the documentation: http://docs.ansible.com/ansible/dev_guide/developing_modules_general.html or https://docs.ansible.com/ansible/2.3/dev_guide/developing_modules_general.html.

We can develop our own module to automate input from a playbook. Ansible also provides a Python library to parse user arguments and handle errors and returns. First, we will import the AnsibleModule class from the ansible.module_utils.basic package:

from ansible.module_utils.basic import AnsibleModule
if __name__ == '__main__':
main()

The AnsibleModule class provides lots of common code for handling returns and parsing arguments. In the following example, we will parse three arguments for the host, username, and password, and make them required fields:

def main():
data = {"host": {"default": “localhost”, "type": "str"},
"username": {"default": “username”, "type": "str"},
"password": {"default": “password”, "type": "str"},
"url": {"default": “url”, "type": "str"}
}

module = AnsibleModule(argument_spec = data)

All variables need to be declared with dictionary format and the fields are passed in as argument_spec to AnsibleModule. You can then access the value of the arguments through the module.params dictionary by calling the get method on module.params:

host = module.params.get('host')
username = module.params.get('username')
password = module.params.get('password')
url='http://' + host + '/authentication'
module.params.update({"url": url})

Finally, we return the module.params value with all data values, using the exit_json method. Ansible uses this method to handle success providing a response in JSON format with the processing data:

module.exit_json(changed=True, meta=module.params)

Our user_authenticate.yml playbook will pass four variables to the user_authenticate module (host, username, password, and url) in the user_authenticate.py file:

- name: My Custom Module
hosts: localhost
tasks:
- name: authenticating user service
user_authenticate:
host: "localhost"
username: "username"
password: "password"
url :"url"
register: result
- debug: var=result

Ansible allows us to register the values returned by a task in a variable. That way, we can work with them from another task. Depending on the Ansible module used, the variable will keep different values. The keyword used in this case is register.

This is the output obtained when you execute $ ansible-playbook user_authenticate.yml:

PLAY [My Custom Module]
************************************************

TASK [authenticating user service]
************************************************************
ok: [localhost]


TASK [debug]
*******************************************************************
ok: [localhost] => {
"output": {
"changed": false,
"host": "localhost",
"username":"username",
"password": "password",
"url": "http://localhost/authentication'",
}
}

PLAY RECAP
*********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset