Ansible lineinfile adds new line before edit - ansible

I'm trying to edit the value of existing line without overriding it, when I run the playbook it adds the value in a new line
Line: server=foo
Value I'm trying to add: boo
Exception:
Line: server=foo,boo
Result:
Line: server=foo,
boo
Code:
- name: Update
lineinfile:
path: "{{ file_path }}"
backrefs: yes
regexp: '(server=.*)'
line: '\1,boo'

Resolved, since I run on WSL I had to do dos2unix and after edits unix2dos
and no extra new line added

Related

Ansible lineinfile won't add at end of file if line is already present somewhere

The code:
- name: Mark ending
delegate_to: localhost
lineinfile:
create: yes
insertafter: 'EOF'
path: '{{ output_file }}'
line: '=================='
Ansible version 2.8.6
This line is only added the first time the command is executed. I would expect it to add it every time.
The documentation recommends insertafter: 'EOF' for inserting lines at the end of a file, so that's what I'm using.
But now it seems to ignore every insert of this after the first.
More is inserted in between. I'm just using this command to indicate a particular thing has passed.
Setting validate: '' Changes nothing. How do I fix this?

Ansible add a line in file

I am trying to add a new line in a file through ansible lineinfile module. Below is the task defined in the playbook.
lineinfile:
path: /etc/httpd/file.conf
line: myfilecontent
It works fine and add a new line in the file. But when i modify the line content to some another value i.e mynewfilecontent it adds another line instead of updating it.
lineinfile:
path: /etc/httpd/file.conf
line: mynewfilecontent
Help is appreciated.
Thanks
Use the state parameter of lineinfile module and create the structure below
my_lines:
- line: myfilecontent
state: absent
- line: mynewfilecontent
state: present
The state parameter controls if the line is present, or absent in the file. See the example below
- hosts: localhost
vars:
my_lines:
- line: myfilecontent
state: absent
- line: mynewfilecontent
state: present
tasks:
- lineinfile:
path: /tmp/test.conf
create: yes
line: "{{ item.line }}"
state: "{{ item.state }}"
loop: "{{ my_lines }}"
Note 1.
The structure can be simplified by adding the state parameter only if the line is absent
my_lines:
- line: myfilecontent
state: absent
- line: mynewfilecontent
Declare default state present in the loop
state: "{{ item.state|default('present') }}"
Note 2.
The default state, defined in the module, is present, hence the state parameter may be omitted if not present in the data structure
state: "{{ item.state|default(omit) }}"
All 3 variations above are functionally equivalent.
If you want your line to potentially replace an existing line in the file then you need to provide a regexp argument to the lineinfile module.
The regular expression to look for in every line of the file.
For state=present, the pattern to replace if found. Only the last line found will be replaced.
For state=absent, the pattern of the line(s) to remove.
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.
The last line which matches your regexp will be replaced with your line value; if there is no match then a new line will be added.
Without a regexp argument the module will just check for an exact match to your line value.
See https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html.

replace a line in a .profile using ansible

I'm trying to insert a line at the end of my .profile using ansible. It should add to the end if the line is absent. I'm trying the following, but it does'nt do anything:
- name: update profile
lineinfile:
dest: ~/.profile
regexp: 'PATH=$PATH:$HOME/.local/bin'
state: absent
insertafter: EOF
line: 'PATH=$PATH:$HOME/.local/bin'
Thanks in advance for any suggestions
Use state: present if you want line to exist, and not sure why you want to use regexp in this case.
Also EOF is the default value for insertafter, so no need to define it.
- name: update profile
lineinfile:
dest: ~/.profile
state: present
line: 'PATH=$PATH:$HOME/.local/bin'

replace and lineinfile module does not replace a line

I am trying to configure the WSO2 API manager. I have to change some lines in some xml configuration files.
How do I replace in APIM_HOME/repository/conf/api-manager.xml a line
<GatewayEndpoint>http://${carbon.local.ip}:${http.nio.port},https://${carbon.local.ip}:${https.nio.port}</GatewayEndpoint>
with
<GatewayEndpoint>http://ip-111-111-11.abc.xyz.com:1234,https://ip-111-111-11.abc.xyz.com:{https.nio.port}</GatewayEndpoint>
I have tried some regex with lineinfile,replace modules & state=present but it is adding the new line to the end of file. I would rather want to replace the existing line with the new line in same position as that of old line.
Try this -
- name: "Test lineinfile"
lineinfile:
dest: "/etc/ansible/lineinfile.xml"
state: "present"
line: "<GatewayEndpoint>http://ip-111-111-11.abc.xyz.com:1234,https://ip-111-111-11.abc.xyz.com:{https.nio.port}</GatewayEndpoint>"
regexp: "<GatewayEndpoint>"
This worked for me and replaced the line
<GatewayEndpoint>http://${carbon.local.ip}:${http.nio.port},https://${carbon.local.ip}:${https.nio.port}</GatewayEndpoint>
with the line
<GatewayEndpoint>http://ip-111-111-11.abc.xyz.com:1234,https://ip-111-111-11.abc.xyz.com:{https.nio.port}</GatewayEndpoint>

How to add new line in ansible playbook

I am trying to add new line in yaml configuration file through ansible playbook using lineinfile. What I have tried till now.
#attempt-1
- name: "Configure Node River"
lineinfile: "dest=/path/to/config.conf line='node.river: river_name'"
#attempt-2
- name: "Configure Node River"
lineinfile: "dest=/path/to/config.conf state=present regexp='^' line='node.river: river_name'"
Also I have tried as per reference document.
# Add a line to a file if it does not exist, without passing regexp
- lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo"
Can anyone guide me how to add new line which is not present in file. I know I can add End of the File by adding regexp='' insertafter=EOF in my #attempt-1. But I want to add above line in middle of the file.
As I understand you can use insertafter=<regexp>:
--- play.yml
- hosts: localhost
gather_facts: False
tasks:
- name: "Configure Node River"
local_action: 'lineinfile dest=/home/ig/test.conf
line="node.river: river_name" insertafter="first line"'
test.conf before:
first line
second line
test conf after:
first line
node.river: river_name
second line

Resources