Reputation: 493
Trying to use GNS3 to practice ansible script, there is a docker instance called "Network Automation" with built-in ansible. However, it still uses Python 2.7 as the interpreter:
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]
I understand I can use "ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'" command to run a playbook with Python version 3, or I can specifiy var within the playbook:
- name: Common package
hosts: all
gather_facts: no
vars:
ansible_python_interpreter: /usr/bin/python3
roles:
- { role: python, tags: [ init, python, common, addusers] }
...
...
However, I would like to have a permanent way to force ansible to use Python3 version. How can I achieve this? Thanks.
Upvotes: 28
Views: 135766
Reputation: 254
I think you can set the variable in the config file, /etc/ansible/ansible.cfg, that should apply it to everything (unless a higher level variable overwrites it).
ansible_python_interpreter = /usr/bin/python3
Upvotes: 4
Reputation: 61
Adding some points that you might overlook based on comments above:
Upvotes: 1
Reputation: 163
Why not use the var directory in your role...
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
in vars/main.yml just add....
---
# vars file for XXXX
ansible_python_interpreter: /usr/bin/python3
Upvotes: 14
Reputation: 809
Per https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html you could simply set it in the inventory for that host, or in your configuration file for ansible (which can also be shipped in the same directory as the playbooks and/or inventory):
To control the discovery behavior:
- for individual hosts and groups, use the ansible_python_interpreter inventory variable
- globally, use the interpreter_python key in the [defaults] section of ansible.cfg
Upvotes: 2