Sebastian
Sebastian

Reputation: 75

Edit IPs with Ansible

Is it possible to customize a IP adress with Ansible. For example when the remote system IP is 127.34.34.21 the DNS IP is 127.34.34.1

The DNS IP is always the system IP but the last number is 1. Is it possible that Ansible can do this dynamical?

Look up the system IP > Customize it > set the new IP as DNS IP.

Upvotes: 2

Views: 130

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Q: "Is it possible to customize an IP address with Ansible?"

A: Yes. It is. There are many options.

  • Use split method and join filter. For example
    - set_fact:
        dns: "{{ ip.split('.')[:3]|join('.') ~ '.1' }}"
      vars:
        ip: 127.34.34.21
    - debug:
        var: dns

give

  dns: 127.34.34.1
  • It's possible to use the splitext filter instead of split/join. For example, the task below gives the same result
    - set_fact:
        dns: "{{ (ip|splitext)[0] ~ '.1' }}"
  • Next option is ipaddr filter. For example, the task below gives the same result
    - set_fact:
        dns: "{{ (ip ~ '/24')|ipaddr('first_usable') }}"

Q: "Is it possible that Ansible can do this dynamical?"

A: Yes. It is. You'll have to decide which IP address to use. For example, use ansible_default_ipv4.address

    - debug:
        var: ansible_default_ipv4.address
    - set_fact:
        dns: "{{ (ip ~ '/24')|ipaddr('first_usable') }}"
      vars:
        ip: "{{ ansible_default_ipv4.address }}"
    - debug:
        var: dns

give (abridged)

  ansible_default_ipv4.address: 10.1.0.27
  dns: 10.1.0.1

See setup. See other attributes of the variable ansible_default_ipv4. See variable ansible_all_ipv4_addresses.

Upvotes: 2

Related Questions