device,interface,description,ip_address R1,GigabitEthernet1,MGMT_ACCESS,10.0.0.103/24 R1,GigabitEthernet2,MANAGEMENT,192.168.10.1/24 R1,GigabitEthernet3,ENGINEERING,192.168.20.1/24 R2,GigabitEthernet1,MGMT_ACCESS,10.0.0.104/24 R2,GigabitEthernet2,MANAGEMENT,192.168.10.2/24 R2,GigabitEthernet3,ENGINEERING,192.168.20.2/24 R3,GigabitEthernet1,MGMT_ACCESS,10.0.0.105/24 R3,GigabitEthernet2,MANAGEMENT,192.168.10.3/24 R3,GigabitEthernet3,ENGINEERING,192.168.20.3/24 all: hosts: R1: ansible_host: 10.0.0.103 R2: ansible_host: 10.0.0.104 R3: ansible_host: 10.0.0.105 vars: ansible_network_os: ios ansible_connection: network_cli ansible_user: cisco ansible_password: cisco ansible_host_key_checking: false --- - name: Manage Router Interfaces as Code hosts: all gather_facts: no tasks: - name: Read interface definitions from CSV read_csv: path: interfaces.csv delegate_to: localhost run_once: true register: interface_data - name: Enable interfaces and set descriptions cisco.ios.ios_interfaces: config: - name: "{{ item.interface }}" description: "{{ item.description }}" enabled: true state: merged loop: "{{ interface_data.list | selectattr('device', 'equalto', inventory_hostname) | list }}" - name: Build L3 interface configuration list set_fact: l3_config: "{{ l3_config | default([]) + [{'name': item.interface, 'ipv4': [{'address': item.ip_address}]}] }}" loop: "{{ interface_data.list | selectattr('device', 'equalto', inventory_hostname) | list }}" - name: Configure IP addresses on interfaces cisco.ios.ios_l3_interfaces: config: "{{ l3_config }}" state: merged