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?

eden881
  • 195
  • 1
  • 1
  • 10
  • I guess you can't. File an [issue on GitHub](https://github.com/ansible/ansible/issues/new) and explain your use case, or [patch the code yourself](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/files/ini_file.py). – techraf Mar 21 '18 at 14:40

3 Answers3

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.

ankuj
  • 1
  • 2
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.

Henrik Pingel
  • 8,676
  • 2
  • 24
  • 38
  • 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 '18 at 11:26
  • `lineinfile` is an option, but I believe that this is not the correct way to manage ini files – eden881 Mar 24 '18 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.)

dexter
  • 64
  • 1
  • 5
  • 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 '18 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 '18 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 '18 at 19:35