# lineinfile Best Practice?

**URL:** <https://forum.ansible.com/t/lineinfile-best-practice/19564>\
**Category:** Ansible Project\
**Created:** [October 20, 2015, 3:05pm UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564 "2015-10-20T15:05:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josef\_Fortier](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Josef\_Fortier](https://forum.ansible.com/u/Josef_Fortier)\
**Post date:** [October 20, 2015, 3:05pm UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564/1 "2015-10-20T15:05:57Z")

</div>

What ideas do others have on Best Practice for using lineinfile?

As a potential conversation starter, here’s a couple of ideas that have recently gelled for me:

1. Use two rules:

- First delete all versions of the line
- Then add the finished version of the line.  
This makes the process more idempotent (which, granted lineinfile has inherent problems with…)  
For context, this idea gells around adding a mailrelay statement for postfix’s main.cf. In my environment I have 4 different platforms with various ad-hoc maintenance (covering quite a few years).

1. Use a (trailing) comment header (i.e. a “here are our changes” line) and then append changes after this.

Some more context here, I suspect lineinfile appeals to those of us who have an existing set of servers that we are incrementally bringing into line. The “correct” path is likely to use a template of a file, but that requires determining which of the (our various) distro’s is the “correct” set of settings. For something like “use our mail relay” this is a fairly large hurdle.

Thanks for any comments in advance 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 20, 2015, 3:10pm UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564/2 "2015-10-20T15:10:54Z")

</div>

rule 0) If at all possible, use template module instead, it gives you  
full control and verification.

---

<div class="post-metadata">

**Author:** ![Josef\_Fortier](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Josef\_Fortier](https://forum.ansible.com/u/Josef_Fortier)\
**Post date:** [October 20, 2015, 7:50pm UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564/3 "2015-10-20T19:50:01Z")

</div>

Your reply, while correct, seems designed to shut down useful feedback. Perhaps you did not mean it that way.

I noted up front that:

> The “correct” path is likely to use a template of a file

I’m aware that lineinfile has problems. Which is why I’m requesting feedback on how one might use it best.

For a lot of use cases lineinfile, is an immediately useful expedient step, with advantages that far outweigh it’s disadvantages.  
In the case I suggested, for example:

> adding a mailrelay statement for postfix’s [main.cf](http://main.cf/).

I have 6 different legacy versions of main.cf floating around, covering two different distributions. Doing it the correct way means:

1. Reading through each version to determine if there is something special about a given setup.
2. Researching the parts I do not fully understand
3. Determining which of the differing parts should be in the final version

That work is good work, but it’s a lot of work. It’s not unrealistic to suggest that’s a week or more of work (postfix is pretty flexible, which translates to reading though a _lot_ of options).  
Most of those options don’t really have a final “correct” setting. But I know up front what relayhost should be.

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [October 20, 2015, 8:31pm UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564/4 "2015-10-20T20:31:11Z")

</div>

Hello Josef,

could you explain what exactly the problem is here? Maybe posting some examples with actual/ and expected results?

I have different relay hosts in main.cf, too. I just use a var per group and lineinfile to set relay and didn’t have a problem with that.

esco

---

<div class="post-metadata">

**Author:** ![Josef\_Fortier](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@Josef\_Fortier](https://forum.ansible.com/u/Josef_Fortier)\
**Post date:** [October 20, 2015, 9:47pm UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564/5 "2015-10-20T21:47:36Z")

</div>

Hi Esco:

---

<div class="post-metadata">

**Author:** ![Javier\_Palacios](https://avatars.discourse-cdn.com/v4/letter/j/67e7ee/32.png) [@Javier\_Palacios](https://forum.ansible.com/u/Javier_Palacios)\
**Post date:** [October 21, 2015, 8:25am UTC](https://forum.ansible.com/t/lineinfile-best-practice/19564/6 "2015-10-21T08:25:08Z")

</div>

Hello Josef,

you might have a look to the augeas role. I’ve not used it with ansible but with puppet, and if you are changing a standard configuration file is probably the best approach. Even if you don’t want to use the role, use augeas with a ‘command’ might also help

Javier Palacios
