0

I need to pass a dynamic group name info with_items so that i can access a specific fact that is ran from another host. I cannot hard code the group name

I tried to set a generic variable that is passed as 'GroupName' a few different ways. Including

with_items: "{{ groups['{{GROUPNAME}}'] }}"

   - name: Name of task
     debug:
       msg: "{{ hostvars[item]['ansible_check_mode'] }}"
     with_items: "{{ groups['GROUPNAME'] }}"

fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute '{{ GROUPNAME }}'"}

james
  • 157
  • 1
  • 2
  • 11

1 Answers1

1

Get the list of the hosts in the group and loop them

  vars:
    my_group: GROUPNAME
  tasks:
    - set_fact:
        my_hosts: "{{ groups|
                      dict2items|
                      selectattr('key', 'match', my_group)|
                      map(attribute='value')|
                      list|
                      flatten }}"
    - debug:
        msg: "{{ hostvars[item]['ansible_check_mode'] }}"
      loop: "{{ my_hosts }}"

(not tested)

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63