暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

在Windows10上准备Cisco NXAPI的开发环境之安装Ansible

非资深老网工 2019-01-27
1013

Cisco DevNet 网站提供了丰富的学习资源。当然,首先需要在电脑上安装开发环境。本人所用的电脑是 Win10,因为公司网络环境的原因,也不方便安装 Linux 虚机,所以需要直接在 Win10 上安装这些开发工具和库。

其中有一个步骤,需要 pip install-r requirements.txt
。这个 requirements.txt 第一行就是要 collect Ansible,然后就遇到了麻烦,安装失败,提示 unable to resolve link inside archive

  1. Collecting ansible==2.6.2 (from -r requirements.txt (line 1))

  2.  Using cached https://files.pythonhosted.org/packages/27/b2/91e7300aaa387538c4fa843db981d254a8903d5d5e807771f348a1981941/ansible-2.6.2.tar.gz

  3.  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

  4. ...(snip)

安装过程会继续,最后提示一个 error:symbolic link privilegenotheld
 错误:

  1. ...(snip)

  2. Installing collected packages: ansible, certifi, chardet, lxml, ncclient, pyang, urllib3, requests, xmltodict

  3.  Running setup.py install for ansible: started

  4.    Running setup.py install for ansible: finished with status 'error'

  5. ...(snip)

  6.    error: symbolic link privilege not held

  7.    ----------------------------------------

  8.  Rolling back uninstall of ansible

  9. ...(snip)

直接在 Shell 里面 pip install ansible
 也是一样的结果。

从 https://pypi.org/project/ansible/#files
 下载 Ansible 的安装包进行安装,也会失败,只是错误信息有些变化:

  1. $ python setup.py install

  2. ...(snip)

  3.    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 的目录进行安装:

  1. $ python setup.py install

  2. ...(snip)

  3. creating build\lib\ansible\modules\network\cli

  4. copying lib\ansible\modules\network\cli\cli_command.py -> build\lib\ansible\modules\network\cli

  5. ...(snip)

  6. byte-compiling build\bdist.win-amd64\egg\ansible\modules\network\nxos\nxos_acl.py to nxos_acl.cpython-37.pyc

  7. ...(snip)

  8. Using c:\users\chengc2\nxos-code\py3-venv\lib\site-packages

  9. Finished processing dependencies for ansible==2.7.6

  10. (py3-venv)

安装过程顺利结束。我们来测试一下:

  1. $ python -i

  2. Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32

  3. Type "help", "copyright", "credits" or "license" for more information.

  4. >>> import ansible

  5. >>>

接下来就可以继续安装 nxos 实验所需要的库了。但是有一点很重要,一定要修改本地的 requirements.txt 文件,把第一行 ansible 删除掉或者注释掉。否则我们辛苦安装的 Ansible 会被卸载,而 pip install
 仍然会失败。

  1. $ cd nxos-code

  2. $ pip install -r requirements.txt

  3. 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)

  4. Collecting bcrypt==3.1.4 (from -r requirements.txt (line 2))

  5. ...(snip)

不过还是有个问题,Cisco 在 GitHub 上面的 requirements.txt 是半年前更新的,版本号都有点老。如果已经在 DevNet site 上完成了一些 Fundamental Lab,有些库已经安装了。并且 Ansible 在安转过程中也会安装一些依赖库。所以实际上只需要在 requirements.txt 中保留少数几个库就可以了:

  1. #ansible==2.6.2

  2. #asn1crypto==0.24.0

  3. #bcrypt==3.1.4

  4. #certifi==2018.4.16

  5. #cffi==1.11.5

  6. #chardet==3.0.4

  7. #cryptography==2.3

  8. #idna==2.7

  9. #Jinja2==2.10

  10. #lxml==4.2.4

  11. MarkupSafe==1.0

  12. ncclient==0.6.0

  13. #paramiko==2.4.1

  14. #pyang==1.7.5

  15. #pyasn1==0.4.4

  16. pycparser==2.18

  17. #PyNaCl==1.2.1

  18. #PyYAML==3.13

  19. requests==2.19.1

  20. #six==1.11.0

  21. #urllib3==1.23

  22. #xmltodict==0.11.0

不过既然已经这样了,那就用 pip
 更新这些库吧。首先查看一下哪些库的版本是比较旧的:

  1. $ pip list --outdated

  2. Package      Version   Latest     Type

  3. ------------ --------- ---------- -----

  4. bcrypt       3.1.4     3.1.6      wheel

  5. certifi      2018.4.16 2018.11.29 wheel

  6. cryptography 2.3       2.5        wheel

  7. idna         2.7       2.8        wheel

  8. lxml         4.2.4     4.3.0      wheel

  9. MarkupSafe   1.0       1.1.0      wheel

  10. ncclient     0.6.0     0.6.3      sdist

  11. paramiko     2.4.1     2.4.2      wheel

  12. pyang        1.7.5     1.7.8      wheel

  13. pyasn1       0.4.4     0.4.5      wheel

  14. pycparser    2.18      2.19       sdist

  15. PyNaCl       1.2.1     1.3.0      wheel

  16. requests     2.19.1    2.21.0     wheel

  17. setuptools   40.6.2    40.6.3     wheel

  18. six          1.11.0    1.12.0     wheel

  19. urllib3      1.23      1.24.1     wheel

如果用 pip install--upgrade
 命令来升级,每次只能升级 1 个库。如果要批量更新所有的库,需要写一个 Python 脚本,我们把脚本保存为 pip_upgrade.py

  1. import pkg_resources

  2. from subprocess import call

  3. packages = [dist.project_name for dist in pkg_resources.working_set]

  4. call("pip install --upgrade " + ' '.join(packages), shell=True)

然后运行这个脚本,就可以批量更新所有的库了:

  1. $ 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


文章转载自非资深老网工,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论