我正在尝试从virtualenv运行剧本,并且正在使用ansible-dynamic库存脚本。 问题是:清单脚本使用的是默认系统解释器,而不是我使用“ ansible_python_interpreter”变量传递的脚本。
如果激活virtualenv并从命令行运行,则剧本可以正常运行。另外,当我使用/opt/myproj/.ve/bin/python3.6 inv.py
运行时,清单脚本工作正常。
但是该剧本是通过其他python脚本而不是从命令行远程调用的。
/opt/myproj/.ve/bin/ansible-playbook playbooks/rotate_passwords.yml -i inv.py -e ansible_python_interpreter=/opt/myproj/.ve/bin/python3.6 -vvv
ansible-playbook 2.7.10
config file = /opt/myproj/ansible.cfg
configured module search path = ['/opt/myproj/library']
ansible python module location = /opt/myproj/.ve/lib64/python3.6/site-packages/ansible
executable location = /opt/myproj/.ve/bin/ansible-playbook
python version = 3.6.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /opt/myproj/ansible.cfg as config file
setting up inventory plugins
/opt/myproj/inv.py did not meet host_list requirements, check plugin documentation if this is unexpected
/opt/myproj/inv.py did not meet yaml requirements, check plugin documentation if this is unexpected
/opt/myproj/inv.py did not meet auto requirements, check plugin documentation if this is unexpected
[WARNING]: * Failed to parse /opt/myproj/inv.py with script plugin: Inventory script (/opt/myproj/inv.py) had an execution error: Traceback (most recent call last):
File "/opt/myproj/inv.py", line 11, in <module> import pymysql.cursors ModuleNotFoundError: No module named 'pymysql'
我希望inv.py应该在给定ansible_python_interpreter的情况下调用,我已经在其中安装了所有pip模块。
答案 0 :(得分:1)
Ansible清单脚本不会像模块那样收到任何特殊的Python替换行为。由于它们可以用任何语言编写,因此唯一的要求是将执行位设置为有效,并设置有效的shebang。因此,不管shebang说什么,它都会运行。
几个选项:
#!/usr/bin/env python
上进行shebang-应该从控制器继承venv Python。