1
votes

When I set the value for ansible_python_interpreter for a host to /usr/bin/python3 ansible-playbook still seems to be using /usr/bin/python which points to python2.7 I get this from the -vvv where the output says

Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/vmware/vmware_guest.py

I put in a debug in my yml file that was to output the variable and it returns:

"ansible_python_interpreter": "/usr/bin/python3"

ansible --version
ansible 2.8.2
   config file = /ansible/automation/ansible.cfg
   configured module search path = ['/root/.ansible/plugins/modules', 
 '/usr/share/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.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 
20150623 (Red Hat 4.8.5-36)]

I have shifted the location in the inventory file to be the first variable and I added it as a group variable but no joy.

When I ran the ansible-playbook command with the -e ansible_python_interpreter=/usr/bin/python3 the script ran correctly

Inventory file

[control] <REDACTED HOST NAME> ansible_connection=local ansible_python_interpreter=/usr/bin/python3

yml excerpt

--- - hosts: control gather_facts: false tasks: - name: Debug a variable debug: var: ansible_python_intrepreter - name: Clone a virtual machine from Windows template and customize vmware_guest: hostname: "{{ hostname }}" username: "{{ username }}" password: "{{ password }}" validate_certs: no datacenter: some-datacenter cluster: some-cluster folder: "some-folder" name: some-host template: some-template datastore: "some-datastore" networks: - name: some-network-name ip: x.x.x.x netmask: 255.255.255.0 gateway: x.x.x.x mac: aa:bb:dd:aa:00:14 domain: domain.com dns_servers: - x.x.x.x - x.x.x.x

When the process runs I get this error "Failed to import the required Python library (requests) on Python /usr/bin/python2"

1

1 Answers

1
votes

The variables must be in the same line as the host.

[control]
REDACTED_HOST_NAME ansible_connection=local ansible_python_interpreter=/usr/bin/python3

It is more convenient to use the group_vars

[control]
REDACTED_HOST_NAME
[control:vars]
ansible_connection=local
ansible_python_interpreter=/usr/bin/python3

With the hosts

localhost
[control]
REDACTED_HOST_NAME
[control:vars]
ansible_connection=local
ansible_python_interpreter=/usr/bin/python3

the play

- hosts: localhost
  gather_facts: no
  tasks:
    - debug:
        msg: "{{ hostvars['REDACTED_HOST_NAME'].ansible_python_interpreter }}"

gives

ok: [localhost] => {
    "msg": "/usr/bin/python3"
}

, but with the hosts below

localhost
[control]
REDACTED_HOST_NAME ansible_connection=local
ansible_python_interpreter=/usr/bin/python3

the same play fails

'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_python_interpreter'