ansible 2.10 / lineinfile: regexp: "{{netw}}" 变量未溶解

问题描述 投票:0回答:1

`

- name: read network
  hosts: localhost
  vars:
    CUSTOMER: "{{ CUST | default("me") }}"
    list: " {{ lookup('file', 'networks').split('\n')[0] }} "
    gway: " {{ list.split(' ')[2] }} "
    netw: " {{ list.split(' ')[1] }} "
  tasks:
    - name: test1
      lineinfile:
        path: 'networks'
        state: absent
        regexp: "{{netw}}"
    - debug:
        var: gway
    - debug:
        var: netw

输出中打印的变量 netw 正确:我的网络文件有网络和网关示例: 172.16.25.0/29 172.16.25.1

但不从文件中删除该行。 如果我写这个硬编码,它就可以工作:regexp:“172.16.25.1”

variables ansible
1个回答
0
投票

正确的代码是这样的:

- name: read network
  hosts: localhost
  vars:
    list: "{{lookup('file', 'networks').split('\n')[0]}}"
    gway: "{{list.split(',')[2]}}"
    netw: "{{list.split(',')[1]}}"
    line_to_remove: "{{netw}}"
  tasks:
    - name: test1
      lineinfile:
        path: "networks"
        state: absent
        regexp: '{{ line_to_remove | regex_escape() }}'
    - debug:
        msg: "{{ line_to_remove }}"
© www.soinside.com 2019 - 2024. All rights reserved.