I have the following inventory
group1:
host001:
host002:
group2:
host101:
host102:
Now I want to loop over either of the groups, controlled via a parameter e.g.
- hosts: localhost
vars:
target: group1
- name: Do something for an inventory group
include_tasks: sample-play.yml
loop: "{{ groups['{{ target }}'] }}"
Is that even possible? if yes how? I tried
loop: "{{ groups['{{ target }}'] }}"
and
loop: "{{ groups.{{ target }} }}"
Both ending with the following error:
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected name or number. String: {{ groups.{{ target }} }}"}
What I want to achieve is to modify a local file and add an entry for each host in a group. Concretely I want to add a proxy entry for each host of a group to the ssh config-file.