Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which module to use to edit files - Ansible

Tags:

ansible

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 ?

like image 506
Luv33preet Avatar asked Dec 18 '22 06:12

Luv33preet


1 Answers

There are several options, depending on your purpose.
The lineinfile - module is the best option, if you just want to add, replace or remove one line.
The replace - module is best, if you want to add, replace or delete several lines.
The blockinfile - module can add several lines, surrounded by markers.

If you only want to change two or three lines, you could use as many calls of lineinfile. To change a whole config file, I would recommend, like the commenters suggest, use the template - module.

like image 136
ThoFin Avatar answered Jan 08 '23 16:01

ThoFin