Moon
Moon

Reputation: 3037

Why do I get different python version for implicit and explicit localhost

I have both python2.7 and python3.7 in my mac and ansible is installed using python3. Python version and ansible version returns below:

$ python -V
Python 2.7.16
$
$ /usr/bin/env python -V
Python 2.7.16
$
$ /usr/bin/python -V
Python 2.7.16
$
$ ansible --version
ansible 2.9.7
  config file = None
  configured module search path = ['/Users/demo/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/demo/Library/Python/3.7/lib/python/site-packages/ansible
  executable location = /Users/demo/Library/Python/3.7/bin/ansible
  python version = 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)]

Also have an inventory hosts with explicit localhost configuration.

localhost ansible_connection=local 

I understand that setting ansible_python_interpreter for the inventory will use the specific version but wondering why I get two different python versions for below cases that use either implicit or explicit localhost. My understanding was that for both cases ansible will use the default python version.

$ ansible localhost -m setup | grep ansible_python_version
[WARNING]: No inventory was parsed, only implicit localhost is available
        "ansible_python_version": "3.7.3",
$
$ ansible localhost -i hosts -m setup | grep ansible_python_version
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
        "ansible_python_version": "2.7.16",

Upvotes: 2

Views: 3718

Answers (2)

Zeitounator
Zeitounator

Reputation: 44760

The answer is in the documentation

when Ansible needs to contact a ‘localhost’ but you did not supply one, we create one for you. This host is defined with specific connection variables equivalent to this in an inventory:

hosts:
  localhost:
    vars:
      ansible_connection: local
      ansible_python_interpreter: "{{ansible_playbook_python}}"

As you can see the implicit localhost declaration is defining an ansible_python_interpreter pointing to the one used by the current playbook on the controller (i.e. ansible_playbook_python documented in Ansible special variables)

The explicit localhost definition you posted does not define any and uses auto-discovery.

Upvotes: 4

Moon
Moon

Reputation: 3037

Just to share what I have found in my investigation on this topic.

Implicit localhost

$ ansible localhost -m setup | grep ansible_python_version
[WARNING]: No inventory was parsed, only implicit localhost is available
        "ansible_python_version": "3.7.3",

It wasn't obvious to me how ansible_playbook_python determines python interpreter by looking at Implicit ‘localhost’ nor Magic Variables docs. So poked around ansible source a bit and here is what this variable means.

Value for ansible_playbook_python comes from python's sys.executable which returns path of the python interpreter that ansible installation is using. In my case, ansible --version shows it uses python 3.7.3 so as the return. However, there are cases when sys.executable may return nothing (reference) and in that case, ansible would try to fallback to default /usr/bin/python.

Explicit localhost

$ ansible localhost -i hosts -m setup | grep ansible_python_version
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python
interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
        "ansible_python_version": "2.7.16",

Behavior is quite clear from interpreter discovery doc that ansible uses /usr/bin/python by default if available on the host. Otherwise, would try to locate platform specific python interpreter and uses that version. In my case, I already had /usr/bin/python of version 2.7.16 so ansible uses that interpreter and so as the return.

Upvotes: 1

Related Questions