# Need help on building ansible inventory file properly

**URL:** <https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881>\
**Category:** Ansible Project\
**Created:** [December 30, 2019, 8:19am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881 "2019-12-30T08:19:47Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 30, 2019, 8:19am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/1 "2019-12-30T08:19:47Z")

</div>

Hi,  
I have been searching all over the internet and there isn’t any good documentation that explains how to build my inventory properly. The official documentation is bad and there aren’t any example on inet that I could find.

Can you please help me? 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

As you can see there are 6 groups: POC-ENV, Nexus, Switches, Avaya, Branch-Switches, Branch-Routers and I am trying to list all the hosts in a file and direct the inventory to read it from there since I have so many.

What am I doing wrong? can you fix my code?  
Thanks for the helpers.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 9:05am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/2 "2019-12-30T09:05:31Z")

</div>

It's possible to create the groups with "add\_host"  
[https://docs.ansible.com/ansible/latest/modules/add\_host\_module.html](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](https://docs.ansible.com/ansible/latest/plugins/lookup/subelements.html#subelements-traverse-nested-key-from-a-list-of-dictionaries)

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group: 'POC\_ENV'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts: "{{ lookup('file',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'/home/ansible/MACCABI/hosts/POC-ENV\_list.yml').splitlines() }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group: 'Nexus'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts: "{{ lookup('file',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'/home/ansible/MACCABI/hosts/nexus\_list.yml').splitlines() }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ item.0.group }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('subelements', my\_hosts, 'hosts') }}"

&nbsp;&nbsp;(not tested)

Then use the groups in the next plays.

Notes:

\* See "How to build your inventory"  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#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  
&nbsp;&nbsp;the groups. See "Creating valid variable names"  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#creating-valid-variable-names](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names)  
\* See "Developing dynamic inventory"  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_inventory.html#developing-dynamic-inventory](https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-dynamic-inventory)

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 9:10am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/3 "2019-12-30T09:10:17Z")

</div>

Errata:

&nbsp;&nbsp;For example, create the list of dictionaries "my\_hosts"

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 30, 2019, 9:31am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/4 "2019-12-30T09:31:54Z")

</div>

> Hi again,

Thanks a lot for your reply.

I still don’t understand what these code lines used for in my case:

tasks:

- add\_host:  
name: “{{ item.1 }}”  
groups: “{{ item.0.group }}”  
loop: “{{ lookup(‘subelements’, my\_hosts, ‘hosts’) }}”

can you please explain?

I understand that you have wrote for me a lookup on the files mentioned for each lines which is exactly what I wanted. Now my intensions will be to copy and paste hosts of each environment to the appropriate host file.

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 30, 2019, 10:07am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/5 "2019-12-30T10:07:24Z")

</div>

> Hi,

Can you please take a look on my errors and try to help me figuring out why I am getting them?

This is my inventory code:

- hosts: localhost  
vars:  
my\_hosts:
- group: ‘POC\_ENV’  
hosts: “{{ lookup(‘file’,  
‘/home/ansible/MACCABI/hosts/POC\_ENV.yml’).splitlines() }}”

vars:  
ansible\_ssh\_private\_key\_file: /home/ansible/.ssh/id\_rsa  
ansible\_ssh\_common\_args: -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null  
ansible\_user: ansible

This is my playbook code (task is to add a VLAN):

name: Playing VLAN Configuration  
hosts: POC-ENV  
connection: local  
vars:  
vlan\_id: 999  
vlan\_name: TEST\_VLAN\_TEST

tasks:

- include\_role:  
name: add\_vlan  
name: show\_vlan

This is the errors I get:

 ![Ansible add vlans errors.PNG](https://us1.discourse-cdn.com/ansible/original/2X/7/7e5a218f818d70d70a1bd1dd84b978f9c196d8bb.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [December 30, 2019, 10:20am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/6 "2019-12-30T10:20:36Z")

</div>

Hi,

It’s difficult to understand what you want to do… Perhaps you should explain in plain english before trying to code it ?

In your code, there’s a “hosts” directive in your inventory… You should not have it in this file (“hosts” directive is for playbooks)

If you have multiple inventory, perhaps a way to select which inventory to select is to add an argument to your running playbook

ansible-playbook -i /home/ansible/MACCABI/hosts/POC\_ENV.yml myplaybook.yml

Regards,

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 10:41am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/7 "2019-12-30T10:41:10Z")

</div>

Short answer: These code lines create the groups you want.

Details:

1) Quoting from "add\_host"  
[https://docs.ansible.com/ansible/latest/modules/add\_host\_module.html](https://docs.ansible.com/ansible/latest/modules/add_host_module.html)

"Use variables to create new hosts and groups in inventory for use in later  
plays of the same playbook."

2) For example, with this list of dictionaries

&nbsp;&nbsp;&nbsp;&nbsp;"my\_hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"group": "POC\_ENV",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"POC\_ENV\_01",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"POC\_ENV\_02",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"POC\_ENV\_03"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"group": "Nexus",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nexus\_01",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nexus\_02",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"nexus\_03"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;]

3) The debug task

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "name: {{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "groups: {{ item.0.group }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('subelements', my\_hosts, 'hosts') }}"

gives output that explains the plugin "subelements"

&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name: POC\_ENV\_01",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"groups: POC\_ENV"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name: POC\_ENV\_02",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"groups: POC\_ENV"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name: POC\_ENV\_03",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"groups: POC\_ENV"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name: nexus\_01",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"groups: Nexus"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name: nexus\_02",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"groups: Nexus"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name: nexus\_03",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"groups: Nexus"  
&nbsp;&nbsp;&nbsp;&nbsp;]

4) The add\_host tasks

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ item.0.group }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('subelements', my\_hosts, 'hosts') }}"

create groups that can be used in the next plays. For example the play

&nbsp;&nbsp;- hosts: POC\_ENV  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: inventory\_hostname

gives

&nbsp;&nbsp;ok: [POC\_ENV\_01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "POC\_ENV\_01"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [POC\_ENV\_02] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "POC\_ENV\_02"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [POC\_ENV\_03] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"inventory\_hostname": "POC\_ENV\_03"  
&nbsp;&nbsp;}

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 11:26am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/8 "2019-12-30T11:26:17Z")

</div>

Sure. As a working example, the playbook

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group: 'POC\_ENV'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts: "{{ lookup('file',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'/home/ansible/MACCABI/hosts/POC-ENV\_list.yml').splitlines() }}"  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ item.0.group }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_ssh\_private\_key\_file: /home/ansible/.ssh/id\_rsa  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_ssh\_common\_args: -o StrictHostKeyChecking=no  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-o UserKnownHostsFile=/dev/null  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible\_user: ansible  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('subelements', my\_hosts, 'hosts') }}"

&nbsp;&nbsp;- hosts: POC\_ENV  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ msg.split('\n') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host [{{ inventory\_hostname }}]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key [{{ ansible\_ssh\_private\_key\_file}}]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args [{{ ansible\_ssh\_common\_args }}]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user [{{ ansible\_user }}]

gives

ok: [POC\_ENV\_01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"host [POC\_ENV\_01]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"key [/home/ansible/.ssh/id\_rsa]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"args [-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user [ansible]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}  
ok: [POC\_ENV\_02] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"host [POC\_ENV\_02]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"key [/home/ansible/.ssh/id\_rsa]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"args [-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user [ansible]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}  
ok: [POC\_ENV\_03] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"host [POC\_ENV\_03]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"key [/home/ansible/.ssh/id\_rsa]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"args [-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"user [ansible]",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;""  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

Notes:

\* In your code "hosts: POC-ENV" the name of the group is not valid.  
\* See "add\_host" examples how to add variables to the inventory  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/modules/add\_host\_module.html#examples](https://docs.ansible.com/ansible/latest/modules/add_host_module.html#examples)  
\* Run "ansible-lint playbook.yml" before posting the code

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 11:55am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/9 "2019-12-30T11:55:21Z")

</div>

It's possible to put the variables into the "group\_vars". For example

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ item.0.group }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('subelements', my\_hosts, 'hosts') }}"

with the group\_vars below give the same results

&nbsp;&nbsp;$ cat group\_vars/POC\_ENV.yml

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 30, 2019, 12:23pm UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/10 "2019-12-30T12:23:26Z")

</div>

> Alright so this is how my playbook looks like now:

> **(attachments)**
>
> ![Ansible add vlans errors.PNG](https://us1.discourse-cdn.com/ansible/original/2X/4/41a292e6f5a7051cf7d004b21fecb2934b408baa.png)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 1:18pm UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/11 "2019-12-30T13:18:52Z")

</div>

Wrong indentation. Fix:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- group: 'POC\_ENV'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts:

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 30, 2019, 1:39pm UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/12 "2019-12-30T13:39:27Z")

</div>

> 

Alright!  
Thanks for your help so far, now it’s working without errors.  
Ignore previous messages and lets talk about the current status.

The result I get is:

 ![Ansible add vlans errors.PNG](https://us1.discourse-cdn.com/ansible/original/2X/3/3c207ce461f39e0d34796fd6971218f1ded7113e.png)

The result I’m expecting to see is that the ansible will run on each line inside POC\_ENV.yml the task show\_vlan.  
The task show\_vlan redirects to the “roles” folder where there is a show\_vlan.yml file with the commands I want to execute on the remote host (from POC\_ENV per line)  
I want to store the result of the show\_vlan.yml in a variable and print it.

This is the show\_vlan.yml content:

- name: Show VLAN  
ios\_command:  
commands: show vlan brief  
register: show\_vlan

- debug: var=show\_vlan.stdout\_lines

What am I missing next?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 30, 2019, 1:52pm UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/13 "2019-12-30T13:52:17Z")

</div>

The output of the task "add\_host" is weird. "item.1" is empty in couple  
of lines or keeps the variables instead of the host's name.

(I hesitate to hope that it helps)

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [December 31, 2019, 9:15am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/14 "2019-12-31T09:15:13Z")

</div>

I suggest using a directory that contains the files you need to build up your inventory. You can also use symlinks I think

This is described in the documentation in the section starting ' **Aggregating inventory sources with a directory’** here:

[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#using-multiple-inventory-sources](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#using-multiple-inventory-sources)

I think this might be a simpler way to achieve what you want rather than using add\_host and including lots of inventory files directly into your playbook.

It does depend on not having hostnames that clash of course.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 31, 2019, 10:13am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/15 "2019-12-31T10:13:25Z")

</div>

Hi J Hawkesworth,  
Can you explain exactly in which file do I need to add what code?

Considering I have the inventories POC\_ENV.yml, avaya.yml, nexus.yml for example. What file do I need to edit? the ansible.cfg and the inventory file? can you provide the exact solution?

currently my ansible.cfg file directs to /etc/ansible/inventory.yml

and my inventory.yml is empty (which is the master inventory as i understand and from it I am supposed to direct ansible to each one of the inventories mentioned above such as POC\_ENV.yml,etc.)

J Hawkesworth:

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [December 31, 2019, 10:24am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/16 "2019-12-31T10:24:39Z")

</div>

I suggest doing this:

mkdir /etc/ansible/inventory

# copy POC\_ENV.yml, avaya.yml, nexus.yml to /etc/ansible/inventory

changing your ansible.cfg so that it looks for inventory in

/etc/ansible/inventory

IIRC that will load all the inventory files in that directory

I have not tried this using .yaml format inventory file but it works fine using ini format so I suspect it will work ok using .yaml format inventory.

If you run  
`ansible-playbook -vvvvvv a_test_playbook.yml`

the first few lines will show you where it is looking for inventory and what plugins it is using to parse and load your inventory.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [December 31, 2019, 2:22pm UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/17 "2019-12-31T14:22:37Z")

</div>

**Hi,**  
**I have followed the instruction and it’s working so my inventories are now working like I wanted. My end goal is to ssh to the specified machine in POC\_ENV inventory (TEST-AGG-SW) and run the command “show vlan brief” and return it to stdout.**  
**For some reason it is not working and im not even sure if my code runs the show vlan br but not post it to stdout or not sure if it even performs the SSH to the switch or not.**

**My configuration:**

**This is my playbook:**

> **(attachments)**
>
> ![ansible ping proof.PNG](https://us1.discourse-cdn.com/ansible/original/2X/d/dc1e8d962cc45a711701ffa4022e5a487b51d454.png)

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [December 31, 2019, 9:03pm UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/18 "2019-12-31T21:03:27Z")

</div>

Hi

I can see a couple of things that might need changing to get this to work:

Try changing your playbook so it looks like this:

`

---

<div class="post-metadata">

**Author:** ![Yehuda\_Pinhas](https://avatars.discourse-cdn.com/v4/letter/y/a9a28c/32.png) [@Yehuda\_Pinhas](https://forum.ansible.com/u/Yehuda_Pinhas)\
**Post date:** [January 2, 2020, 8:07am UTC](https://forum.ansible.com/t/need-help-on-building-ansible-inventory-file-properly/31881/19 "2020-01-02T08:07:36Z")

</div>

Hi Jon,  
Thank you so much, it’s working now.

Also, much thanks Vladimir for his assistance.

Take care.
