# Building an ini file based on user input

**URL:** <https://forum.ansible.com/t/building-an-ini-file-based-on-user-input/25241>\
**Category:** Ansible Project\
**Created:** [August 13, 2017, 9:21pm UTC](https://forum.ansible.com/t/building-an-ini-file-based-on-user-input/25241 "2017-08-13T21:21:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chipcha](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@chipcha](https://forum.ansible.com/u/chipcha)\
**Post date:** [August 13, 2017, 9:21pm UTC](https://forum.ansible.com/t/building-an-ini-file-based-on-user-input/25241/1 "2017-08-13T21:21:30Z")

</div>

Hi all,

I developed a small playbook which uses an inventory file to store my variables.  
I chose an ini format since it is more easy to define groups and it makes the file more organize and readable.  
My first question is, do you recommend this type of management, though an ini file, or is there another alternative way to do this?

Now, I want to build a new ansible tool, that generates this inventory ini file using the user input.  
I saw a module called ini\_file which I thought might be helpful, but it seems that it format the file different than the ini format described in the ansible documentation [http://docs.ansible.com/ansible/latest/intro\_inventory.html](http://docs.ansible.com/ansible/latest/intro_inventory.html)

Here are few examples of the differences between the two:

1. ini\_file module - When adding a value with ini\_file module the syntax must always be [key]=[value] even if there is no value:

`[webservers] foo=foo.example.com bar=None`

ansible docs - In the docs the ini file uses standalone names, so we can have a group with only hostnames, for example:

`[webservers] foo.example.com bar.example.com`

1. ini\_file module - It is not possible to declare host variables in a one liner:  
`  
[atlanta]  
http\_port=80  
maxRequestsPerChild=808

`

ansible docs:

`[atlanta] host1 http_port=80 maxRequestsPerChild=808`

Also it seems that the ini\_file module does not support reading from the ini file only insert or update.

What do you recommend for the best approach? is there any other ansible module I can use to manipulate data in my INI file or should I use a different approach?

Here is an example of my ini file:  
`

# INI file

[data\_storage]  
data\_storage name=name1 address1=12.12.12.12 path1=data\_storage dc\_name=DC1

[data\_storage\_ver2]  
data\_storage\_ver2 name=name2 address1=12.12.12.13 path1=data\_storage2 dc\_name=DC2

[storage:children]  
data\_storage  
data\_storage\_ver2  
`

The playbook looks like this:  
`

# main playbook

…  
…

- include: library/add\_NFS\_domain.yml storage={{ item }}  
with\_inventory\_hostnames: storage  
…

# task file

- ovirt\_hosts\_facts:  
pattern: “status=up and datacenter={{ hostvars[storage][‘dc\_name’] }}”  
auth: “{{ ovirt\_auth }}”

- ovirt\_storage\_domains:  
name: “{{ hostvars[storage][‘name’] }}”  
host: “{{ ovirt\_hosts[0].name }}”  
data\_center: “{{ hostvars[storage][‘dc\_name’] }}”  
auth: “{{ ovirt\_auth }}”  
nfs:  
address: “{{ hostvars[storage][‘ip1’] }}”  
path: “{{ hostvars[storage][‘path1’] }}”

`

---

<div class="post-metadata">

**Author:** ![alikins](https://avatars.discourse-cdn.com/v4/letter/a/d07c76/32.png) [@alikins](https://forum.ansible.com/u/alikins)\
**Post date:** [August 14, 2017, 4:43pm UTC](https://forum.ansible.com/t/building-an-ini-file-based-on-user-input/25241/2 "2017-08-14T16:43:03Z")

</div>

The default ansible inventory format is only ‘ini-like’ so I wouldn’t expect tools for handling standard ini files (like the ini\_file module) to work well.  
The examples you mention are good examples of where that falls apart.

Haven’t ran the examples or dug in yet, but first thought is it looks like this is implementing what inventory and the ‘hosts:’ keyword does.

I would try a more standard approach first, notes later…

As far as generating a inventory with ansible tasks… That smells like the wrong approach to me.

inventory ini file looks a little odd to me. Having a group and a host with the same name is confusing (data\_storage)  
Is ‘data\_storage\_ver2’ a valid resolveable hostname? if not, need to provide a value for ansible\_host

`# main playbook `  
`- include: library/add_NFS_domain.yml storage={{ item }}`  
`with_inventory_hostnames: storage`  
``

(why is the yaml file in library/ ? library is usually for per playbook ansible modules)  
Where does ovirt\_hosts come from?

try roughly:

# storage.ini (inventory)

data\_storage storage\_domain\_name=name1 address1=12.12.12.12 path1=data\_storage dc\_name=DC1  
data\_storage\_ver2 storage\_domain\_name=name2 address1=12.12.12.13 path1=data\_storage2 dc\_name=DC2

[storage]  
data\_storage  
data\_storage\_ver2

# main playbook assuming ovirt\_hosts and ovirt\_auth is defined somewhere…

- name: ensure ovirt storage domains for each host

# apply to hosts in the ‘storage’ group

hosts: storage

- tasks:

- ovirt\_hosts\_facts:  
pattern: “status=up and datacenter={{ dc\_name }}”  
auth: “{{ ovirt\_auth }}”

- ovirt\_storage\_domains:  
name: “{{ storage\_domain\_name }}”  
host: “{{ ovirt\_hosts[0].name }}”  
data\_center: “{{ dc\_name }}”  
auth: “{{ ovirt\_auth }}”  
nfs:  
address: “{{ ip1 }}”  
path: “{{ path1 }}”

---

<div class="post-metadata">

**Author:** ![chipcha](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@chipcha](https://forum.ansible.com/u/chipcha)\
**Post date:** [September 5, 2017, 11:23am UTC](https://forum.ansible.com/t/building-an-ini-file-based-on-user-input/25241/3 "2017-09-05T11:23:10Z")

</div>

Hi Adrian,  
Thanks for your response!

see my comments inline

> The default ansible inventory format is only ‘ini-like’ so I wouldn’t expect tools for handling standard ini files (like the ini\_file module) to work well.  
> The examples you mention are good examples of where that falls apart.
> 
> Haven’t ran the examples or dug in yet, but first thought is it looks like this is implementing what inventory and the ‘hosts:’ keyword does.
> 
> I would try a more standard approach first, notes later…
> 
> As far as generating a inventory with ansible tasks… That smells like the wrong approach to me.
> 
> inventory ini file looks a little odd to me. Having a group and a host with the same name is confusing (data\_storage)  
> Is ‘data\_storage\_ver2’ a valid resolveable hostname? if not, need to provide a value for ansible\_host

You are right, it is confusing, I changed the alias name to a different name.

‘data\_storage\_ver2’ is the applicative storage name that I use to add it to my setup.  
It isn’t the host name, I use the ip of it to connect to it with mount command.

> `# main playbook `  
> `- include: library/add_NFS_domain.yml storage={{ item }}`  
> `with_inventory_hostnames: storage`  
> ``
> 
> (why is the yaml file in library/ ? library is usually for per playbook ansible modules)

I changed my code to work roles.  
Thanks.

> Where does ovirt\_hosts come from?

ovirt\_hosts is a variable initialized in ovirt\_hosts\_facts.

> try roughly:
> 
> # storage.ini (inventory)
> 
> data\_storage storage\_domain\_name=name1 address1=12.12.12.12 path1=data\_storage dc\_name=DC1  
> data\_storage\_ver2 storage\_domain\_name=name2 address1=12.12.12.13 path1=data\_storage2 dc\_name=DC2

> [storage]  
> data\_storage  
> data\_storage\_ver2

I gave it a try, seems to do the trick. Thanks!!!
