Which module to use to edit files - Ansible

Viewed 17769

I want to edit the configuration file of telegraf(system metrics collecting agent).

Telegraf comes in with a default config file which can be edited. There are many input and output plugins defined in there, which are commented out and can be added by removing the comments and also be customized.

I want to edit only some of the plugins defined there, not all of them. For example, consider this is the file,

[global]
  interval='10s'

[outputs.influxdb]
  host=['http://localhost:8086']

#[outputs.elasticsearch]
#  host=['http://localhost:9200']

[inputs.netstat] 
  interface='eth0'

Now, I want to edit the 3 blocks, global, outputs.influxdb and inputs.netstat. I don't want to edit outputs.elasticsearch but also want that the block outputs.elasticsearch should remain in the file.

When Using Ansible, I firstly used Template module, but if I use that, then the commented data would be lost.

Then I used the ini_file module, instead of editing the already present block, it adds a new block even if it is already present, and results in something like this,

[outputs.influxdb]
 host=[http://localhost:8086]
[outputs.influxdb]
 host=[http://xx.xx.xx.xx:8086]

Which module is ideal for my scenario ?

2 Answers
Related