Ansible - maven_artifact模块的Python LXML问题



我有ansible(v2.0.0.2)和python(v2.7.6),我正在运行"maven_artifact"模块。

作为直接的命令,它工作正常

ansible localhost -m maven_artifact -a "group_id=commons-collections artifact_id=commons-collections dest=/tmp/commons-collections-latest.jar" -vvvv

但是当我通过剧本做同样的事情时

- name: download via maven
maven_artifact: group_id=junit artifact_id=junit dest=/tmp/junit-latest.jar

它失败并出现此错误

fatal: [test01vm1]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "maven_artifact"}, 
"module_stderr": "", 
"module_stdout": "rnTraceback (most recent call last):rn  
File "/home/admin123/.ansible/tmp/ansible-tmp-1454675562.75-201853614879442/maven_artifact",
line 25, in <module>rn    from lxml import etreernImportError: 
No module named lxmlrn", "msg": "MODULE FAILURE", "parsed": false}

我相信可能与python lxml模块有关,我找到了这些现有的票证

http://stackoverflow.com/questions/13355984/get-errors-when-import-lxml-etree-to-python
http://stackoverflow.com/questions/4598229/installing-lxml-module-in-python

我想知道有人可以解决这个问题吗?


编辑 - 添加 python 路径详细信息

我运行了这个命令以查看 python 主页上的路径

14:55:11@pcZBook-15:/usr/local/etc$ python -c 'import sys; print(":".join(sys.path))'

文件夹列表为

:/opt/stack/keystone
:/opt/stack/glance
:/opt/stack/cinder
:/opt/stack/nova
:/opt/stack/horizon
:/usr/lib/python2.7
:/usr/lib/python2.7/plat-x86_64-linux-gnu
:/usr/lib/python2.7/lib-tk
:/usr/lib/python2.7/lib-old
:/usr/lib/python2.7/lib-dynload
:/usr/local/lib/python2.7/dist-packages
:/usr/lib/python2.7/dist-packages
:/usr/lib/python2.7/dist-packages/PILcompat
:/usr/lib/python2.7/dist-packages/gtk-2.0
:/usr/lib/pymodules/python2.7
:/usr/lib/python2.7/dist-packages/ubuntu-sso-client
:/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode

我通常做的是调用:

- name: Install PIP
  apt: name=python-pip state=present
- name: Install lxml
  pip: name=lxml

ansible 似乎试图在远程目标主机上执行"maven_artifact"命令,该主机没有所需的 python 库。

就我而言,我只想在本地"ansible_host"上运行命令,所以我只是添加了"local_action"前缀,命令就会运行。

- name: download via maven
  local_action: maven_artifact group_id=junit artifact_id=junit dest=/tmp/junit-latest.jar

只是做

sudo apt-get install python-lxml

您需要在 [test01vm1] 中安装 lxml。

pip install lxml

验证目标计算机是否具有 python lxml 模块,ansible 选择已安装 lxml 模块的 python 解释器。

[ec2-user@i-05f345345aas6b bin]$ pip list | grep xml
lxml            3.2.1

然后通过添加以下内容仔细检查ansible是否选择了该python版本:

ansible_python_interpreter=/usr/bin/python2

请参阅 https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html

我观察到,如果目标机器中没有定义 python 解释器,ansible 2.12 ansible 默认选择 python3 解释器。所以仔细检查

pip3 list | grep lxml

该 lxml 模块也可用于 python3

最新更新