prado
prado

Reputation: 129

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)

  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. But as per the ansible doc, \1 is used ambiguously, and \g<1> used explicitly.

Question: Will this change impact any other format of hostname?

Upvotes: 0

Views: 316

Answers (1)

JR.
JR.

Reputation: 304

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. Using the explicit form preserves your original intent. In my example, replace would become \g<1>23-server\g<2>.

Upvotes: 1

Related Questions