尝试使用 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/