在ansible_python_interpreter变量中使用env变量

时间:2017-08-16 17:03:37

标签: ansible

如何在ansible_python_interpreter变量中使用$ {USER}?

在deploy.yml中,我有几个任务,第一个任务是将python安装到/ local / $ {USER} / venv。后续任务应该使用已安装的python而不是我自己的环境中的那个。 我尝试了不同的组合,那些组合不起作用。

deploy.yml

- name: use installed python
  host: localhost
  vars_files:
    - settings.yml
  vars:
    # commented items did not work neither here nor in settings.yml
    # ansible_python_interpreter: "/local/{{lookup('env', 'USER')}}/venv/bin/python"                                                                                          
    # ansible_python_interpreter: "/local/${USER}/venv/bin/python"                                                                                                            
    # venv_dir defined in settings.yml
    # ansible_python_interpreter: "{{venv_dir}}/bin/python"
    # hardcoded worked:
    ansible_python_interpreter: "/local/myuser/bin/python"

错误如:

"/bin/sh: {{venv_dir}}/bin/python: No such file or directory\n"
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/bin/sh: {{venv_dir}}/bin/python: No such file or directory\n", "module_stdout": "", "msg": "MODULE FAILURE"}

settings.yml中:

---
root_dir: "/local/{{lookup('env', 'USER')}}"
venv_dir: "{{root_dir}}/venv/"

注意:将ansible_python_interpreter移动到settings.yml中没有帮助。

我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

我猜ansible_python_interpreter并没有模仿。

您可以使用set_fact解决方法:

---
- hosts: localhost
  gather_facts: false
  tasks:
    # default Python here
    - shell: echo hello
    - set_fact:
        ansible_python_interpreter: /local/{{lookup('env', 'USER')}}/venv/bin/python
    # modified Python here
    - shell: echo hello

答案 1 :(得分:0)

在全球范围内,使用interpreter_python的{​​{1}}部分中的[defaults]

interpreter_python = / usr / bin / python2.7