[英]How to update new hostname in a file using ansible?
I am using below code to replace old hostname with new one, it is working except for hostnames starting with numbers.(OLD_HOSTNAME and NEW_HOSTNAME are vars
)我正在使用以下代码将旧主机名替换为新主机名,除了以数字开头的主机名外,它可以正常工作。(OLD_HOSTNAME 和 NEW_HOSTNAME 是vars
)
tasks:
- name: "Updating file"
replace:
name: /tmp/interfaces
backup: yes
regexp: '(\s+){{ OLD_HOSTNAME }}(\s+)'
replace: '\1{{ NEW_HOSTNAME }}\2'
If I replace \\1 with \\g<1> , the hostnames starting with numbers will also get placed.如果我用\\g<1>替换\\1 ,也会放置以数字开头的主机名。 But as per the ansible doc , \\1 is used ambiguously
, and \\g<1> used explicitly
.但根据 ansible doc , \\1 is used ambiguously
,而\\g<1> used explicitly
。
Question: Will this change impact any other format of hostname?问题:此更改会影响任何其他格式的主机名吗?
No, using the explicit form will not affect other hostname formats.不,使用显式形式不会影响其他主机名格式。
The reason why you have a problem when NEW_HOSTNAME
begins with a number is that the replace
string would become something like \\123-server\\2
if NEW_HOSTNAME
was 23-server
and there is no backreference \\123
.当NEW_HOSTNAME
以数字开头时出现问题的原因是,如果NEW_HOSTNAME
是23-server
并且没有反向引用\\123
,则replace
字符串将变成类似\\123-server\\2
。 Using the explicit form preserves your original intent.使用显式形式可以保留您的原始意图。 In my example, replace
would become \\g<1>23-server\\g<2>
.在我的示例中, replace
将变为\\g<1>23-server\\g<2>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.