一、环境

  • 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、在交换机上验证操作。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐