lineinfile Best Practice?

415 views
Skip to first unread message

Josef Fortier

unread,
Oct 20, 2015, 11:05:57 AM10/20/15
to Ansible Project
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).

2) 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 :-)

Brian Coca

unread,
Oct 20, 2015, 11:10:58 AM10/20/15
to Ansible Project
rule 0) If at all possible, use template module instead, it gives you
full control and verification.



--
Brian Coca

Josef Fortier

unread,
Oct 20, 2015, 3:50:01 PM10/20/15
to Ansible Project
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.

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.


esco real

unread,
Oct 20, 2015, 4:31:11 PM10/20/15
to Ansible Project
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

Josef Fortier

unread,
Oct 20, 2015, 5:47:36 PM10/20/15
to Ansible Project
Hi Esco:


On Tuesday, October 20, 2015 at 3:31:11 PM UTC-5, esco real wrote:
could you explain what exactly the problem is here? Maybe posting some examples with actual/ and expected results?

Nothing insurmountable. Just trying to formulate in my own mind what works the best. I was/am able to work around the issues I'll detail, but I'd rather have had a uniform approach up front.

First Issue: pre-existing relayhost on a handful of servers.
I used regex: to catch the empty hosts
Unintended result, hosts with existing relayhost had two set.

Second issue: Too loose/tight regex (either or both).
Correcting the first issue either led to duplication's or just updated the duplicates to uniform values.

It looks like backrefs is a partial fix for this, but after a little reflection I realized that in this case (and many other cases like it) my best approach is to create two rules, one that deletes the line first, and then a role to add it. In particular this catches multiple lines in file. It's also easier to get the regex correct (i.e, "delete all other lines that might set the var")

Javier Palacios

unread,
Oct 21, 2015, 4:25:17 AM10/21/15
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/251451e7-2c36-4576-938e-acf548938978%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages