【发布时间】:2022-01-05 18:33:26
【问题描述】:
我从一个剧本中获得了这个 Ansible 任务,我试图用它来用 /etc/ansible/hosts 中设置为变量的新主机名替换 /etc/hosts 中出现的主机名:
- name: Update /etc/hosts
replace:
path: /etc/hosts
regexp: '^\w+-\w+'
replace: "{{ new_hostname }}"
我试图更改 /etc/hosts 内容的 Ansible 目标的主机名是 webna-host0011,这也是我试图在 /etc/hosts 中捕获的模式和替换所有出现的。
当我运行 playbook 时,此任务没有找到任何匹配项来进行替换,并且 /etc/hosts 文件保持不变。
我使用 https://pythex.org/ 和 webna-host0011 作为我的测试字符串检查了正则表达式,并且能够获得正确的匹配,但是在任务本身中使用此表达式时找不到匹配项。
这是 /etc/hosts 本身:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
10.12.52.19 webna-host0011.onegrp.com webna-host0011
正则表达式匹配主机名本身,但似乎无法进行匹配,因为它位于文件本身的行中。
【问题讨论】:
-
我觉得应该是
regexp: '^\\w+-\\w+',但可能还不够。 -
没有规则来定义你搜索的内容,你必须指明要替换的全名