1

我正在尝试将 Ansible 与其CloudStack 模块一起使用,例如在支持 CloudStack 的云提供商处创建一个计算实例(这里: Exoscale,在这种情况下应该无关紧要)。

tl;博士

ansible找不到CloudStack python 库:cs

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

尽管已安装。

细节

我的库存/主机文件由一行组成:

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(通过 pip 在同一个 conda 环境中安装)

/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,已安装并且可以在 playbook 中手动调用。然而,CloudStack 模块没有找到该库。

到目前为止尝试过

  • 使用 python 2.7 和 3.6 测试

欢迎任何帮助!

4

1 回答 1

2

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

您可以通过ansible_python_interpreter. localhost如果您在希望本地任务使用的同一 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:
  ...
于 2018-04-20T08:14:10.367 回答