Appending a string at the end of line

10 views
Skip to first unread message

Rakesh Parida

unread,
Nov 19, 2019, 6:08:32 PM11/19/19
to Ansible Project
Hi ,

I have a file :

cat test.txt
Baseline: ABC ABC1 ABC23 ABC24 +ABC33 ABC_PAD  +ABC_PAD_CAD 

Now i want to add +TEST_ABC at the end of Line :
Desired O/P :
Baseline: ABC ABC1 ABC23 ABC24 +ABC33 ABC_PAD  +ABC_PAD_CAD +TEST_ABC

I am using lineinfile module but the string is getting added  after the line 
Desired O/P :
Baseline: ABC ABC1 ABC23 ABC24 +ABC33 ABC_PAD  +ABC_PAD_CAD +TEST_ABC

Actual O/P:
Desired O/P :
Baseline: ABC ABC1 ABC23 ABC24 +ABC33 ABC_PAD  +ABC_PAD_CAD 
+TEST_ABC

Below is the task

---
- hosts: localhost
  vars:
    file_location: /home/raka/test.txt
  gather_facts: False

  tasks:
    - name: Update file
      lineinfile:
        path: "{{ file_location }}"
        state: present
        backup: yes
        insertafter: '(\s+Baseline: .*)(/>)$'
        line: "{{ add_name }}"

  Now on executing the playbook as 
ansible-playbook file-update.yml -e "add_name=+TEST_ABC"


Reply all
Reply to author
Forward
0 new messages