Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to change Python Version

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.

like image 852
Cook Avatar asked Jan 13 '20 12:01

Cook


People also ask

How do I change the Ansible Python version?

In this case, you need to sudo su then install the ansible with pip3, otherwise, it will end up installing for you account only under: ~/. local/bin. By new pip version, it's recommended to use python3 -m pip install xxx than directly execute pip3 install xxx.

Which version of Python does Ansible use?

Currently Ansible can be run from any machine with Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed. Windows isn't supported for the control machine. This includes Red Hat, Debian, CentOS, macOS, any of the BSDs, and so on.

How do I run Ansible in Python 3?

Ansible will automatically detect and use Python 3 on many platforms that ship with it. To explicitly configure a Python 3 interpreter, set the ansible_python_interpreter inventory variable at a group or host level to the location of a Python 3 interpreter, such as /usr/bin/python3.

Is Ansible based on Python?

Ansible itself is written in Python and has a fairly minimal learning curve. Ansible follows a simple setup procedure and does not depend on any additional software, servers or client daemons. It manages nodes over SSH and is parallel by default.


Video Answer


1 Answers

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
like image 84
MMM Avatar answered Sep 18 '22 14:09

MMM