首页 > 解决方案 > Ansible 库存脚本需要 requests 模块,我如何将包安装到 Ansible 本身?

问题描述

我正在使用一个 python 脚本,它从一个 rest api 中提取,然后以 JSON 格式构建并打印出一个清单。与此处的问题非常相似,如何使用包含主机信息的 json 文件作为 ansible 库存的输入

但是我的脚本需要请求从其余 api 中提取数据,但它看起来不像 Ansible 有这个包?

ansible-playbook -i host_wrapper.py gather_ios_facts.yml -k -vvv
...
/Users/alexw/ansible/host_wrapper.py did not meet yaml requirements, check plugin documentation if this is unexpected
 [WARNING]:  * Failed to parse /Users/alexw/ansible/host_wrapper.py with script plugin: Inventory script
(/Users/alexw/ansible/host_wrapper.py) had an execution error: Traceback (most recent call last):   File
"/Users/alexw/ansible/host_wrapper.py", line 2, in <module>     import requests, json ImportError: No module named
requests

  File "/usr/local/lib/python3.6/site-packages/ansible/plugins/inventory/script.py", line 114, in parse
    raise AnsibleError("Inventory script (%s) had an execution error: %s " % (path, err))

我的 host_wrapper 脚本的第一行

#!/usr/bin/env python
import requests, json
...

编辑:

我以为我会用 pip 安装它,但它仍然失败,输出如下:

AW-MacBook-Pro:ansible alexw$ pip install reqeusts
Collecting reqeusts
  Could not find a version that satisfies the requirement reqeusts (from versions: )
No matching distribution found for reqeusts
You are using pip version 19.0.2, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
AW-MacBook-Pro:ansible alexw$ pip install requests
Requirement already satisfied: requests in /usr/local/lib/python3.6/site-packages (2.21.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/site-packages (from requests) (2018.11.29)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.6/site-packages (from requests) (1.24.1)
Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python3.6/site-packages (from requests) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/site-packages (from requests) (3.0.4)
You are using pip version 19.0.2, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

AW-MacBook-Pro:ansible alexw$ ansible-playbook -i host_wrapper.py gather_ios_facts.yml -k -vvv
ansible-playbook 2.7.7
  config file = /Users/alexw/ansible/ansible.cfg
  configured module search path = ['/Users/alexw/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.6.4 (default, Mar 22 2018, 13:54:22) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
Using /Users/alexw/ansible/ansible.cfg as config file
SSH password:
/Users/alexw/ansible/host_wrapper.py did not meet host_list requirements, check plugin documentation if this is unexpected
/Users/alexw/ansible/host_wrapper.py did not meet yaml requirements, check plugin documentation if this is unexpected
 [WARNING]:  * Failed to parse /Users/alexw/ansible/host_wrapper.py with script plugin: Inventory script
(/Users/alexw/ansible/host_wrapper.py) had an execution error: Traceback (most recent call last):   File
"/Users/alexw/ansible/host_wrapper.py", line 2, in <module>     import requests, json ImportError: No module named
requests

标签: pythonansibleansible-inventory

解决方案


原来我的shebang指向没有安装模块的python 2.7。我的本地 python 命令默认设置为运行 3.6。

将我的 shebang 修改为指向 3.6,现在可以正确导入

#!/usr/local/bin/python3.6
import requests, json

推荐阅读