Last active
July 3, 2023 17:05
-
-
Save laidbackware/d54a5c90e6a35afee2d9e366a162f2fb to your computer and use it in GitHub Desktop.
Ansible IP manipulation cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: false | |
tasks: | |
- name: Set constants | |
ansible.builtin.set_fact: | |
cidr_source: 10.0.0.0/16 | |
cidr_divide_bits: 4 | |
cidr_divide_position: 2 | |
- name: Manipulate data | |
ansible.builtin.set_fact: | |
cidr_last_ip: |- | |
{{ cidr_source | ansible.utils.ipaddr('-2') | ansible.utils.ipv4('address') }} | |
cidr_split: |- | |
{{ cidr_source | | |
ansible.utils.ipsubnet((cidr_source.split('/')[1] |int) + cidr_divide_bits, cidr_divide_position) }} | |
cidr_split_last_ip: |- | |
{{ cidr_source | | |
ansible.utils.ipsubnet((cidr_source.split('/')[1] |int) + cidr_divide_bits, cidr_divide_position) | | |
ansible.utils.ipaddr('-2') | ansible.utils.ipv4('address') }} | |
cidr_split_first_ip: |- | |
{{ cidr_source | | |
ansible.utils.ipsubnet((cidr_source.split('/')[1] |int) + cidr_divide_bits, cidr_divide_position) | | |
ansible.utils.ipaddr('1') | ansible.utils.ipv4('address') }} | |
- ansible.builtin.debug: | |
msg: | |
- Last IP of source CIDR {{ cidr_last_ip }} | |
- Divided CIDR by 4 bits and select 3rd entry {{ cidr_split }} | |
- Divided CIDR by 4 bits and select 3rd entry and first IP of CIDR {{ cidr_split_first_ip }} | |
- Divided CIDR by 4 bits and select 3rd entry and last IP of CIDR {{ cidr_split_last_ip }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment