我试图将Ansible与其CloudStack模块一起使用,例如在支持CloudStack的云提供商上创建一个计算实例(在这里:外尺度,在本例中不重要)。
tl;博士
ansible找不到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 (与anaconda一起安装的python,名为ansible的conda环境)
/Users/ccauet/anaconda3/envs/ansible/bin/pythonpython --version
Python 2.7.14which ansible (通过pip安装在同一个conda环境中)
/Users/ccauet/anaconda3/envs/ansible/bin/ansibleansible --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模块没有找到库。
到目前为止尝试过
欢迎任何帮助!
发布于 2018-04-20 08:14:10
在我看来,cs_instance模块可能使用的是系统Python而不是conda虚拟环境。我以前在本地运行其他Ansible模块时遇到过这个问题。
通过设置存货中的事实 of localhost,可以使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:
...https://stackoverflow.com/questions/49935738
复制相似问题