【问题标题】:how can I specify the version of Python to use in an Ansible playbook?如何指定要在 Ansible 剧本中使用的 Python 版本?
【发布时间】:2019-10-18 12:07:01
【问题描述】:

我正在开发一个仍在运行 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 这么说

您仍然可以将 ansible_python_interpreter 设置为任何变量级别的特定路径(例如,在 host_vars、vars 文件、playbooks 中等)。

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

【问题讨论】:

    标签: ansible


    【解决方案1】:

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

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

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


    答:ansible_python_interpreter 不是Playbook keyword。它是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
    

    ,或任何其他合适的地方。见Variable precedence: Where should I put a variable?


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多