Software >> Automation >> Ansible >> Modules >> replace >> Examples

 

replace


## DOCUMENTATION

online help: https://docs.ansible.com/ansible-core/2.11/collections/ansible/builtin/replace_module.html
offline help: ansible-doc replace

## EXAMPLE 1
## PLAYBOOK

---
- name: replace module example 1
  hosts: all
  become: True
  gather_facts: no
  tasks:
  - name: (1) replace specific regexp pattern .int => _int
    replace:
      path: /home/ansible/nodes.txt
      regexp: '.int'
      replace: '_int'
  - name: Debug (1)
    shell: "cat /home/ansible/nodes.txt"
    register: filecontents
  - debug: var=filecontents.stdout_lines
  #
  - name: (2) replace all matching lines between before and after expressions
    replace:
      path: /home/ansible/nodes.txt
      after: '<DoNotUse>'
      before: '</DoNotUse>'
      regexp: '^(.+)$'
      replace: '# '
  - name: Debug (2)
    shell: "cat /home/ansible/nodes.txt"
    register: filecontents
  - debug: var=filecontents.stdout_lines
  #
  - name: (3) replace all matching lines before an expression
    replace:
      path: /home/ansible/nodes.txt
      before: '#NEW'
      regexp: '^(.+)$'
      replace: '# '
  - name: Debug (3)
    shell: "cat /home/ansible/nodes.txt"
    register: filecontents
  - debug: var=filecontents.stdout_lines
  #
  - name: (4) replace all matching lines after an expression
    replace:
      path: /home/ansible/nodes.txt
      after: '#OLD'
      regexp: '^(.+\s+)([a-z0-9]+)$'
      replace: ' old_'
  - name: Debug (4)
    shell: "cat /home/ansible/nodes.txt"
    register: filecontents
  - debug: var=filecontents.stdout_lines



## OUTPUT

PLAY [replace module example 1] ********************************************************************

TASK [(1) replace specific regexp pattern .int => _int] ********************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (1)] ***********************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "filecontents.stdout_lines": [
        "192.168.0.207   ubuntu20",
        "192.168.0.208   opensuse15",
        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",

        "<DoNotUse>",
        "192.168.0.109   oraclelinux7",
        "192.168.0.110   centos8",
        "</DoNotUse>",
        "#OLD",
        "192.168.0.152   mgmt7",
        "192.168.0.153   mgmt8"
    ]
}
ok: [ubuntu20_int] => {
    "filecontents.stdout_lines": [
        "192.168.0.207   ubuntu20",
        "192.168.0.208   opensuse15",
        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",

        "<DoNotUse>",
        "192.168.0.109   oraclelinux7",
        "192.168.0.110   centos8",
        "</DoNotUse>",
        "#OLD",
        "192.168.0.152   mgmt7",
        "192.168.0.153   mgmt8"
    ]
}

TASK [(2) replace all matching lines between before and after expressions] *************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (2)] ***********************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "filecontents.stdout_lines": [
        "192.168.0.207   ubuntu20",
        "192.168.0.208   opensuse15",
        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",
        "<DoNotUse>",
        "# 192.168.0.109   oraclelinux7",
        "# 192.168.0.110   centos8",

        "</DoNotUse>",
        "#OLD",
        "192.168.0.152   mgmt7",
        "192.168.0.153   mgmt8"
    ]
}
ok: [ubuntu20_int] => {
    "filecontents.stdout_lines": [
        "192.168.0.207   ubuntu20",
        "192.168.0.208   opensuse15",
        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",
        "<DoNotUse>",
        "# 192.168.0.109   oraclelinux7",
        "# 192.168.0.110   centos8",

        "</DoNotUse>",
        "#OLD",
        "192.168.0.152   mgmt7",
        "192.168.0.153   mgmt8"
    ]
}

TASK [(3) replace all matching lines before an expression] *****************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (3)] ***********************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "filecontents.stdout_lines": [
        "# 192.168.0.207   ubuntu20",
        "# 192.168.0.208   opensuse15",

        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",
        "<DoNotUse>",
        "# 192.168.0.109   oraclelinux7",
        "# 192.168.0.110   centos8",
        "</DoNotUse>",
        "#OLD",
        "192.168.0.152   mgmt7",
        "192.168.0.153   mgmt8"
    ]
}
ok: [ubuntu20_int] => {
    "filecontents.stdout_lines": [
        "# 192.168.0.207   ubuntu20",
        "# 192.168.0.208   opensuse15",

        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",
        "<DoNotUse>",
        "# 192.168.0.109   oraclelinux7",
        "# 192.168.0.110   centos8",
        "</DoNotUse>",
        "#OLD",
        "192.168.0.152   mgmt7",
        "192.168.0.153   mgmt8"
    ]
}

TASK [(4) replace all matching lines after an expression] ******************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [Debug (4)] ***********************************************************************************
changed: [ubuntu20_int]
changed: [debian11_int]

TASK [debug] ***************************************************************************************
ok: [debian11_int] => {
    "filecontents.stdout_lines": [
        "# 192.168.0.207   ubuntu20",
        "# 192.168.0.208   opensuse15",
        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",
        "<DoNotUse>",
        "# 192.168.0.109   oraclelinux7",
        "# 192.168.0.110   centos8",
        "</DoNotUse>",
        "#OLD",
        "192.168.0.152    old_mgmt7",
        "192.168.0.153    old_mgmt8"

    ]
}
ok: [ubuntu20_int] => {
    "filecontents.stdout_lines": [
        "# 192.168.0.207   ubuntu20",
        "# 192.168.0.208   opensuse15",
        "#NEW",
        "192.168.0.101   debian11    debian11_int",
        "192.168.0.102   mgmt7       mgmt7_int",
        "192.168.0.103   mgmt8       mgmt8_int",
        "192.168.0.104   rhel6       rhel6_int",
        "192.168.0.105   rhel7       rhel7_int",
        "192.168.0.106   rhel8       rhel8_int",
        "192.168.0.107   ubuntu20    ubuntu20_int",
        "192.168.0.108   opensuse15  opensuse15_int",
        "<DoNotUse>",
        "# 192.168.0.109   oraclelinux7",
        "# 192.168.0.110   centos8",
        "</DoNotUse>",
        "#OLD",
        "192.168.0.152    old_mgmt7",
        "192.168.0.153    old_mgmt8"

    ]
}

PLAY RECAP *****************************************************************************************
debian11_int               : ok=12   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ubuntu20_int               : ok=12   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0