# How to read sub-members from group, within YAML file (i.e. like custom Ansible hosts file)

**URL:** <https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [March 27, 2017, 5:13am UTC](https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067 "2017-03-27T05:13:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 27, 2017, 5:13am UTC](https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067/1 "2017-03-27T05:13:54Z")

</div>

One of my Ansible roles is creating AWS infrastructure. Same time, it also creates YAML file, like Ansible hosts file. Structure is as follow:

[master]  
1.1.1.1 instance-id=i-011111111 # key1=val1  
2.2.2.2 instance-id=i-022222222 # key1=val2  
3.3.3.3 instance-id=i-033333333 # key1=val3

[nodes]  
4.4.4.4 instance-id=i-044444444 # key1=val4  
5.5.5.5 instance-id=i-055555555 # key1=val5  
6.6.6.6 instance-id=i-066666666 # key1=val6  
7.7.7.7 instance-id=i-077777777 # key1=val7  
8.8.8.8 instance-id=i-088888888 # key1=val8  
9.9.9.9 instance-id=i-099999999 # key1=val9

It would be stored at some custom location (i.e. common/vars/inventory.yml).

My query is how to get an instance-id and key1 value from the above file? Where key1 is commented and instance-id is not commented?

Please, someone can provide some hint?

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [March 27, 2017, 7:17am UTC](https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067/2 "2017-03-27T07:17:20Z")

</div>

That's an .ini file , not a yaml file.

Why don't you just uncomment the key1= part?  
Then you can use it as an inventory directly and be able to access  
both instance-id and key1 as host vars.

Personally I'd split this into 2 plays - one provisioner (that has the  
file creation as its last task) and one to use for setting up the  
instances  
for service that uses the previously generated inventory file as its inventory.

(I would be remiss not to mention dynamic inventory here, so I am)

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 27, 2017, 10:58am UTC](https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067/3 "2017-03-27T10:58:15Z")

</div>

Thanks, Dick Davies,

I have few related queries:

1. As best of my understanding **_Instance-Id_** is not a valid key world to be placed in an Ansible hosts file. Is it okay, if we place such keywords without making them comment (#)? Will it create any problem, when such host inventory file will be used with Ansible for normal host reference?

2. Do have to use lookup only? Lookup with with\_items? Please, can you give some example or URL to example?

3. In case, above method is not best practice, please, can you also suggest which ansible module can process INI file placed at the custom location?

Thanks and regards,

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [March 27, 2017, 11:19am UTC](https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067/4 "2017-03-27T11:19:14Z")

</div>

Hi Bhavin

I may be wrong but it sounded like you are generating this file yourself.  
So if instance-id is invalid, why not use instance\_id instead?

If it's an inventory there's no need to use a lookup\_ function, just pass  
that to the next playbook.

e.g.

ansible-playbook createinstances.yml # you run this to create hosts  
and write the host file

ansible-playbook -i host\_file do\_something.yml # use the generated  
file as an inventory

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 27, 2017, 11:25am UTC](https://forum.ansible.com/t/how-to-read-sub-members-from-group-within-yaml-file-i-e-like-custom-ansible-hosts-file/24067/5 "2017-03-27T11:25:53Z")

</div>

Hi Dick,

Yes, you are right. This file is custom generated.

Yes, I will replace from instance-id to instance\_id. At the time of creating such custom configuration (i.e. INI or YAML) file.

Such configuration file (i.e. host inventory file) will be created when infra is created. But it may be used after few weeks or months when any maintenance task will take place. Now, the question is for that maintenance task, how to fetch host’s instance\_id at a later stage? Which module or method can be used? which should point to the custom location INI file.

Thanks and regards,  
Bhavin Parmar
