如何指定在Ansible剧本中使用的Python版本?

时间:2019-10-18 12:07:01

标签: ansible

我正在一个仍在运行Python 2的项目中工作。我正在尝试使用Ansible设置新的测试服务器。我开始使用的基本Linux安装仅具有Python 3,因此我需要我的第一个“引导”剧本才能使用Python 3,但随后我希望后续的剧本使用Python 2。

我可以在清单文件中指定python的版本,如下所示:

[test_server:vars]
ansible_python_interpreter=/usr/bin/python3

[test_server]
test_server.example.com

但是随后我必须去编辑清单文件,以确保我将Python 3用于引导剧本,然后再对其余剧本进行编辑。好像很奇怪我尝试过在剧本中更改ansible_python_interpreter的几种不同版本,例如

- hosts: test_server
    ansible_python_interpreter: /usr/bin/python

- hosts: test_server
  tasks:
    - name: install pip
      ansible_python_interpreter: /usr/bin/python
      apt:
        name: python-pip

但ansible抱怨

  

错误! “ ansible_python_interpreter”不是任务的有效属性

尽管https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html表示

  

您仍然可以在任何变量级别(例如在host_vars,vars文件,剧本等中)将ansible_python_解释器设置为特定路径。

正确执行此操作的调用是什么?

1 个答案:

答案 0 :(得分:0)

- hosts: test_server
  tasks:
    - name: install pip
      ansible_python_interpreter: /usr/bin/python
      apt:
        name: python-pip
  

错误! “ ansible_python_interpreter”不是任务的有效属性

     

Q:“正确执行此操作的调用是什么?”

A: ansible_python_interpreter variable,必须这样声明。例如在任务范围内

- hosts: test_server
  tasks:
    - name: install pip
      apt:
        name: python-pip
      vars:
        ansible_python_interpreter: /usr/bin/python

,或在剧本的范围内

- hosts: test_server
  vars:
    ansible_python_interpreter: /usr/bin/python
  tasks:
    - name: install pip
      apt:
        name: python-pip