# insertbefore

**URL:** <https://forum.ansible.com/t/insertbefore/12334>\
**Category:** Ansible Project\
**Created:** [February 11, 2013, 6:55am UTC](https://forum.ansible.com/t/insertbefore/12334 "2013-02-11T06:55:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 11, 2013, 6:55am UTC](https://forum.ansible.com/t/insertbefore/12334/1 "2013-02-11T06:55:33Z")

</div>

If there is appetite for an "insertbefore" option for the lineinfile module, I have just added a pull request for it. I know that in my short use of ansible, there are a few times where this would have been useful.

Also, can someone give a quick overview on how to write and use a test for a module? I've looked at the doc on the web, and .py files in the test directory, but wasn't able to get anywhere.

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 11, 2013, 1:07pm UTC](https://forum.ansible.com/t/insertbefore/12334/2 "2013-02-11T13:07:10Z")

</div>

I kind of think lineinfile is hard to read, and that where possible  
people should always use the template module and push things down when  
possible.

I generally would say no, because if we're trying to model position in  
this module, something is getting away from us.

Templates are \*MUCH\* simpler.

Though I realize they don't always work for everyone.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 11, 2013, 3:16pm UTC](https://forum.ansible.com/t/insertbefore/12334/3 "2013-02-11T15:16:26Z")

</div>

The case where lineinfile is preferable over a template is when you need only one or two lines to be changed in a file that is likely to change completely with an upgrade of the product.

The first example that comes to mind for me, is catalina.sh from Apache Tomcat. I need to add a few lines to it to set the path to the JRE, yet I don't want to suck the entire file into a template, because the next time a deploy a different version of Tomcat, I'd have to do it all over again. Also, when I run two slightly different versions of Tomcat in the same environment, I can use the same task to add a couple of lines to catalina.sh, while I'd have to have different templates for different versions.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 11, 2013, 3:40pm UTC](https://forum.ansible.com/t/insertbefore/12334/4 "2013-02-11T15:40:42Z")

</div>

> The case where lineinfile is preferable over a template is when you need  
> only one or two lines to be changed in a file that is likely to change  
> completely with an upgrade of the product.\

> The first example that comes to mind for me, is catalina.sh from Apache  
> Tomcat. I need to add a few lines to it to set the path to the JRE, yet I  
> don't want to suck the entire file into a template, because the next time a  
> deploy a different version of Tomcat, I'd have to do it all over again.  
> Also, when I run two slightly different versions of Tomcat in the same  
> environment, I can use the same task to add a couple of lines to  
> catalina.sh, while I'd have to have different templates for different  
> versions.

Sure, I understand the reasons, just sharing for those that may not  
have known \*often\* lineinfile is avoidable.

What is an example of insertbefore as it would look in a playbook?

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 11, 2013, 3:52pm UTC](https://forum.ansible.com/t/insertbefore/12334/5 "2013-02-11T15:52:34Z")

</div>

- name: Add JRE to Catalina.sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile: dest=${tomcat\_dir}/bin/catalina.sh insertbefore='^# OS specific support.' regexp='^JRE\_HOME=' line="JRE\_HOME=${java\_dir}" backup=yes

The format of catalina.sh looks like this (in the middle):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# CATALINA\_PID (Optional) Path of the file which should contains the pid  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# of catalina startup java process, when start (fork) is used

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [February 11, 2013, 3:56pm UTC](https://forum.ansible.com/t/insertbefore/12334/6 "2013-02-11T15:56:35Z")

</div>

Yves Dorfsman wrote:

> > Sure, I understand the reasons, just sharing for those that may not  
> > have known \*often\* lineinfile is avoidable.
> > 
> > What is an example of insertbefore as it would look in a playbook?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Add JRE to Catalina.sh  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile: dest=${tomcat\_dir}/bin/catalina.sh insertbefore='^# OS  
> specific support.' regexp='^JRE\_HOME=' line="JRE\_HOME=${java\_dir}"  
> backup=yes

You know tomcat supports a $CATALINA\_BASE/bin/setenv.sh to contain all of  
your variables, such as JAVA\_HOME, right?

Daniel

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 11, 2013, 4:10pm UTC](https://forum.ansible.com/t/insertbefore/12334/7 "2013-02-11T16:10:38Z")

</div>

No I didn't! Thanks for pointing this out, reading and experimenting with it as we speak...

This was just one case where I think insertbefore has (had!) value among others, pretty much any time you want to insert before a section, but can regex the end of the last section, insertbefore will make your life easier. I'll look for another example where I had to work around not having it, and post later.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 11, 2013, 10:48pm UTC](https://forum.ansible.com/t/insertbefore/12334/8 "2013-02-11T22:48:27Z")

</div>

So here is another valid case I run into for insertbefore:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: install cracklib  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile: dest=/etc/pam.d/common-passowrd insertbefore='^password sufficient pam\_unix.so' regexp='^password required pam\_cracklib' line='password required pam\_cracklib.so retry=3 minlen=8'

I think for most valid cases for insertafter, you will find a valid case for insertbefore.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 11, 2013, 11:19pm UTC](https://forum.ansible.com/t/insertbefore/12334/9 "2013-02-11T23:19:34Z")

</div>

I would be sorely tempted to just template that file in that case.

What else is modifying that file out of band?
