记录华三网络设备ansible环境搭建
一、环境
- CentOS: 7.9
- Python: 3.7.4
- Ansible: 2.10.4
二、ansible环境操作步骤
1、centos默认python为2.7,更新到3.7.4(3.8也是可以),安装依赖:
yum install gcc make zlib zlib-devel openssl openssl-devel libffi-devel bzip2-devel ncurses-devel gdbm-devel readline-devel xz-devel sqlite-devel tk-devel libffi-devel -y
2、下载编译安装python-3,7.4
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar -zxvf Python-3.7.4.tgz
cd Python-3.7.4/
./configure prefix=/usr/local/python3
make && make install
3、将python3.7连接到bin环境中。
find / -name python3
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
4、安装ansible之前有的环境需要将pip3以及setuptools升级到新版本。
pip3 install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/
5、安装ansible2.10.4
pip3 install markupsafe -i https://mirrors.aliyun.com/pypi/simple/
pip3 install ansible==2.10.4 -i https://mirrors.aliyun.com/pypi/simple/
三、华三ansible模块安装
1、安装ansible模块依赖
pip3 install ncclient==0.6.9 -i https://mirrors.aliyun.com/pypi/simple/
pip3 install scp==0.13.3 -i https://mirrors.aliyun.com/pypi/simple/
pip3 install textfsm==1.1.0 -i https://mirrors.aliyun.com/pypi/simple/
pip3 install ipaddr==2.2.0 -i https://mirrors.aliyun.com/pypi/simple/
2、下载华三ansible模块
wget https://github.com/HPENetworking/hpe-cw7-ansible/archive/refs/heads/main.zip
3、解压模块文件
unzip main.zip
4、进入解压文件目录,拷贝两个文件到python3目录进行覆盖原。
cd hpe-cw7-ansible-main/
cp ./for-ncclient/rpc.py /usr/local/python3/lib/python3.7/site-packages/ncclient/operations/rpc.py
cp ./for-ncclient/manager.py /usr/local/python3/lib/python3.7/site-packages/ncclient/manager.py
5、安装ansible模块
chmod 777 setup.py
python3 setup.py install
6、查找namespaces.py文件,并将内容中“http://www.hp.com/”的“hp”修改为“h3c”。
find / -name namespaces.py |grep pyhpecw7
/root/hpe-cw7-ansible-main/pyhpecw7/utils/xml/namespaces.py
/root/hpe-cw7-ansible-main/build/lib/pyhpecw7/utils/xml/namespaces.py
/usr/local/python3/lib/python3.8/site-packages/pyhpecw7-0.0.11-py3.7.egg/pyhpecw7/utils/xml/namespaces.py
四、ansible环境变量配置
1、修改ansible的配置文件
[root@localhost]# vi /etc/ansible/ansible.cfg
[defaults]
library = /root/hpe-cw7-ansible-main/library
host_key_checking = False
command_warnings=False
deprecation_warnings=False
2、将需要配置的交换机ip添加到host s文件中(修改为你的实际地址)
3、创建一个test.yaml的ansible剧本
---
- name: VLAN Automation with Ansible on HP Com7 Devices
hosts: h3c
gather_facts: no
connection: local
tasks:
- name: ensure VLAN 10 exists
comware_vlan: vlanid=10 name=VLAN10_WEB descr=LOCALSEGMENT username={{ username }} password={{ password }} hostname={{ inventory_hostname }}
- name: ensure VLAN 20 exists
comware_vlan: vlanid=20 name=VLAN20 state=present username={{ username }} password={{ password }} hostname={{ inventory_hostname }}
- name: ensure VLAN 10 does not exist
comware_vlan: vlanid=10 state=absent username={{ username }} password={{ password }} hostname={{ inventory_hostname }}
4、配置交换机用户、密码、ssh服务等,允许ssh登录(不再赘述,自行查找资料),ansible服务器可以ping通交换机。
5、执行ansible剧本。
6、在交换机上验证操作。
太多无奈丶: 还有其他方式吗?这样做完其他设备就无法使用了
洗脚小王子: "msg": "ConnectionSSHError: host: 172.18.1.190, port: 830 msg: There was an error connecting with SSH. The NETCONF server may be down or refused the connection. The connection may have timed out if the server wasn't reachable."}
洗脚小王子: Traceback (most recent call last): File "/mnt/zabbix/Python-3.7.4/Lib/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/mnt/zabbix/Python-3.7.4/Lib/runpy.py", line 85, in _run_code exec(code, run_globals) File "/mnt/zabbix/Python-3.7.4/Lib/ensurepip/__main__.py", line 5, in <module> sys.exit(ensurepip._main()) File "/mnt/zabbix/Python-3.7.4/Lib/ensurepip/__init__.py", line 204, in _main default_pip=args.default_pip, File "/mnt/zabbix/Python-3.7.4/Lib/ensurepip/__init__.py", line 117, in _bootstrap return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths) File "/mnt/zabbix/Python-3.7.4/Lib/ensurepip/__init__.py", line 27, in _run_pip import pip._internal zipimport.ZipImportError: can't decompress data; zlib not available make: *** [Makefile:1132: install] Error 1
洗脚小王子: 我想问为什么要覆盖rpc.py这些文件吖
m0_66312004: 普通的ssh客户端都行