J
J
juffinhalli2015-06-04 15:44:52
linux
juffinhalli, 2015-06-04 15:44:52

How to append a line to a file once using Ansible?

Given:
File /etc/network/interfaces
Conditions:
The file already contains settings that should not be changed.
Task:
Add the line foo after the line bar to the file, but only once. The second run of the playbook should not add anything. Only one string foo needed
My attempts:
Know how to do it with command module and sed, but looking for a native solution in Ansible
Trying to solve with lineinfile either destroyed the string bar or added foo on every run of ansible-playbook.
Thanks in advance for your valuable advice.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
aardvarkx1, 2015-06-04
@juffinhalli

Now I tried to add a line to the file using lineinfile and everything works as it should. The row is added and when restarted, there is no re-adding.
Here is an example as described for ansible.
- name: insert line in file
lineinfile: dest=/tmp/ans insertafter="^bar" line="foo"

Y
Yuri Shikanov, 2015-06-04
@dizballanze

Make a template for the interfaces file and just use the template module which in your template will replace {{ foo }} with what you need and replace /etc/network/interfaces. Because the result of rendering the template with the same values ​​of foo is the same - you can safely run the playbook any number of times.

V
Vladimir, 2015-06-06
@mvs

Finally, read the documentation docs.ansible.com/lineinfile_module.html and use the regexp parameter to determine part of a line or a complete line to replace.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question