Find word and IP with regexp

Viewed 35

I am working with some file with lines:

Server=127.0.0.1

ServerActive=111.111.111.111

I have a Ansible task:

---

- name: Ansible multiple replace data in file {{ path_agentd_conf }}
  replace:
    path: "{{ item.path }}"
    regexp: "{{ item.regexp }}"
    replace: "{{ item.replace }}"
  with_items:
    - {path: "{{ path_agentd_conf }}", regexp: "Server=127.0.0.1", replace: "Server={{ ip }}"}
    - {path: "{{ path_agentd_conf }}", regexp: "ServerActive=111.111.111.111", replace: "ServerActive={{ ip }}"}
    - {path: "{{ path_agentd_conf }}", regexp: "Hostname=Zabbix server", replace: "Hostname={{ ansible_hostname }}"}
    - {path: "{{ path_agentd_conf }}", regexp: "# HostMetadata=", replace: "HostMetadata=73dc46da7abd5987469855b99ae5ca5b5be"}

I tried to find in file line with Server=127.0.0.1 and ServerActive=111.111.111.111 and after that replace it to just other IP, to ServerActive=222.222.222.222

I am using this regexp: This regexp looking for all numbers of Ips.

  with_items:
    - {path: "{{ path_agentd_conf }}", regexp: '"^Server=\d[0-9]{1,3}\.\d[0-9]{1,3}\.\d[0-9]{1,3}.\d[0-9]{1,3}$/g"', replace: "Server={{ ip }}"}
    - {path: "{{ path_agentd_conf }}", regexp: '"^ServerActive=\d[0-9]{1,3}\.\d[0-9]{1,3}\.\d[0-9]{1,3}.\d[0-9]{1,3}$/g"', replace: "ServerActive={{ ip }}"}

I did not have errors, but also did not changes. Ip also can be different. Could you please help with advise, how I can use regexp to find and replace those lines?

0 Answers
Related