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

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?

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)

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,

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

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