简体   繁体   English

Ansible动态库存python解释器问题

[英]Ansible dynamic inventory python interpreter issue

I am trying to run a playbook from virtualenv and I am using ansible-dynamic inventory script. 我正在尝试从virtualenv运行剧本,并且正在使用ansible-dynamic库存脚本。 Problem is: The inventory script is using default system interpreter not the one I have passed using "ansible_python_interpreter" variable. 问题是:清单脚本使用的是默认系统解释器,而不是我使用“ ansible_python_interpreter”变量传递的脚本。

The playbook works fine if activate virtualenv and run from the command line. 如果激活virtualenv并从命令行运行,则剧本可以正常运行。 Also, the inventory script works fine when I run using /opt/myproj/.ve/bin/python3.6 inv.py . 另外,当我使用/opt/myproj/.ve/bin/python3.6 inv.py运行时,清单脚本可以正常工作。 But the playbook is invoked remotely by a different python script, not from the command line. 但是该剧本是由其他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'

I expect the inv.py should get invoked with given ansible_python_interpreter where I have installed all pip modules. 我希望应该在给定的ansible_python_interpreter调用inv.py的地方安装了所有pip模块。

Ansible inventory scripts don't receive any special Python substitution behavior like modules do. Ansible清单脚本不会像模块那样收到任何特殊的Python替换行为。 Since they can be written in any language, the only requirement is that the execute bit is set and a valid shebang is in place. 由于它们可以用任何语言编写,因此唯一的要求是将执行位设置为有效,并设置有效的shebang。 So it's going to literally run whatever the shebang says. 因此,不管shebang说什么,它都会运行。

A couple of options: 有两个选择:

  • Make the shebang on the inventory script #!/usr/bin/env python - that should inherit the venv Python from the controller. 在清单脚本#!/usr/bin/env python进行shebang-该脚本应该从控制器继承venv Python。
  • Rewrite the inventory script as an inventory plugin . 将清单脚本重写为清单插件 It's more complex, but a lot more flexible, and runs inside the controller, so sidesteps any Python configuration issues once your controller is working properly. 它更复杂,但更灵活,并且可以在控制器内部运行,因此一旦控制器正常工作,就可以避免任何Python配置问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM