I am managing a repository of Ansible playbooks.
Every playbook ( playbooks/tool_*/playbook_*.yml
) stands in a dedicated folder, which also hosts auxiliary files such as variables playbooks, Jinja2 templates, HTML templates and Ansible custom modules.
So it looks like this:
playbooks
├── tool_1
│ ├── library
│ │ ├── ansible_custom_module_1.py
│ │ └── ansible_custom_module_2.py
│ ├── templates
│ │ └── ...
│ ├── vars
│ │ └── ...
│ └── playbook_1.yml
├── tool_2
│ ├── library
│ │ ├── ansible_custom_module_1.py
│ │ ├── ansible_custom_module_2.py
│ │ └── ansible_custom_module_3.py
└── tool_3
└── ...
The ansible custom modules are called in the playbook in this way:
- name: do the custom task n1
ansible_custom_module_1:
input1: ...
input2: ...
input3: ...
Now, I would like to have all the ansible custom modules in only one folder playbooks/ansible_custom_modules/library/
,
so that I can remove all the folders playbooks/tool_*/library/
and thus avoid to have duplicates of the same file.
What do I have to do to be able to call the ansible custom modules in playbooks/ansible_custom_modules/library/
from the playooks playbooks/tool_*/playbook_*.yml
?
Is this even possible?
I have looked the matter up in Ansible documentation, but I have found this statement which seems to suggest that such a folder structure cannot let the playbooks to retrieve ansible custom modules.
To create a module:
Create a library directory in your workspace, your test play [i.e. the ansible trial custom module] should live in the same directory.
I created a symbolink link pointing from the playbook library foledr to the "centralized" library folder as following
ll playbooks/tool_1/library/
library -> playbooks/ansible_custom_modules/library
but it seems that the playbook ignores it:
ERROR! couldn't resolve module/action 'ansible_custom_module_1'. This often indicates a misspelling, missing collection, or incorrect module path.
The error appears to be in '/.../playbooks/tool_1/playbook_1.yml': line 319, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: do the custom task n1
^ here
The same is if the symbolik link is created on the custom modules files and not on the library
folder
ll playbooks/tool_1/library/
ansible_custom_module_1.py -> playbooks/ansible_custom_modules/library/ansible_custom_module_1.py
ansible_custom_module_2.py -> playbooks/ansible_custom_modules/library/ansible_custom_module_2.py
You might want to read the page adding standalone local modules for all playbooks and roles of the documentation.
To load standalone local modules automatically and make them available to all playbooks and roles, use the DEFAULT_MODULE_PATH configuration setting or the
ANSIBLE_LIBRARY
environment variable. The configuration setting and environment variable take a colon-separated list, similar to$PATH
.
So, one of the many solutions is to have a configuration in ~/.ansible.cfg
stating:
[defaults]
library = /path/to/that/folder/called/playbooks/ansible_custom_modules/library
There are tons of other variations of the same idea.
As long as you know:
where the configuration file could be:
ANSIBLE_CONFIG
(environment variable if set)ansible.cfg
(in the current directory)~/.ansible.cfg
(in the home directory)/etc/ansible/ansible.cfg
Source: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file
that the configuration could be set either by the environment variable ANSIBLE_LIBRARY
or via a configuration settings in the section [defaults]
under the key library
and how relative path are behaving in the configuration file
Then you can definitely come up with a combination that would fit your needs.