繁体   English   中英

Ansible 正则表达式搜索并替换一行中的 IP 地址

[英]Ansible regex to search and replace an IP Address in a line

我有一个 Ansible 剧本,我需要在一行中更改Server Name ,并在另一行中更改Specific IP address ,因为在这一行中有一堆 IP 地址由逗号分隔。

I have the below play which works absolutely fine in case I have to change the entire IP address line with the defined set of IP addresses, But I'm looking for specific IP address ie 191.168.1.4 to be looked upon and if found then just仅替换它并保留 IP 的 rest 原样。

---
- name: Playbook to replace line in zabbix_agentd.conf
  hosts: all
  #remote_user: root
  gather_facts: False

  tasks:
  - name: Changing the zabbix-agent configuration on the client
    lineinfile:
      path: /tmp/zabbix_agentd.conf
      ### line to be searched & matched
      regexp: '{{ item.From }}'
      ### new line to be replaced with the old matched one
      line: '{{ item.To }}'
      state: present
      backup: yes
      backrefs: yes

    with_items:
    - { From: '#ServerActive=myzabbix1.example.com', To: 'ServerActive=myzabbix2.example.com'}
    - { From: 'Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5', To: 'Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.10,192.168.1.5'}
    # Actions will be triggered at the end of each block of task and notifies a handler.
    notify: restart_zabbix_service

  handlers:
  - name: restart_zabbix_service
    # referenced by a globally unique name and are notified by notifiers.
    service:
      name: zabbix-agentd
      state: restarted

这是一个完整的 MCVE,可让您走上正轨。 根据您的上述内容:

---
- name: Replace demo
  hosts: localhost
  gather_facts: false

  vars:
    demo_file_content: |-
      #ServerActive=myzabbix1.example.com
      Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5
      SomeOtherTestForDemo: toto 192.168.1.4 pipo bingo
    demo_file_destination: /tmp/replace_demo.conf

  tasks:
    - name: Start our test from scratch with a fresh file
      copy:
        dest: "{{ demo_file_destination }}"
        content: "{{ demo_file_content }}"

    - name: Check file content before demo
      slurp:
        path: "{{ demo_file_destination }}"
      register: demo_start
    - debug:
        msg: "{{ (demo_start.content | b64decode).split('\n') }}"

    - name: Replace elements in file
      replace:
        path: "{{ demo_file_destination }}"
        regexp: "{{ item.regexp }}"
        replace: "{{ item.replace }}"
      loop:
        # Replace active server and remove comment if exists
        - regexp: "^#?(ServerActive=)myzabbix1.example.com$"
          replace: "\\g<1>myzabbix2.example.com"
        # Replace all occurences of a specific IP anywhere
        - regexp: "^(.*)192\\.168\\.1\\.4(.*)$"
          replace: "\\g<1>192.168.1.10\\g<2>"

    - name: Check file content after demo
      slurp:
        path: "{{ demo_file_destination }}"
      register: demo_end
    - debug:
        msg: "{{ (demo_end.content | b64decode).split('\n') }}"


    - name: Cleanup (unless you pass `-e keep_demo_file=true` to ansible-playbook)
      file:
        path: "{{ demo_file_destination }}"
        state: absent
      when: not keep_demo_file | default(false) | bool

这使:

$ ansible-playbook test.yml

PLAY [Replace demo] ***************************************************************************************

TASK [Start our test from scratch with a fresh file] ******************************************************
changed: [localhost]

TASK [Check file content before demo] *********************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************************
ok: [localhost] => {
    "msg": [
        "#ServerActive=myzabbix1.example.com",
        "Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5",
        "SomeOtherTestForDemo: toto 192.168.1.4 pipo bingo"
    ]
}

TASK [Replace elements in file] ***************************************************************************
changed: [localhost] => (item={'regexp': '^#?(ServerActive=)myzabbix1.example.com$', 'replace': '\\g<1>myzabbix2.example.com'})
changed: [localhost] => (item={'regexp': '^(.*)192\\.168\\.1\\.4(.*)$', 'replace': '\\g<1>192.168.1.10\\g<2>'})

TASK [Check file content after demo] **********************************************************************
ok: [localhost]

TASK [debug] **********************************************************************************************
ok: [localhost] => {
    "msg": [
        "ServerActive=myzabbix2.example.com",
        "Server=192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.10,192.168.1.5",
        "SomeOtherTestForDemo: toto 192.168.1.10 pipo bingo"
    ]
}

TASK [Cleanup (unless you pass `-e keep_demo_file=true` to ansible-playbook)] *****************************
changed: [localhost]

PLAY RECAP ************************************************************************************************
localhost                  : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM