Ansible批量部署华为CE交换机
有几台华为CE系列交换机上线,需要批量部署,印象中Ansible有专门针对华为CE的模块,于是就有了这篇笔记。
序
CE交换机侧主要配置,主要开启netconf
[CE] user-interface vty 0 4
snetconf server enable
[CE] ssh user username service-type stelnet snetconf
一、安装
$ sudo yum install python-devel python2-pip
CE模块仅支持2.3
$ sudo pip2.7 install ansible==2.3.3
$ sudo pip2.7 install ncclient
二、安装CloudEngine模块
git clone https://github.com/HuaweiSwitch/CloudEngine-Ansible.git
cd CloudEngine-Ansible
sh install.sh
三、快速使用
使用命令模式查看vlan信息
$ ansible -m ce_command -a "commands='display vlan summary' transport='cli' host=192.168.30.101 port=xx username=xx password=xx" localhost --connection local
localhost | SUCCESS => {
"changed": false,
"stdout": [
"Number of static VLAN: 15\nVLAN ID: 1 30 1111 1113 1115 1117 1119 1160 1211\n to 1212 1221 to 1222 1231 to 1232 1501 \n\nNumber of dynamic VLAN: 0\nVLAN ID: \n\nNumber of service VLAN: 31\nVLAN ID: 4064 to 4094"
],
"stdout_lines": [
[
"Number of static VLAN: 15",
"VLAN ID: 1 30 1111 1113 1115 1117 1119 1160 1211",
" to 1212 1221 to 1222 1231 to 1232 1501 ",
"",
"Number of dynamic VLAN: 0",
"VLAN ID: ",
"",
"Number of service VLAN: 31",
"VLAN ID: 4064 to 4094"
]
]
}
ansible-playbook
- inventory
[all:vars]
[sr]
192.168.30.101 ansible_ssh_port=22 username=username password='password'
192.168.30.102 ansible_ssh_port=22 username=username password='password'
- playbook.yml
- name: CloudEngine command test
hosts: sr
connection: local
gather_facts: no
vars:
cli:
host: "{{ inventory_hostname }}"
port: "{{ ansible_ssh_port }}"
username: "{{ username }}"
password: "{{ password }}"
transport: cli
list_of_vlans:
- { id: 1989, name: "test" }
tasks:
- name: Create Vlan
ce_vlan:
vlan_id: "{{ item.id }}"
name: "{{ item.name }}"
state: absent
provider: "{{ cli }}"
with_items: "{{ list_of_vlans }}"
- 执行
$ ansible-playbook -i inventory playbook.yml
PLAY [CloudEngine command test] **********************************************************************************************************************************************************************************
TASK [Create Vlan] ***********************************************************************************************************************************************************************************************
ok: [192.168.30.102] => (item={u'id': 1989, u'name': u'test'})
ok: [192.168.30.101] => (item={u'id': 1989, u'name': u'test'})
PLAY RECAP *******************************************************************************************************************************************************************************************************
192.168.30.101 : ok=1 changed=0 unreachable=0 failed=0
192.168.30.102 : ok=1 changed=0 unreachable=0 failed=0
更多使用案例
https://github.com/HuaweiSwitch/CloudEngine-Ansible/tree/master/examples
报错1
fatal: [192.168.30.102]: FAILED! => {"changed": false, "failed": true, "msg": "unable to open shell. Please see: https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell", "rc":255}
解决
export ANSIBLE_PARAMIKO_HOST_KEY_AUTO_ADD=True
或写入 ansible.cfg
[paramiko_connection]
host_key_auto_add = True
报错2
fatal: [192.168.30.102]: FAILED! => {"changed": false, "failed": true, "msg": "Error: Could not open connection, possibly due to unacceptable SSH subsystem name."}
fatal: [192.168.30.101]: FAILED! => {"changed": false, "failed": true, "msg": "Error: Could not open connection, possibly due to unacceptable SSH subsystem name."}
解决
在交换机侧配置将snetconf加入到service-type
ssh user username service-type stelnet snetconf