Ansible CloudStack 模块找不到 CloudStack python 库 cs

Ansible CloudStack modules do not find CloudStack python library cs

我正在尝试将 Ansible 与其 CloudStack modules to e.g. create a computing instance at a cloud provider supporting CloudStack (here: Exoscale 一起使用,在这种情况下应该无关紧要)。

tl;dr:

ansible 找不到 CloudStack python library cs:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "python library cs required: pip install cs"}

尽管正在安装。

详情:

我的 inventory/host 文件由一行组成:

localhost ansible_connection=local

我现在 运行 一个最小的测试手册 play.yml ansible-playbook play.yml

play.yml内容为:

---
- hosts: localhost
  become: no
  gather_facts: no

  tasks:
    - name: Include global variables
      include_vars: vars/vars.yml
    - name: Include global secrets
      include_vars: vars/vault.yml

    - name: python version
      command: python --version
      register: python_version

    - name: print python version
      debug:
        msg: "{{ python_version }}"

    - name: manual code execution
      command: python -c "from cs import CloudStack; print(CloudStack)"
      register: cs_output

    - name: print manual code execution
      debug:
        msg: "{{ cs_output }}"

    - name: Create debian instance
      cs_instance:
        api_key: "{{ vault.exoscale.key }}"
        api_secret: "{{ vault.exoscale.secret }}"
        api_url: "{{ exoscale.endpoints.compute }}"
        name: test-vm-1
        iso: Linux Debian 7 64-bit
        hypervisor: VMware

运行 这会产生以下结果:

PLAY [localhost] *******************************************************************************************************

TASK [Include global variables] ****************************************************************************************
ok: [localhost]

TASK [Include global secrets] ******************************************************************************************
ok: [localhost]

TASK [print python version] ********************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": [
            "python",
            "--version"
        ],
        "delta": "0:00:00.014565",
        "end": "2018-04-20 08:14:04.997040",
        "failed": false,
        "rc": 0,
        "start": "2018-04-20 08:14:04.982475",
        "stderr": "Python 2.7.14",
        "stderr_lines": [
            "Python 2.7.14"
        ],
        "stdout": "",
        "stdout_lines": []
    }
}

TASK [manual code execution] *******************************************************************************************
changed: [localhost]

TASK [print manual code execution] *************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": true,
        "cmd": [
            "python",
            "-c",
            "from cs import CloudStack; print(CloudStack)"
        ],
        "delta": "0:00:00.263650",
        "end": "2018-04-20 08:14:05.687666",
        "failed": false,
        "rc": 0,
        "start": "2018-04-20 08:14:05.424016",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "<class 'cs.client.CloudStack'>",
        "stdout_lines": [
            "<class 'cs.client.CloudStack'>"
        ]
    }
}

TASK [Create debian instance] ******************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "python library cs required: pip install cs"}

PLAY RECAP *************************************************************************************************************
localhost                  : ok=7    changed=2    unreachable=0    failed=1    

which python(python安装了anaconda,conda环境命名为ansible

/Users/ccauet/anaconda3/envs/ansible/bin/python

python --version

Python 2.7.14

which ansible(同conda环境通过pip安装)

/Users/ccauet/anaconda3/envs/ansible/bin/ansible

ansible --version(包括使用的 python 版本的定义)

ansible 2.5.0
  config file = /Users/ccauet/Repositories/p8/exoscale/ansible.cfg
  configured module search path = [u'/Users/ccauet/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/ccauet/anaconda3/envs/ansible/lib/python2.7/site-packages/ansible
  executable location = /Users/ccauet/anaconda3/envs/ansible/bin/ansible
  python version = 2.7.14 | packaged by conda-forge | (default, Mar 30 2018, 18:21:11) [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]

pip freeze

ansible==2.5.0
asn1crypto==0.24.0
bcrypt==3.1.4
certifi==2018.4.16
cffi==1.11.5
chardet==3.0.4
cryptography==2.2.2
cs==2.1.6
enum34==1.1.6
idna==2.6
ipaddress==1.0.22
Jinja2==2.10
MarkupSafe==1.0
paramiko==2.4.1
pyasn1==0.4.2
pycparser==2.18
PyNaCl==1.2.1
PyYAML==3.12
requests==2.18.4
six==1.11.0
urllib3==1.22

如您所见,cs 已安装,可以在剧本中手动调用。然而 CloudStack 模块没有找到这个库。

目前已尝试:

欢迎任何帮助!

在我看来 cs_instance 模块可能正在使用系统 Python 而不是 conda 虚拟环境。我之前在本地使用其他 Ansible 模块 运行 时遇到过这个问题。

您可以通过设置 localhostansible_python_interpreter fact in the inventory 让 Ansible 在您的 Conda 虚拟环境中使用 Python 解释器。如果您在您希望本地任务使用的相同 Conda 环境中 运行 Ansible,则可以将其作为前置任务执行,如下所示:

---
- hosts: localhost
  become: no
  gather_facts: no

  pre_tasks:

   - name: Get local python interpreter
     command: which python
     register: local_python_interpreter

   - name: Set ansible_python_interpreter to local python interpreter
     set_fact:
       ansible_python_interpreter: "{{ local_python_interpreter.stdout }}"

  tasks:
  ...