Okay, we all know Ansible installation is coming with default modules and you don’t need to download or configure any additional modules for normal cases, Yes batteries already included.
But, what if you need to achieve something with Ansible but a suitable module is not available ? No worries, you can write your own modules for local use; that’s the beauty of Ansible.
Also See all Community Supported Ansible Modules.
Before you start, just make sure you have searched the module library and confirmed nothing suitable for your special use.
Learn more about developing a new Ansible Module.
You can add a local module in any of below locations as Ansible automatically loads all executable files found in those directories as modules.
You can search and confirm the same by checking Ansible version, you can see the paths in
ansible python module location line.
$ ansible --version ansible 2.5.1 config file = /home/devops/ansible.cfg configured module search path = [u'/home/devops/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/dist-packages/ansible executable location = /usr/bin/ansible python version = 2.7.17 (default, Nov 7 2019, 10:07:09) [GCC 7.4.0]
No need to worry, if you cannot see modules or no such path (
/usr/share/ansible/plugins/modules) , as we told earlier those are default directories configured to search for modules.
You can find the path of existing modules using
ansible-doc command as below.
$ ansible-doc -t module yum > YUM (/usr/lib/python2.7/dist-packages/ansible/modules/packaging/os/yum.py) Installs, upgrade, downgrades, removes, and lists packages and groups with the `yum' package manager. This module only works on Python 2. If you require Python 3 support see the [dnf] module. . . <output removed>
Now, I am downloading
diff module from github (just clone the repo or download only
diff.py from repo) and move
diff.py to my own module path –
$ git clone https://github.com/cytopia/ansible-module-diff $ ls -l ansible-module-diff/ total 52 -rwxrwxr-x 1 devops devops 11128 Feb 3 10:58 diff.py -rw-rw-r-- 1 devops devops 35059 Feb 3 10:58 LICENSE -rw-rw-r-- 1 devops devops 2391 Feb 3 10:58 README.md $ mkdir -p /home/devops/.ansible/plugins/modules $ cp ansible-module-diff/diff.py /home/devops/.ansible/plugins/modules/
Verify my module if its fetched by Ansible or not.
$ ansible-doc -t module diff > DIFF (/home/devops/.ansible/plugins/modules/diff.py) Diff compare a string, file or command output against a string file or command output. Check mode is only supported when diffing strings or files, commands will only be executed in actual run. More examples at https://github.com/cytopia/ansible-module-diff . . <removed>
Success ! You can see module info and documentation.
In some cases, you don’t want to share the custom module with everyone or wants to limit to some projects only.
library– in the same project directory. You can specify a module path in your
[defaults] ... library = ./library ...
library– within that role directory.