python - Ansible:如何更改 Python 版本

标签 python python-3.x ubuntu ansible

尝试使用 GNS3 练习 ansible 脚本,有一个名为“Network Automation”的 docker 实例,内置 ansible。但是,它仍然使用 Python 2.7 作为解释器:

root@Network-Automation:~# ansible --version
ansible 2.7.11
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]

我知道我可以使用“ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'”命令来运行带有 Python 版本 3 的剧本,或者我可以在剧本中指定 var:
- name: Common package
  hosts: all
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  roles:
    - { role: python, tags: [ init, python, common, addusers] }
...
...

但是,我想有一种永久的方式来强制 ansible 使用 Python3 版本。我怎样才能做到这一点?谢谢。

最佳答案

为什么不在你的角色中使用 var 目录...

├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml
在 vars/main.yml
只需添加....
---                                                                                                                                                                        
# vars file for XXXX
  ansible_python_interpreter: /usr/bin/python3

关于python - Ansible:如何更改 Python 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59716485/

相关文章:

python - 如何在 pandas dataframe python 上使用 apply 函数时中断?

Python 赋值运算符?

ruby-on-rails - Nginx 到 Unicorn 的反向代理 - nginx 配置不正确?

CSS 在 MacOS 上看到字体在 VPS 上看不到字体(Ubuntu 20.04 LTS)

python - 如何以非阻塞方式链接 future ?即,如何在不阻塞的情况下将一个 future 作为另一个 future 的输入?

linux - 在 Ubuntu 中重启 Nginx

python - 具有多个值到元组的字典

python - 如何让 python 在 Linux 上使用管道读取 tcpdump 捕获的 .pcap 数据?

Python 文档测试 : skip a test conditionally

python - 无法捕获具有不同模式的某些电话号码