Ansible lineinfile insertafter injects line at end of file

ansible lineinfile append to end of line
ansible lineinfile insertafter multiple lines
ansible lineinfile newline
ansible replace multiple lines
ansible lineinfile variable
ansible blockinfile newline
ansible remove duplicate lines in file
ansible replace multiple files

I'm using lineinfile as follows:

lineinfile dest=./hosts_exp insertafter='\[hosts1\]' line="xxxxxxxxx" state=present

My hosts_exp is as follows:

[local]
localhost

[hosts1]

[hosts2]

[hosts3]

lineinfile inserts the text after [hosts3] instead of inserting it after [hosts1].

use:

lineinfile:
  dest: "./hosts_exp"
  line: "xxxxxxxxx"
  insertafter: '^\[hosts1\]'
  state: present

Ansible lineinfile insertafter injects line at end of file, I'm using lineinfile as follows: lineinfile dest=./hosts_exp insertafter='\[hosts1\]' line​="xxxxxxxxx" state=present. My hosts_exp is as follows: [local] localhost  Neither lineinfile nor replace (see the duplicate link for a suggested solution) were the issue. In fact, they were doing their respective jobs - and ending lines with a line feed. The problem arises because before that the file only has CLRF terminators, which are invisible when they exist exclusively.

example:

- name: "blah"
  lineinfile:
    dest: "/test.sh"
    insertafter: 'test text'
    line: "text add"
    state: present

Ansible lineinfile insertafter injects line at end of file, I'm using lineinfile as follows: lineinfile dest=./hosts_exp insertafter='\[hosts1\]' line​="xxxxxxxxx" state=present My hosts_exp is as follows: [local] localhost  SUMMARY BUG: The lastline is deleted and replaced, with lineinfile insertafter=EOF Want to add a new string to the end of the file, but the result is that the lastline is deleted and replaced. Replace last line instead of insert after last line.

It appears redundant, but you need to specify the regex too:

lineinfile:
  dest: ./hosts_exp
  insertafter: '\[hosts1\]'
  regexp: '\[hosts1\]'
  line: "xxxxxxxxx"
  state=present

Why? The regexp says "look for this line". The insertafter says "inject the line here".

I tested this; here's the commit. There are a few minor changes in my commit from the line above, use as necessary.

lineinfile – Manage lines in text files, insertafter. string. Choices: EOF ←. *regex*. Used with state=present . If specified, the line will be inserted after the last match of specified regular expression. It would be more robust to add insertafter: '^AllowUsers.+$', as that will create the AllowUsers line at the end of the file if the regex isn't found. If it is found, it will replace the line. – dthor Mar 20 '18 at 18:00

Use backrefs: yes

lineinfile:
  backrefs: yes
  dest: ./hosts_exp
  insertafter: '\[hosts1\]'
  regexp: '\[hosts1\]'
  line: "xxxxxxxxx"
  state=present

This flag changes the operation of the module slightly; insertbefore and insertafter will be ignored, and if the regexp doesn't match anywhere in the file, the file will be left unchanged. If the regexp does match, the last matching line will be replaced by the expanded line parameter.

Replace a line in a config file with ansible, How do you replace a line in a file using Ansible? If specified, the line will be inserted after the last match of specified regular expression. A special value is available; EOF for inserting the line at the end of the file. If specified regular expression has no matches, EOF will be used instead. May not be used with backrefs.

How to add lines to a file in Ansible, How do you add a line in a file using Ansible? Ansible lineinfile module is helpful when you want to add, remove, modify a single line in a file. You can also use conditions to match the line before modifying or removing using the regular expressions. You can reuse and modify the matched line using the back reference parameter. Consider yourself having any of these following requirement

Ansible lineinfile examples, is helpful when you want to add, remove, modify a single line in a file. You can also use conditions to match the line before modifying or removing using the regular expressions. You can reuse and modify the matched line using the back reference parameter. If the regular expression is not matched, the line will be added to the file in keeping with insertbefore or insertafter settings. When modifying a line the regexp should typically match both the initial state of the line as well as its state after replacement by line to ensure idempotence.

How to use the Ansible lineinfile module, parameter along with the regexp parameter. If the regexp matches a line/multiple lines, then the last matched line will be replaced. Also, the grouped elements in regexp are populated and can be used for modification. In the below example we are commenting a line. As of Ansible 2.3, the dest option has been changed to path as default, but dest still works as well. Option follow has been removed in Ansible 2.5, because this module modifies the contents of the file so follow=no doesn’t make sense. When more then one block should be handled in one file you must change the marker per task.

Comments
  • What version of ansible are you using? I'm getting the expected result using your test, xxxxxxx is added after [hosts01].
  • @RamondelaFuente Now try after having line "xxxxxxxxx" already in your hosts_exp file. I mean try to add another same line.
  • Ansible's lineinfile module won't add the same line twice - the task is marked "ok" as I expect it to be.
  • It worked for me w/o regex, the issue was that it wasn't taking the same text again and again for insertion, I need to provide a new text and that aligns with my requirement here. Regex is not necessary.
  • To clarify, using the regex is only necessary if you do NOT want the line added if both regex and insertafter match. So this is necessary if it should only be applied the first time which is the most common situation.