I have an application composed of micro modules distributed on multiple hosts.
inventory:
[moduleA_hostgroup]
host1
host2
[moduleB_hostgroup]
host2
host3
[moduleC_hostgroup]
host1
host4
Each module has specific location and configuration files. I'd like to fetch the config for all installed modules
vars:
modules:
moduleA:
path: "/opt/moduleA"
files_to_fetch: ['*.conf']
moduleB:
path: "/etc/moduleB"
files_to_fetch: ['*.json','*.properties']
moduleC:
path: "/etc/moduleC"
files_to_fetch: ['*.conf','*.json']
I can retrieve those files for the modules by running a role:
- hosts: moduleA_hostgroup
pre_tasks:
- name: pre_tasks
set_fact:
path: "{{ modules.moduleA.path }}"
pattern: "{{ modules.moduleA.files_tofetch }}'
roles:
- ../roles/fetch_module_config
- hosts: moduleB_hostgroup
pre_tasks:
- name: pre_tasks
set_fact:
path: "{{ modules.moduleB.path }}"
pattern: "{{ modules.moduleB.files_tofetch }}'
roles:
- ../roles/fetch_module_config
etc..
Issue is, I'll have to run the roles as many times as there are modules
Is there an elegant way to loop through the module list and run the role on the module hostgroup only?
Best Answer
include_role
should do the trick.But that is rather clunky. IMO a role is not right for such a task, you should consider converting it into a task and then use
include_task
instead.