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.
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.
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.
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?
- name: Add JRE to Catalina.sh
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):
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of catalina startup java process, when start (fork) is used
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?
- name: Add JRE to Catalina.sh
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
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.
So here is another valid case I run into for insertbefore:
- name: install cracklib
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.
I would be sorely tempted to just template that file in that case.
What else is modifying that file out of band?