Ansible库存脚本需要请求模块,如何将软件包安装到Ansible本身?

时间:2019-02-28 17:46:51

标签: python ansible ansible-inventory

我正在使用一个Python脚本,该脚本从rest api中提取,然后以JSON格式生成并打印出清单。与此处的问题how to use json file consisting of host info as input to ansible inventory

非常相似

但是我的脚本要求从其余api提取数据的请求,但是看起来包没有Ansible吗?

def readinstring():   
            try:
                word = str(input('Enter string'))
                #raise Exception
            except ValueError:
                print('bhai string print karna hay')
            #except Exception:
                #print('more than 10 characters, thats wrong nibba')
            else:
                for letter in word:
                    print (letter)
            return



readinstring()

host_wrapper脚本的第一行

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

编辑:

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

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

1 个答案:

答案 0 :(得分:0)

事实证明,我的shebang指向没有安装模块的python 2.7。我的本地python命令默认设置为运行3.6。

修改了我的shebang,使其指向3.6,现在可以正确导入

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