我正在使用Ansible 2.7.9
,并且在lineinfile模块中的正则表达式定义上遇到了麻烦。我的文件包含以下行:

host.example.com = /path/to/a/directory


我想删除该行。由于{{stocker_hostname}}包含点,因此我尝试使用replace避开这些点。我的剧本中的任务如下所示:

- name: Remove LE webroot definition
    lineinfile: 
      path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
      regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
      state: absent


执行剧本时,发生以下错误:

[...]
The offending line appears to be:
      path: "etc/path/to/config/{{ inventory_hostname }}.conf"
      regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
                                                      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:
[...]


如您所见,我正在使用双引号,并且我发现的所有示例都在使用此语法。我在这里做错了什么?感谢您的帮助。

#1 楼

这个问题似乎已经解决,如果我也可以在正则表达式中转义反斜杠:

- name: Remove LE webroot definition
    lineinfile: 
      path: "/etc/path/to/config/{{ inventory_hostname }}.conf"
      regexp: "^{{ inventory_hostname | replace('.', '\.') }} = /path/to/a/directory"
      state: absent