[Solved] 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:

[email protected]:~# 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
    ansible_python_interpreter: /usr/bin/python3
    - { 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.

Enquirer: Cook

Solution #1:

Referring from the official ansible docs –

Individual Linux distribution packages may be packaged for Python2 or Python3. When running from distro packages you’ll only be able to use Ansible with the Python version for which it was installed.
Sometimes distros will provide a means of installing for several Python versions (via a separate package or via some commands that are run after install). You’ll need to check with your distro to see if that applies in your case.

The easiest way to run /usr/bin/ansible under Python 3 is to install it with the Python3 version of pip. This will make the default /usr/bin/ansible run with Python3:

$ pip3 install ansible
$ ansible --version | grep "python version"
  python version = 3.6.2 (default, Sep 22 2017, 08:28:09) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]

If you are running Ansible Running From Source and want to use Python 3 with your source checkout, run your command via python3. For example:

$ source ./hacking/env-setup
$ python3 $(which ansible) localhost -m ping
$ python3 $(which ansible-playbook) sample-playbook.yml

Respondent: shiva2492
Solution #2:

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
Respondent: Josip Rodin
Solution #3:

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
Respondent: MMM
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.