On Sun, 11 Apr 2021 22:58:20 -0400
Govind C <[email protected]> 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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20210412055221.6183ac70%40gmail.com.

Attachment: pgpvndjndBqhL.pgp
Description: OpenPGP digital signature

Reply via email to