【问题标题】:Ansible regex to search and replace an IP Address in a lineAnsible 正则表达式在一行中搜索和替换 IP 地址
【发布时间】:2020-10-30 05:45:33
【问题描述】:

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

如果我必须使用定义的一组 IP 地址更改整个 IP 地址行,我有下面的播放绝对可以正常工作,但我正在寻找特定的 IP 地址,即要查看的191.168.1.4,如果找到,然后只替换它并保留其余 IP。

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

【问题讨论】:

  • @Zeitounator,我会检查一下。
  • @任何建议。

标签: regex linux ansible yaml


【解决方案1】:

这是一个完整的 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

【讨论】:

  • 感谢@Zeitounator +1,我正在检查解决方案,这听起来非常好。我会带着结果回来的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 2015-11-22
  • 2019-09-29
  • 2018-11-10
  • 1970-01-01
相关资源
最近更新 更多