On Mon, 30 Dec 2019 00:19:47 -0800 (PST)
Yehuda Pinhas <[email protected]> wrote:

> I need to build inventory file that contains 6 groups total and each group
> will have around 100 hosts in it.. this is what I currently have.
>
> all:
>   hosts:
>     POC-ENV:
>       hosts:
>         ansible_host: /home/ansible/MACCABI/hosts/POC-ENV_list.yml
>     Nexus:
>       hosts:
>         ansible_host: /home/ansible/MACCABI/hosts/nexus_list.yml
>     Switches:
>       hosts:
>         ansible_host: /home/ansible/MACCABI/hosts/switch_list.yml
>     Avaya:
>       hosts:
>         ansible_host: /home/ansible/MACCABI/avaya_list.yml
>     Branch-Switches:
>       hosts:
>         ansible_host: /home/ansible/MACCABI/branch-switch_list.yml
>     Branch-Routers:
>       hosts:
>         ansible_host: /home/ansible/MACCABI/branch-router_list.yml

It's possible to create the groups with "add_host"
https://docs.ansible.com/ansible/latest/modules/add_host_module.html

For example, create the dictionary "my_hosts" with all groups and hosts, and
"loop subelements"
https://docs.ansible.com/ansible/latest/plugins/lookup/subelements.html#subelements-traverse-nested-key-from-a-list-of-dictionaries

  - hosts: localhost
    vars:
      my_hosts:
        - group: 'POC_ENV'
          hosts: "{{ lookup('file',
                     
'/home/ansible/MACCABI/hosts/POC-ENV_list.yml').splitlines() }}"
        - group: 'Nexus'
          hosts: "{{ lookup('file',
                     '/home/ansible/MACCABI/hosts/nexus_list.yml').splitlines() 
}}"
          ...
    tasks:
      - add_host:
          name: "{{ item.1 }}"
          groups: "{{ item.0.group }}"
        loop: "{{ lookup('subelements', my_hosts, 'hosts') }}"

  (not tested)

Then use the groups in the next plays.


Notes:

* See "How to build your inventory"
  
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#how-to-build-your-inventory
* 'POC-ENV', 'Branch-Switches' and 'Branch-Routers' are not valid names of
  the groups. See "Creating valid variable names"
  
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names
* See "Developing dynamic inventory"
  
https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-dynamic-inventory

Cheers,

        -vlado

-- 
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/20191230100531.368ce530%40gmail.com.

Attachment: pgpWw9tBxN3B2.pgp
Description: OpenPGP digital signature

Reply via email to