python - 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
解决方案
原来我的shebang指向没有安装模块的python 2.7。我的本地 python 命令默认设置为运行 3.6。
将我的 shebang 修改为指向 3.6,现在可以正确导入
#!/usr/local/bin/python3.6
import requests, json
推荐阅读
- json - 将多个列表与 jq 合并为单个列表
- meteor - Meteor 发布在发布中向客户端发送自定义清理错误
- android - 在flutter中杀死它后需要在android设备中运行该应用程序
- excel - 您输入了太多参数 excel IF 错误
- google-bigquery - 在 BigQuery 中使用 NEST 从气流中运行查询的问题
- elasticsearch - 如何标记星号('*')?
- kubernetes - 用于多个服务的基于路径的路由的 K8s 入口
- javascript - 重新渲染 Reactjs 后,父组件立即丢失状态
- java - 使用 JPA 的域对象中的注释违反了数据库是一个细节
- ms-access - 根据周和年标准选择最新日期