简体   繁体   English

虽然gce.py有效,但Ansible的GCE模块找不到apache-libcloud

[英]GCE module in Ansible cannot find apache-libcloud although gce.py works

I installed ansible, apache-libcloud with pip. 我用pip安装了ansible,apache-libcloud。 Also, I can use the gcloud cli and ansible works for any non-gce-related playbooks. 此外,我可以使用gcloud cli和ansible工作用于任何非gce相关的剧本。

When using the gce module as a task to create instances in an ansible playbook, the following error occurs: 使用gce模块作为任务在ansible playbook中创建实例时,会发生以下错误:

TASK: [Launch instances] ****************************************************** 
<127.0.0.1> REMOTE_MODULE gce instance_names=mm2 machine_type=f1-micro image=ubuntu-1204-precise-v20150625 zone=europe-west1-d service_account_email= pem_file=../pkey.pem project_id=fancystuff-11
<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1437669562.03-233461447935889 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1437669562.03-233461447935889 && echo $HOME/.ansible/tmp/ansible-tmp-1437669562.03-233461447935889']
<127.0.0.1> PUT /var/folders/v4/ll0_f8lj7yl7yghb645h95q9ckfc19/T/tmpyDoPt9 TO /Users/d046179/.ansible/tmp/ansible-tmp-1437669562.03-233461447935889/gce
<127.0.0.1> EXEC ['/bin/sh', '-c', u'LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 /usr/bin/python /Users/d046179/.ansible/tmp/ansible-tmp-1437669562.03-233461447935889/gce; rm -rf /Users/d046179/.ansible/tmp/ansible-tmp-1437669562.03-233461447935889/ >/dev/null 2>&1']
failed: [localhost -> 127.0.0.1] => {"failed": true, "parsed": false}
failed=True msg='libcloud with GCE support (0.13.3+) required for this module'


FATAL: all hosts have already failed -- aborting

And the site.yml of the playbook I wrote: 我写的剧本的site.yml:

 name: Create a sandbox instance
  hosts: localhost
  vars:
    names: mm2
    machine_type: f1-micro
    image: ubuntu-1204-precise-v20150625
    zone: europe-west1-d
    service_account_email: xxx@developer.gserviceaccount.com
    pem_file: ../pkey.pem
    project_id: fancystuff-11
  tasks:
    - name: Launch instances
      local_action: gce instance_names={{names}} machine_type={{machine_type}}
                    image={{image}} zone={{zone}} service_account_email={{ service_account_email }}
                    pem_file={{ pem_file }} project_id={{ project_id }}
      register: gce

The gce cloud module fails with the error message "ibcloud with GCE support (0.13.3+) required for this module". gce云模块失败,并显示错误消息“此模块需要支持GCE(0.13.3+)的ibcloud”。 However, running gce.py from the ansible github repo works. 但是,从ansible github repo运行gce.py可以正常工作。 The python script finds the apache-libcloud library and prints a json with all running instances. python脚本找到apache-libcloud库并打印一个包含所有正在运行的实例的json。 Besides, pip install apache-libcloud states it is installed properly. 此外,pip install apache-libcloud表示它已正确安装。

Is there anything I am missing like an environment variable that points to the python libraries (PYTHONPATH)? 有没有什么我想丢失的环境变量指向python库(PYTHONPATH)?


UPDATE 1: 更新1:

I included the following task before the gce task: 我在gce任务之前包含了以下任务:

- name: install libcloud
  pip: name=apache-libcloud

This also does not affect the behavior nor prevents any error messages. 这也不会影响行为,也不会阻止任何错误消息。


Update 2: 更新2:

I added the following task to inspect the available PYTHONPATH: 我添加了以下任务来检查可用的PYTHONPATH:

- name: Getting PYTHONPATH
  local_action: shell python -c 'import sys; print(":".join(sys.path))'
  register: pythonpath
- debug:  
    msg: "PYTHONPATH: {{ pythonpath.stdout }}"

The following is returned: 返回以下内容:

PYTHONPATH: :/usr/local/lib/python2.7/site-packages/setuptools-17.1.1-py2.7.egg:/usr/local/lib/python2.7/site-packages/pip-7.0.3-py2.7.egg:/usr/local/lib/python2.7/site-packages:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python27.zip:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/usr/local/Cellar/python/2.7.10/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/site-packages:/Library/Python/2.7/site-packages

UPDATE 3: 更新3:

I introduced my own test.py script as a task which executes the same apache-libcloud imports as the gce ansible module. 我引入了自己的test.py脚本作为执行与gce ansible模块相同的apache-libcloud导入的任务。 The script imports just fine!!! 脚本导入就好!!!

Setting the PYTHONPATH fixes the issue. 设置PYTHONPATH可以解决问题。 For example: 例如:

$ export PYTHONPATH=/usr/local/lib/python2.7/site-packages/

I'm using OSX and I solved this for myself. 我正在使用OSX而且我为自己解决了这个问题。 Short answer: install ansible with pip. 简答:用pip安装ansible。 (rather than eg brew) (而不是例如酿造)

I inspected the PYTHONPATH that Ansible sets runtime and it looked like it had nothing to do whith my normal system PYTHONPATH. 我检查了Ansible设置运行时的PYTHONPATH,看起来它与我的正常系统PYTHONPATH无关。 Eg for me, my system PYTHONPATH was empty, and setting that like eg mlazarov suggested didn't make any difference. 例如,对我来说,我的系统PYTHONPATH是空的,像mlazarov建议的设置没有任何区别。 I made ansible print the PYTHONPATH it uses runtime, and it looked like this: 我用ansible打印了它使用运行时的PYTHONPATH,它看起来像这样:

ok: [localhost] => {
"msg": "PYTHONPATH: :/usr/local/Cellar/ansible/1.9.4/libexec/lib/python2.7/site-packages:/usr/local/Cellar/ansible/1.9.4/libexec/vendor/lib/python2.7/site-packages:/Library/Frameworks/Python.framework/Versions/3.4/lib/python34.zip:/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4:/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plat-darwin:/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/lib-dynload:/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages"
}

So there's only ansible's own site-packages and some strange Python3 installations (I'm using python2.7) 所以只有ansible自己的站点包和一些奇怪的Python3安装(我使用的是python2.7)

Something in this discussion made me think it might be a problem with the ansible installation, my ansible was installed with brew. 这个讨论中的一些东西让我觉得它可能是ansible安装的问题,我的ansible安装了brew。 I reinstalled it globally with pip (simply running sudo pip install ansible), and that fixed the problem. 我用pip重新安装它(简单地运行sudo pip install ansible),这解决了问题。 Now the PYTHONPATH ansible prints looks much better, with my virtualenv python installation in the beginning, and no more "libcloud with GCE support (0.13.3+) required for this module". 现在PYTHONPATH ansible打印看起来好多了,我的virtualenv python安装在开始时,并且不再需要“此模块需要GCE支持(0.13.3+)的libcloud”。

I was able to resolve the issue by setting the PYTHONPATH environment variable (export PYTHONPATH=/path/to/site-packages) with the current site-packages folder. 我能够通过使用当前的site-packages文件夹设置PYTHONPATH环境变量(导出PYTHONPATH = / path / to / site-packages)来解决此问题。 Apparently, ansible establishes its own environment during module execution and ignores any paths available in python except the paths from the environment variable PYTHONPATH. 显然,ansible在模块执行期间建立自己的环境,并忽略python中可用的任何路径,除了来自环境变量PYTHONPATH的路径。

I find this a peculiar behavior which is not documented on the ansible websites. 我发现这是一种奇特的行为,并未在ansible网站上记录。

I have a similar environment setup. 我有类似的环境设置。 I found some information at the bottom of this section: https://github.com/jlund/streisand#prerequisites 我在本节底部找到了一些信息: https//github.com/jlund/streisand#prerequisites

Essentially there's some magic files you can update so the brew'd ansible will add a folder to search for packages: 基本上你可以更新一些魔术文件,所以brew'd ansible会添加一个文件夹来搜索包:

mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth

Hope that fixes it for you! 希望能为你修复它!

就我而言,情况如下:

pip install apache-libcloud

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

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