在使用Ansible管理思科交换机时,需要特权提升来执行某些命令。下面是一个包含代码示例的解决方法,示例中使用了NetworkChuck的思科交换机。
---
- name: Configure Cisco Switch using Ansible
hosts: switches
gather_facts: no
connection: network_cli
become: yes
become_method: enable
become_password: "{{ enable_password }}"
tasks:
- name: Show running configuration
ios_command:
commands:
- show running-config
- name: Configure VLAN
ios_command:
commands:
- vlan 10
- name Test_VLAN
- name: Configure interface
ios_config:
lines:
- interface GigabitEthernet1/1
- switchport mode access
- switchport access vlan 10
parents: interface GigabitEthernet1/1
- name: Save configuration
ios_command:
commands:
- write memory
在上面的示例中,become: yes
用于启用特权提升,become_method: enable
用于指定使用enable命令来特权提升,become_password
是一个变量,需要在执行时提供。
在ansible的主机上,可以通过以下命令执行上述playbook:
ansible-playbook -i inventory.ini playbook.yml --extra-vars "enable_password=your_enable_password"
其中,inventory.ini
包含了目标交换机的IP地址或主机名。
通过使用特权提升,可以确保在Ansible中执行特权命令来配置思科交换机。