Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to choose a python interpreter for Ansible playbook?

Tags:

python

ansible

I have python2.7 and python3.5 in my ansible server , while executing playbooks it is using python2.7. I wanted ansible to use python3.5 when executing playbooks.

 in order:
   1 have set export path.
   2 also changed default interpreter path in ansible.cfg as well.
   3 have given specific interpretor path in hostsfile for particular host.

But still, ansible is not running python3.

like image 891
phanindra Avatar asked Dec 17 '19 19:12

phanindra


People also ask

How do I change the interpreter in Ansible?

If you want to set the Python interpreter for individual hosts and groups, set the ansible_python_interpreter inventory variable. If however, you want to set the Python interpreter for global use, then set the interpreter_python key in the [defaults] section in the configuration file ansible.

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.

Does Ansible requires a Python agent on each managed host?

Any ansible operation requires python on the target node except the raw and script modules.


2 Answers

If you want to set the Python interpreter for individual hosts and groups, set the ansible_python_interpreter inventory variable.

If however, you want to set the Python interpreter for global use, then set the interpreter_python key in the [defaults] section in the configuration file ansible.cfg.

For a complete list of possible values for the two options above, please see: https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html

Also see this example for usage of ansible_python_interpreter: https://docs.ansible.com/ansible/2.4/python_3_support.html , section "Testing Python 3 module support".

like image 99
Paulie-C Avatar answered Sep 20 '22 13:09

Paulie-C


1) There is ANSIBLE_PYTHON_INTERPRETER configuration parameter to set:

Path to the Python interpreter to be used for module execution on remote targets

2) The version of Python on controller depends on how Ansible has been built. For example

  • Ubuntu 18.04 use Python 2.x
shell> grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"

shell> dpkg -l | grep ansible
ii  ansible                                2.9.6-1ppa~bionic

shell> ansible --version
ansible 2.9.6
  config file = /home/admin/.ansible.cfg
  configured module search path = [u'/home/admin/.ansible/my_modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
  • Ubuntu 20.04 use Python 3.x
shell> grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"

shell> dpkg -l | grep ansible
ii  ansible                               2.9.6+dfsg-1

shell> ansible --version
ansible 2.9.6
  config file = /home/admin/.ansible.cfg
  configured module search path = ['/home/admin/.ansible/my_modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
  • Centos 7 use Python 2.7 see build.log
  • FreeBSD use Python 3.x or Python 2.x
sheel> uname -a
FreeBSD master.example.com 12.1-RELEASE FreeBSD 12.1-RELEASE r354233 GENERIC  amd64

shell> pkg info | grep ansible
py27-ansible-2.8.5             Radically simple IT automation
py36-ansible-2.8.5             Radically simple IT automation

shell> ansible --version
ansible 2.8.5
  config file = /home/admin/.ansible.cfg
  configured module search path = ['/home/admin/.ansible/plugins/modules', '/usr/local/share/py36-ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.9 (default, Nov 14 2019, 01:16:50) [GCC 4.2.1 Compatible FreeBSD Clang 6.0.1 (tags/RELEASE_601/final 335540)
like image 45
Vladimir Botka Avatar answered Sep 20 '22 13:09

Vladimir Botka