0

Really simple thing I want to do in Ansible ... or so you would think.

All I want to do is to set a variable [which I later use in a j2 template] to the value of ansible_default_ipv4.network, with the last byte chopped off and third incremented.

so, if ansible_default_ipv4.network is 192.168.10.0, I want to the new variable set to 192.168.11.

Here's how I presently do it:

- set_fact:
   x: "{{ ansible_default_ipv4.network.split('.')[0:3] }}"
- set_fact:
   x2: "{{ x.2 | int  + 1 }}"
- set_fact:
   x3: "{{x.0}}.{{x.1}}.{{x2}}"

Works fine, but is as ugly as a box or frogs :(

Is there a cleaner way of doing this?

Thanks!

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • 1
    There is an [ipaddr filter](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters_ipaddr.html) which can be used to manipulate things involving IP address, netmask and the likes. This may be of interest to you. – seshadri_c Oct 31 '20 at 11:57
  • This will produce 256. – ceving Oct 31 '20 at 13:24

1 Answers1

1

Try this

    - set_fact:
        x: "{{ ansible_default_ipv4.network|ipmath(256)|splitext|first }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63