1

How can i use ansible lineinfile module to simulate working of below sed command.

sed -i _bkp '/(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.1)(PORT = 1501))/d;s/(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.2)(PORT = 1501))/(ADDRESS = (PROTOCOL = TCP)(HOST = my-site.com)(PORT = 1501))/;/(BALANCE = yes))/d' myconffile
2
  • Wouldn't be better to use '/AAA/d;s/BBB/CCC/;/DDD/d' ? Jun 11 '19 at 4:23
  • @VladimirBotka : These lines are part of a config file.I was looking for a similar implementation in ansible using lineinfile/replace module.
    – AVS
    Jun 11 '19 at 4:59
1

To replace all lines in the file replace module is needed. This is the equivalent (backup differs).

  tasks:
    - lineinfile:
        path: myconffile
        backup: yes
        state: absent
        regexp: '\(ADDRESS = \(PROTOCOL = TCP\)\(HOST = 10.0.0.1\)\(PORT = 1501\)\)'
    - replace:
        path: myconffile
        backup: yes
        regexp: '\(ADDRESS = \(PROTOCOL = TCP\)\(HOST = 10.0.0.2\)\(PORT = 1501\)\)'
        replace: '(ADDRESS = (PROTOCOL = TCP)(HOST = my-site.com)(PORT = 1501))'
    - lineinfile:
        path: myconffile
        backup: yes
        state: absent
        regexp: '\(BALANCE = yes\)'

The simplified version is below.

  tasks:
    - lineinfile:
        path: myconffile
        backup: yes
        state: absent
        regexp: "{{ item }}"
      loop:
        - 'AAA'
        - 'DDD'
    - replace:
        path: myconffile
        backup: yes
        regexp: 'BBB'
        replace: 'CCC'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.