The first custom module

Writing a custom module does not need to be complicated; in fact, it doesn't even need to be in Python. But since we are already familiar with Python, we will use Python for our custom modules. We are assuming that the module is what we will be using ourselves and our team without submitting back to Ansible, therefore we will ignore some of the documentation and formatting for the time being. 

If you are interested in developing modules that can be submitted upstream to Ansible, please consult the developing modules guide from Ansible (https://docs.ansible.com/ansible/latest/dev_guide/developing_modules.html). 

By default, if we create a folder named library in the same directory as the playbook, Ansible will include the directory in the module search path. Therefore, we can put our custom module in the directory and we will be able to use it in our playbook. The requirement for the custom module is very simple: all the module needs is to return a JSON output to the playbook.

Recall that in Chapter 6, APIs and Intent-Driven Networking, we used the following NXAPI Python script to communicate to the NX-OS device:

    import requests
import json

url='http://172.16.1.142/ins'
switchuser='cisco'
switchpassword='cisco'

myheaders={'content-type':'application/json-rpc'}
payload=[
{
"jsonrpc": "2.0",
"method": "cli",
"params": {
"cmd": "show version",
"version": 1.2
},
"id": 1
}
]
response = requests.post(url,data=json.dumps(payload),
headers=myheaders,auth=(switchuser,switchpassword)).json()

print(response['result']['body']['sys_ver_str'])

When we executed it, we simply received the system version. We can simply modify the last line to be a JSON output, as shown in the following code:

    version = response['result']['body']['sys_ver_str']
print json.dumps({"version": version})

We will place this file under the library folder: 

$ ls -a library/
. .. custom_module_1.py

In our playbook, we can then use the action plugin (https://docs.ansible.com/ansible/dev_guide/developing_plugins.html), chapter8_14.yml, to call this custom module:

    ---
- name: Your First Custom Module
hosts: localhost
gather_facts: false
connection: local

tasks:
- name: Show Version
action: custom_module_1
register: output

- debug:
var: output

Note that, just like the ssh connection, we are executing the module locally with the module making API calls outbound. When you execute this playbook, you will get the following output:

$ ansible-playbook chapter8_14.yml
[WARNING]: provided hosts list is empty, only localhost is available

PLAY [Your First Custom Module] ************************************************

TASK [Show Version] ************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
"output": {
"changed": false,
"version": "7.3(0)D1(1)"
}
}

PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0

As you can see, you can write any module that is supported by API, and Ansible will happily take any returned JSON output.

..................Content has been hidden....................

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