Score:0

Ansible: Loop over the items of a host list and execute role

tw flag

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?

Score:0
in flag

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.

coquinho avatar
tw flag
Thanks, that solved my issue! :)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.