Building an ini file based on user input

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

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’] }}”

`

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 }}”

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!!!