On Sun, 11 Apr 2021 22:58:20 -0400
Govind C <
gov...@gmail.com> wrote:
> group_vars.yml
> G1:
> - groupname: A
> G2:
> - groupname: B
> G3:
> - groupname: C
> G4:
> - groupname: D
>
> ... pass just the G1 and G2 as extra_vars to "groups_name" from the command line.
> - include_vars: group_vars.yml
> - name: Create Group(s)
> group:
> name: "{{ item.groupname }}"
> state: present
> with_items:
> - "{{groups_name}}"
Put the data into dictionaries, e.g.
shell> cat group_vars.yml
G1:
groupname: A
G2:
groupname: B
G3:
groupname: C
G4:
groupname: D
Include the variables into a dictionary and extract the keys, e.g.
- include_vars:
file: group_vars.yml
name: d1
- debug:
msg: "{{ item.groupname }}"
loop: "{{ groups_name|
default([])|
map('extract', d1)|
list }}"
The command
shell> ansible-playbook pb.yml -e '{"groups_name": ["G1","G2"]}'
should give
msg: A
msg: B
If you have to keep the data in the lists select the the first item
msg: "{{ item.0.groupname }}"
--
Vladimir Botka