Ansible to Configure DHCP IP-Helper Address on Multiple Devices
In this post we'll be configuring ip-helper address on multiple devices using Ansible. We'll be defining the interfaces to be configured for different devices in host_vars.
In ansible, host-specific variables can be defined in the host_vars sub-directory either in the home directory of user executing ansible play or in /etc/ansible. Each file/directory in the host_vars sub-directory is name after the host it represents, e.g. host variable for device router-01 are stored in either ~/host_vars/router-01 or /etc/ansible/host_vars/router-01
Define variables for different interfaces of various devices that would be configured by Ansible play.
$ mkdir host_vars$ vi host_vars/ios-xe-01.yml---dhcp_interfaces:- interface: loopback 30$ vi host_vars/ios-xe-02.yml---dhcp_interfaces:- interface: loopback 25- interface: loopback 35
Let's view the roles in tree view
$ tree host_vars/
host_vars/
├── ios-xe-01.yml
└── ios-xe-02.yml
0 directories, 2 files
Create inventory file
$ vi inv_cisco-router# Asible Inventory file for cisco XE routers[hosts]ios-xe-01ios-xe-02[hosts:vars]ansible_network_os=iosansible_user=developeransible_ssh_pass=C1sco12345
Create playbook for configuring ip-helper address
$ vi play_helper-addr.yml
---
- name: Configure ip-helper on multiple devices
hosts: all
gather_facts: no
connection: network_cli
tasks:
- ios_config:
lines:
- ip helper-address 10.200.200.10
- ip helper-address 10.200.200.20
parents: interface {{ item.interface }}
with_items: "{{ dhcp_interfaces }}"
Let's run the playbook
$ ansible-playbook play_helper-addr.yml -i inv_cisco-router
PLAY [Configure ip-helper on multiple devices] *******************************************************************************************
TASK [ios_config] ************************************************************************************************************************
changed: [ios-xe-01] => (item={'interface': 'loopback 25'})
changed: [ios-xe-02] => (item={'interface': 'loopback 35'})
changed: [ios-xe-02] => (item={'interface': 'loopback 30'})
PLAY RECAP *******************************************************************************************************************************
ios-xe-mgmt-latest.cisco.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ios-xe-mgmt.cisco.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ios-xe-01#show ip helper-addressInterface Helper-Address VPN VRG Name VRG StateLoopback30 10.200.200.10 0 None Unknown10.200.200.20 0 None Unknownios-xe-02#show ip helper-addressInterface Helper-Address VPN VRG Name VRG StateLoopback25 10.200.200.10 0 None Unknown10.200.200.20 0 None UnknownLoopback35 10.200.200.10 0 None Unknown10.200.200.20 0 None Unknown
Comments
Post a Comment