1

I use Ansible to build configuration files in ini format. When I use the ini_file module with option and value pair it works as expected, for example:

- name: Create configuration file
  ini_file:
    path: /tmp/test.conf
    state: present
    section: lol
    option: foo
    value: bar

Would result with:

[lol]
foo = bar

However I want a specific section to exist without options in it, like so:

- name: Create configuration file
  ini_file:
    path: /tmp/test.conf
    state: present
    section: lol

But all it does is reporting ok on the task and moves on to the next one.

When I use verbose mode I can see: ok: [localhost] => {"changed": false, "msg": "OK", "path": "/tmp/test.conf", "state": "absent"}

How can I use the module to create option-less sections?

1

3 Answers 3

0

ini is an informal standard, but from my understanding there isn't much sense in a section without an option. That would be my first guess why this isn't implemented in Ansible.

Rather then using the ini-Module I would suggest to use the lineinfile module to ensure that a section is present in a ini file.

2
  • My application is using those sections to define some things at load time (listening ports, for example), and the options are for extra configuration parameters. I'm sure my app is not the only one that use option-less sections...
    – eden881
    Mar 24, 2018 at 11:26
  • lineinfile is an option, but I believe that this is not the correct way to manage ini files
    – eden881
    Mar 24, 2018 at 11:28
0

For building configuration files go with templates, it is a proper way. Use ini_file module only for editing files. Try to avoid lineinfile module, use it as a last resort.

rule 0) If at all possible, use template module instead, it gives you full control and verification. Brian Coca (Senior Software Engineer, Ansible, Red Hat.)

3
  • The problem with using the template module in my case is that it will overwrite the destination conf file each time I run the play...
    – eden881
    Mar 25, 2018 at 18:45
  • I didn't mention that my application might rewrite and modify the conf files, both on start and on the fly. I do use template in several places where I can, but for some files (like the one discussed) it's unavoidable in my case.
    – eden881
    Mar 25, 2018 at 18:48
  • Please than clarify your question. Initially you said that you are using ansible to build configuration files in ini format. Now it sounds like you want to edit ini files with ansible. Please edit your question to cover your case, because for initial question you already got the answer.
    – dexter
    Mar 25, 2018 at 19:35
0

workaround is use allow no value, with blank/ commented code in option.

- ini_file:
    dest: '/tmp/telegraf.conf'
    section: '[inputs.interrupts]'
    option: '  ## no configuration'
    allow_no_value: yes

But if there is already value in you section, then you have to remove it by state: absent if you want section with no options & value. then add. Sorry but i also dint find real answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .