- Install python3-pip and Python's netaddr library, since we will be using the Ansible IP filter, which requires Python's netaddr library:
# On ubuntu
$ sudo apt-get install python3-pip
# On CentOS
$ sudo yum install python3-pip
$ pip3 install netaddr
- Create a new Ansible playbook called ansible_filters.yml, as shown here:
---
- name: Ansible Filters
hosts: csr1
vars:
interfaces:
- { port: FastEthernet0/0, prefix: 10.1.1.0/24 }
- { port: FastEthernet1/0, prefix: 10.1.2.0/24 }
tasks:
- name: Generate Interface Config
blockinfile:
block: |
hostname {{ hostname | upper }}
{% for intf in interfaces %}
!
interface {{ intf.port }}
ip address {{intf.prefix | ipv4(1) | ipv4('address') }} {{intf.prefix | ipv4('netmask') }}
!
{% endfor %}
dest: "configs/csr1_interfaces.cfg"
create: yes
delegate_to: localhost