代码之家  ›  专栏  ›  技术社区  ›  izidor

如何确保配置只有一行

  •  0
  • izidor  · 技术社区  · 2 年前

    我的剧本有以下任务来设置静态IPv6地址:

    - name: Configure stable IPv6
      lineinfile:
        path: /etc/dhcpcd.conf
        regexp: '^static ipv6_address='
        line: "static ip6_address=2001::2222/64"
        state: present
      notify: Restart dhcpcd
    

    我的服务器中包含以下内容 /etc/dhcpcd.conf 文件:

    # fallback to static profile on eth0
    #interface eth0
    #fallback static_eth0
    static ip6_address=2001::0000/64
    static ip6_address=2001::1111/64
    

    当我运行剧本时,ansible添加了一行,而不是替换现有的两行:

    # fallback to static profile on eth0
    #interface eth0
    #fallback static_eth0
    static ip6_address=2001::0000/64
    static ip6_address=2001::1111/64
    static ip6_address=2001::2222/64
    

    我怎样才能只设置一个IP地址?我希望我的配置看起来像:

    # fallback to static profile on eth0
    #interface eth0
    #fallback static_eth0
    static ip6_address=2001::2222/64
    

    解决方案应:

    • 如果配置中没有行,则添加一行
    • 删除所有匹配的行(如果存在),并用新的单行替换它们
    0 回复  |  直到 2 年前
        1
  •  1
  •   U880D    2 年前

    如何确保配置只有一行。。。如果配置中不存在行,则解决方案应添加一行, 全部删除。。。线并用一条新的单线替换它们

    根据您目前的描述,似乎绝对不需要任何复杂的CHECK或IF THEN ELSE ELSEIF或类似的方法。只需使用 template module copy with Parameter content 以在配置文件中提供所需状态。

    如果那一行 static ip6_address=2001::2222/64 应该是最终状态,只需声明即可

    ---
    - hosts: localhost
      become: false
      gather_facts: false
    
      tasks:
    
      - copy:
          content: 'static ip6_address=2001::2222/64'
          dest: dhcpcd.conf
    

    将生成请求的输出。

        2
  •  0
  •   Vladimir Botka    2 年前

    给定要测试的文件

    shell> cat /tmp/ansible/test/dhcpcd.conf 
    # fallback to static profile on eth0
    #interface eth0
    #fallback static_eth0
    static ip6_address=2001::0000/64
    static ip6_address=2001::1111/64
    

    将配置数据放入列表

        dhcpcd_conf:
          - {key: static ip6_address, value: 2001::2222/64}
    

    删除重复的参数,但删除第一个参数

        - name: "Remove duplicate parameters from dhcpcd.conf"
          ansible.builtin.replace:
            dest: /tmp/ansible/test/dhcpcd.conf
            after: '{{ item.key }}(\s|=)'
            regexp: '^\s*{{ item.key }}(\s|=).*$'
          loop: "{{ dhcpcd_conf }}"
    

    gives(使用选项--check--diff运行)

    --- before: /tmp/ansible/test/dhcpcd.conf
    +++ after: /tmp/ansible/test/dhcpcd.conf
    @@ -2,4 +2,4 @@
     #interface eth0
     #fallback static_eth0
     static ip6_address=2001::0000/64
    -static ip6_address=2001::1111/64
    

    如果你想保留最后一场比赛 之后 具有 之前

        - name: "Remove duplicate parameters from dhcpcd.conf"
          ansible.builtin.replace:
            dest: /tmp/ansible/test/dhcpcd.conf
            before: '{{ item.key }}(\s|=)'
            regexp: '^\s*{{ item.key }}(\s|=).*$'
          loop: "{{ dhcpcd_conf }}"
    

    配置文件

        - name: "Configure dhcpcd.conf"
          ansible.builtin.lineinfile:
            dest: /tmp/ansible/test/dhcpcd.conf
            regexp: '^\s*{{ item.key }}(\s|=).*$'
            line: "{{ item.key }}={{ item.value }}"
          loop: "{{ dhcpcd_conf }}"
    

    gives(使用选项--check--diff运行)

    --- before: /tmp/ansible/test/dhcpcd.conf (content)
    +++ after: /tmp/ansible/test/dhcpcd.conf (content)
    @@ -1,5 +1,5 @@
     # fallback to static profile on eth0
     #interface eth0
     #fallback static_eth0
    -static ip6_address=2001::0000/64
    +static ip6_address=2001::2222/64
    

    完整的测试剧本示例

    - hosts: localhost
    
      vars:
    
        dhcpcd_conf:
          - {key: static ip6_address, value: 2001::2222/64}
    
      tasks:
    
        - name: "Remove duplicate parameters from dhcpcd.conf"
          ansible.builtin.replace:
            dest: /tmp/ansible/test/dhcpcd.conf
            after: '{{ item.key }}(\s|=)'
            regexp: '^\s*{{ item.key }}(\s|=).*$'
          loop: "{{ dhcpcd_conf }}"
          tags: duplicates
    
        - name: "Configure dhcpcd.conf"
          ansible.builtin.lineinfile:
            dest: /tmp/ansible/test/dhcpcd.conf
            regexp: '^\s*{{ item.key }}(\s|=).*$'
            line: "{{ item.key }}={{ item.value }}"
          loop: "{{ dhcpcd_conf }}"
          tags: configure
    

    结果

    shell> cat /tmp/ansible/test/dhcpcd.conf 
    # fallback to static profile on eth0
    #interface eth0
    #fallback static_eth0
    static ip6_address=2001::2222/64