Cisco DevNet 网站提供了丰富的学习资源。当然,首先需要在电脑上安装开发环境。本人所用的电脑是 Win10,因为公司网络环境的原因,也不方便安装 Linux 虚机,所以需要直接在 Win10 上安装这些开发工具和库。
其中有一个步骤,需要 pip install-r requirements.txt
。这个 requirements.txt 第一行就是要 collect Ansible,然后就遇到了麻烦,安装失败,提示 unable to resolve link inside archive
:
Collecting ansible==2.6.2 (from -r requirements.txt (line 1))
Using cached https://files.pythonhosted.org/packages/27/b2/91e7300aaa387538c4fa843db981d254a8903d5d5e807771f348a1981941/ansible-2.6.2.tar.gz
In the tar file C:\Users\chengc2\AppData\Local\Temp\pip-unpack-xjp3i16p\ansible-2.6.2.tar.gz the member ansible-2.6.2/test/sanity/import/lib/ansible/module_utils/ansible_release.py is invalid: unable to resolve link inside archive
...(snip)
安装过程会继续,最后提示一个 error:symbolic link privilegenotheld
错误:
...(snip)
Installing collected packages: ansible, certifi, chardet, lxml, ncclient, pyang, urllib3, requests, xmltodict
Running setup.py install for ansible: started
Running setup.py install for ansible: finished with status 'error'
...(snip)
error: symbolic link privilege not held
----------------------------------------
Rolling back uninstall of ansible
...(snip)
直接在 Shell 里面 pip install ansible
也是一样的结果。
从 https://pypi.org/project/ansible/#files
下载 Ansible 的安装包进行安装,也会失败,只是错误信息有些变化:
$ python setup.py install
...(snip)
AttributeError: 'module' object has no attribute 'symlink'
谷歌了一下,原来是 Ansible 在安装的过程中会有大量的软链接 (Symbolic) 的操作,并且在安装之后会进行测试。但是 Windows 的环境与 Linux 不一样,因为路径太长会导致 Ansible 的测试失败,所以在安装的时候需要一点小技巧。
首先,右键菜单,以管理员身份运行 Bandizip(或者其他 zip tool),解压缩 ansible-2.7.6.tar.gz。
然后,进入解压缩之后的文件夹,把 test/integration/targets/copy
目录删除。
最后,右键菜单,以管理员身份运行 Shell,进入 Ansible 的目录进行安装:
$ python setup.py install
...(snip)
creating build\lib\ansible\modules\network\cli
copying lib\ansible\modules\network\cli\cli_command.py -> build\lib\ansible\modules\network\cli
...(snip)
byte-compiling build\bdist.win-amd64\egg\ansible\modules\network\nxos\nxos_acl.py to nxos_acl.cpython-37.pyc
...(snip)
Using c:\users\chengc2\nxos-code\py3-venv\lib\site-packages
Finished processing dependencies for ansible==2.7.6
(py3-venv)
安装过程顺利结束。我们来测试一下:
$ python -i
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ansible
>>>
接下来就可以继续安装 nxos 实验所需要的库了。但是有一点很重要,一定要修改本地的 requirements.txt 文件,把第一行 ansible 删除掉或者注释掉。否则我们辛苦安装的 Ansible 会被卸载,而 pip install
仍然会失败。
$ cd nxos-code
$ pip install -r requirements.txt
Requirement already satisfied: asn1crypto==0.24.0 in c:\users\chengc2\py3-venv\lib\site-packages\asn1crypto-0.24.0-py3.7.egg (from -r requirements.txt (line 1)) (0.24.0)
Collecting bcrypt==3.1.4 (from -r requirements.txt (line 2))
...(snip)
不过还是有个问题,Cisco 在 GitHub 上面的 requirements.txt 是半年前更新的,版本号都有点老。如果已经在 DevNet site 上完成了一些 Fundamental Lab,有些库已经安装了。并且 Ansible 在安转过程中也会安装一些依赖库。所以实际上只需要在 requirements.txt 中保留少数几个库就可以了:
#ansible==2.6.2
#asn1crypto==0.24.0
#bcrypt==3.1.4
#certifi==2018.4.16
#cffi==1.11.5
#chardet==3.0.4
#cryptography==2.3
#idna==2.7
#Jinja2==2.10
#lxml==4.2.4
MarkupSafe==1.0
ncclient==0.6.0
#paramiko==2.4.1
#pyang==1.7.5
#pyasn1==0.4.4
pycparser==2.18
#PyNaCl==1.2.1
#PyYAML==3.13
requests==2.19.1
#six==1.11.0
#urllib3==1.23
#xmltodict==0.11.0
不过既然已经这样了,那就用 pip
更新这些库吧。首先查看一下哪些库的版本是比较旧的:
$ pip list --outdated
Package Version Latest Type
------------ --------- ---------- -----
bcrypt 3.1.4 3.1.6 wheel
certifi 2018.4.16 2018.11.29 wheel
cryptography 2.3 2.5 wheel
idna 2.7 2.8 wheel
lxml 4.2.4 4.3.0 wheel
MarkupSafe 1.0 1.1.0 wheel
ncclient 0.6.0 0.6.3 sdist
paramiko 2.4.1 2.4.2 wheel
pyang 1.7.5 1.7.8 wheel
pyasn1 0.4.4 0.4.5 wheel
pycparser 2.18 2.19 sdist
PyNaCl 1.2.1 1.3.0 wheel
requests 2.19.1 2.21.0 wheel
setuptools 40.6.2 40.6.3 wheel
six 1.11.0 1.12.0 wheel
urllib3 1.23 1.24.1 wheel
如果用 pip install--upgrade
命令来升级,每次只能升级 1 个库。如果要批量更新所有的库,需要写一个 Python 脚本,我们把脚本保存为 pip_upgrade.py
:
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)
然后运行这个脚本,就可以批量更新所有的库了:
$ python pip_upgrade.py
参考:
[1] https://stackoverflow.com/questions/48694085/how-to-overcome-pip-install-ansible-on-windows-failing-with-filename-or-extens
[2] https://stackoverflow.com/questions/2720014/upgrading-all-packages-with-pip




