简体   繁体   English

如何使用ansible更新文件中的新主机名?

[英]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_HOSTNAME23-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.

相关问题 Ansible - 替换文件中某些表达式的第一次出现 - 路径包括主机名 - Ansible - replace first occurrence of certain expression in file - path includes hostname 如何使用正则表达式从主机名中提取 ansible 数字中没有一个特定的 - How to extract with regexp in ansible numbers from hostname without one specific 如何使用ansible修改文件但只出现一次 - How to modify a file but only one occurrence using ansible 使用 ansible 在文件中查找带有 $ 的单词 - Finding word with $ in file using ansible 如何使用awk和正则表达式或子字符串从文件中获取主机名 - How can I get the hostname from a file using awk and regex or substring 使用 Ansible playbook 从包含 3 的行中删除给定的主机名 + URL,以逗号分隔,在任何位置 - Remove a given hostname+URL from a line containing 3, separated by commas, in any position, using Ansible playbook 在 ansible find 模块中使用换行符 - Using a new line character in the ansible find module 如何使用ansible regexp将新字符串插入telegraf.conf的inputs.ping - How to Insert a new string into telegraf.conf's inputs.ping using ansible regexp 使用Ansible替换YAML文件中的特定字符串 - Replace Specific String in YAML File using Ansible 使用 Ansible 正则表达式在文件中搜索字符串 - Search for a string in a file using Ansible regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM