簡體   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