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-01
ios-xe-02

[hosts:vars]
ansible_network_os=ios
ansible_user=developer
ansible_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

 


Let's verify the IP helper address on devices
ios-xe-01#show ip helper-address 
Interface                  Helper-Address  VPN     VRG Name        VRG State
Loopback30                 10.200.200.10   0       None            Unknown
                           10.200.200.20   0       None            Unknown
ios-xe-02#show ip helper-address 
Interface                  Helper-Address  VPN     VRG Name        VRG State
Loopback25                 10.200.200.10   0       None            Unknown
                           10.200.200.20   0       None            Unknown
Loopback35                 10.200.200.10   0       None            Unknown
                           10.200.200.20   0       None            Unknown

Comments

Popular posts from this blog

Specifying SSH port in Ansible Inventory

Ansible-Playbook to display output of multiple show commands (using stdout_lines with Loop)

Filtering Routes in BGP using Route-maps and Prefix-list

Ansible Playbook for Network OS Upgrade with pre and post checks

VMware NSX Traffic Flow — East-West & North-South

Bypassing Proxy Server in Google Chrome

Export or Backup Azure Network Security Groups into CSV using PowerShell

Ansible-playbook for backing up running config of Cisco IOS