0

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?

1 Answer 1

0

include_role should do the trick.

--- - hosts: hosts 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'] tasks: - name: Collecting Data include_role: name: fetch_module_config vars: path: "{{ item.value.path }}" pattern: "{{ item.value.files_to_fetch }}" loop: "{{ modules | dict2items }}" when: item.key in group_names 

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.

1
  • Thanks, that solved my issue! :) Commented Nov 15, 2022 at 9:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.