-–
- name: MAC-based VLAN Change with Confirmation
hosts: localhost
gather_facts: no
vars_prompt:
- name: mac_address
prompt: "Enter MAC address (format: xxxx.xxxx.xxxx)"
private: no
- name: site
prompt: "Enter Site (example: site_blr)"
private: no
- name: switch_name
prompt: "Enter Switch Name"
private: no
- name: interface_name
prompt: "Enter Interface (example: GigabitEthernet1/0/10)"
private: no
- name: vlan_id
prompt: "Enter VLAN ID to configure"
private: no
tasks:
- name: Search MAC address in selected site switches
ios_command:
commands:
- "show mac address-table | include {{ mac_address }}"
delegate_to: "{{ item }}"
loop: "{{ groups\[site\] }}"
register: mac_search_results
ignore_errors: yes
- name: Display MAC search results
debug:
msg: "{{ item.stdout_lines }}"
loop: "{{ mac_search_results.results }}"
- name: Show running config of interface
ios_command:
commands:
- "show run interface {{ interface_name }}"
delegate_to: "{{ switch_name }}"
register: run_output
- name: Display interface config
debug:
var: run_output.stdout_lines
- name: Show MAC address table for interface
ios_command:
commands:
- "show mac address-table interface {{ interface_name }}"
delegate_to: "{{ switch_name }}"
register: mac_table_output
- name: Display MAC table
debug:
var: mac_table_output.stdout_lines
- name: Confirm before change
pause:
prompt: "Proceed with VLAN change? (yes/no)"
register: user_confirm
- name: Apply VLAN change
ios_config:
parents: "interface {{ interface_name }}"
lines:
- switchport access vlan {{ vlan_id }}
delegate_to: "{{ switch_name }}"
when: user_confirm.user_input == "yes"
- name: Save configuration
ios_command:
commands:
- write memory
delegate_to: "{{ switch_name }}"
when: user_confirm.user_input == "yes"
- name: Abort message
debug:
msg: "Operation cancelled by user"
when: user_confirm.user_input != "yes"