rp346
rp346

Reputation: 7028

How to manipulate ansible facts?

I was trying to figure out f there is a way to manipulate ansible facts, for example if ansible_default_ipv4.address = 10.100.1.7 then assign 10.253.1.7 to some variable in playbook.

So basically I just want to replace second octate of ansible_default_ipv4.address from 100 to 253.

Upvotes: 1

Views: 513

Answers (2)

lsambo
lsambo

Reputation: 299

I can help as regard getting the value of a nested element in Ansible 2.5.5 (I use the nameserver as example). You can use:

"{{ansible_dns.nameservers[1]}}"

You can then replace the 1 digit by using item integer in loops or simila.

Upvotes: 0

helloV
helloV

Reputation: 52393

I agree with @ydaetskcoR. You should show what you have tried so far. Since this is tricky, I am providing a solution. There may be a better solution, but this one works.

  vars:
    myip: 10.100.1.7

  tasks:
  - set_fact: newip="{{ myip | regex_replace('^(\d+)\.100.(\d+\.\d+)$', '\\1.253.\\2') }}"
  - debug: var=newip

Upvotes: 1

Related Questions