├── README.md ├── ipaddr_test.retry ├── ipaddr_test.yml ├── rendered ├── rh-ansible-automation.png └── template.j2 /README.md: -------------------------------------------------------------------------------- 1 | # ipaddr filter examples 2 | The [ipaddr filter](http://docs.ansible.com/ansible/latest/playbooks_filters_ipaddr.html) is a Jinja2 filter designed to provide an interface to [netaddr](https://pypi.python.org/pypi/netaddr). This github repo provides examples with the Ansible [template module](http://docs.ansible.com/ansible/latest/template_module.html) to augment the [ipaddr documentation](http://docs.ansible.com/ansible/latest/playbooks_filters_ipaddr.html). 3 | 4 | # The Playbook 5 | ```yaml 6 | --- 7 | - hosts: localhost 8 | connection: local 9 | gather_facts: false 10 | vars: 11 | sean_subnet: "192.168.1.0/24" 12 | tasks: 13 | - template: 14 | src: ./template.j2 15 | dest: ./rendered 16 | ``` 17 | 18 | # Running the playbook 19 | Use the ansible-playbook command to run the included playbook. 20 | 21 | `[sean@rhel7]$ ansible-playbook ipaddr_test.yml` 22 | 23 | The playbook will create a local file called `rendered` which renders the included `template.j2` which includes examples with the ipaddr filter. 24 | 25 | # View the playbook 26 | [Click here to view the playbook](ipaddr_test.yml) 27 | 28 | # View the rendered template 29 | The rendered config will provider numerous examples. For example: 30 | ``` 31 | Jinja2: What is the range of usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('range_usable') }} 32 | Renders: What is the range of usable IP address on 192.168.1.0/24? 192.168.1.1-192.168.1.254 33 | ``` 34 | 35 | [Click here to view the rendered template](rendered) 36 | 37 | --- 38 | ![Red Hat Ansible Automation](rh-ansible-automation.png) 39 | 40 | Red Hat® Ansible® Automation includes three products: 41 | 42 | - [Red Hat® Ansible® Engine](https://www.ansible.com/ansible-engine): a fully supported product built on the foundational capabilities of the Ansible project. 43 | 44 | - [Red Hat® Ansible® Networking Add-On](https://www.ansible.com/ansible-engine): provides support for select networking modules from Arista (EOS), Cisco (IOS, IOS XR, NX-OS), Juniper (Junos OS), Open vSwitch, and VyOS. 45 | 46 | - [Red Hat® Ansible® Tower](https://www.ansible.com/tower): makes it easy to scale automation, manage complex deployments and speed productivity. Extend the power of Ansible with workflows to streamline jobs and simple tools to share solutions with your team. 47 | 48 | Want more info? 49 | [Read this blog post for more info about Engine, the networking add-on and Tower](https://www.ansible.com/blog/red-hat-ansible-automation-engine-vs-tower) 50 | -------------------------------------------------------------------------------- /ipaddr_test.retry: -------------------------------------------------------------------------------- 1 | localhost 2 | -------------------------------------------------------------------------------- /ipaddr_test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | connection: local 4 | gather_facts: false 5 | vars: 6 | sean_subnet: "192.168.1.0/24" 7 | tasks: 8 | - template: 9 | src: ./template.j2 10 | dest: ./rendered 11 | -------------------------------------------------------------------------------- /rendered: -------------------------------------------------------------------------------- 1 | ipaddr filter examples: 2 | The var sean_subnet is: 192.168.1.0/24 3 | 4 | Jinja2: IP Address: {{sean_subnet | ipaddr }} Renders: IP Address: 192.168.1.0/24 5 | 6 | Jinja2: IP Address: {{sean_subnet | ipaddr('netmask') }} Renders: IP Address: 255.255.255.0 7 | 8 | Jinja2: IPv4 Address: {{ sean_subnet | ipv4 }} Renders: IPv4 Address: 192.168.1.0/24 9 | 10 | Jinja2: IPv6 Address: {{ sean_subnet | ipv6 }} Renders: IPv6 Address: False 11 | 12 | Jinja2: Network: {{ sean_subnet | ipaddr('network') }} Renders: Network: 192.168.1.0 13 | 14 | Jinja2: Prefix: {{ sean_subnet | ipaddr('prefix') }} Renders: Prefix: 24 15 | 16 | Jinja2: What is the first usable IP on {{sean_subnet}}? {{ sean_subnet | ipaddr('next_usable') }} Renders: What is the first usable IP on 192.168.1.0/24? 192.168.1.1 17 | 18 | Jinja2: What is the range of usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('range_usable') }} Renders: What is the range of usable IP address on 192.168.1.0/24? 192.168.1.1-192.168.1.254 19 | 20 | Jinja2: What is the last usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('last_usable') }} Renders: What is the last usable IP address on 192.168.1.0/24? 192.168.1.254 21 | 22 | Jinja2: What is the 2nd usable address on {{sean_subnet}}? {{ sean_subnet | next_nth_usable(2) }} Renders: What is the 2nd usable address on 192.168.1.0/24? 192.168.1.2 23 | -------- 24 | Loop method for providing multiple IP addresses for use: 25 | 26 | Jinja: 27 | {% for n in range(10) %} 28 | {{ sean_subnet | next_nth_usable(n) }} 29 | {% endfor %} 30 | Renders: 31 | 32 | 192.168.1.1 33 | 192.168.1.2 34 | 192.168.1.3 35 | 192.168.1.4 36 | 192.168.1.5 37 | 192.168.1.6 38 | 192.168.1.7 39 | 192.168.1.8 40 | 192.168.1.9 41 | -------------------------------------------------------------------------------- /rh-ansible-automation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/network-automation/ipaddr_filter/cc9d2761795f28f45af2225290d32aac5539eb0b/rh-ansible-automation.png -------------------------------------------------------------------------------- /template.j2: -------------------------------------------------------------------------------- 1 | ipaddr filter examples: 2 | The var sean_subnet is: {{ sean_subnet }} 3 | 4 | Jinja2: {% raw %} IP Address: {{sean_subnet | ipaddr }} {% endraw %} 5 | Renders: IP Address: {{ sean_subnet | ipaddr }} 6 | 7 | Jinja2: {% raw %} IP Address: {{sean_subnet | ipaddr('netmask') }} {% endraw %} 8 | Renders: IP Address: {{ sean_subnet | ipaddr('netmask') }} 9 | 10 | Jinja2: {% raw %} IPv4 Address: {{ sean_subnet | ipv4 }} {% endraw %} 11 | Renders: IPv4 Address: {{ sean_subnet | ipv4 }} 12 | 13 | Jinja2: {% raw %} IPv6 Address: {{ sean_subnet | ipv6 }} {% endraw %} 14 | Renders: IPv6 Address: {{ sean_subnet | ipv6 }} 15 | 16 | Jinja2: {% raw %} Network: {{ sean_subnet | ipaddr('network') }} {% endraw %} 17 | Renders: Network: {{ sean_subnet | ipaddr('network') }} 18 | 19 | Jinja2: {% raw %} Prefix: {{ sean_subnet | ipaddr('prefix') }} {% endraw %} 20 | Renders: Prefix: {{ sean_subnet | ipaddr('prefix') }} 21 | 22 | Jinja2: {% raw %} What is the first usable IP on {{sean_subnet}}? {{ sean_subnet | ipaddr('next_usable') }} {% endraw %} 23 | Renders: What is the first usable IP on {{sean_subnet}}? {{ sean_subnet | ipaddr('next_usable') }} 24 | 25 | Jinja2: {% raw %} What is the range of usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('range_usable') }} {% endraw %} 26 | Renders: What is the range of usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('range_usable') }} 27 | 28 | Jinja2: {% raw %} What is the last usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('last_usable') }} {% endraw %} 29 | Renders: What is the last usable IP address on {{sean_subnet}}? {{ sean_subnet | ipaddr('last_usable') }} 30 | 31 | Jinja2: {% raw %} What is the 2nd usable address on {{sean_subnet}}? {{ sean_subnet | next_nth_usable(2) }} {% endraw %} 32 | Renders: What is the 2nd usable address on {{sean_subnet}}? {{ sean_subnet | next_nth_usable(2) }} 33 | -------- 34 | Loop method for providing multiple IP addresses for use: 35 | 36 | Jinja: {% raw %} 37 | {% for n in range(10) %} 38 | {{ sean_subnet | next_nth_usable(n) }} 39 | {% endfor %} {% endraw %} 40 | 41 | Renders: 42 | {% for n in range(10) %} 43 | {{ sean_subnet | next_nth_usable(n) }} 44 | {% endfor %} 45 | --------------------------------------------------------------------------------