2

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"

Lou
1661 gold badge1 silver badge15 bronze badges
asked Jul 16, 2019 at 13:42

1 Answer 1

1

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'
answered Jul 16, 2019 at 16:45
Sign up to request clarification or add additional context in comments.

1 Comment

The variables were on one line. For some reason the second variable dropped to the second line when I was putting it into the post. The debug does return as expected it is the rest of the yml that reverts back to python2. I just ran the ansible-playbook command by calling it explicitly from /usr/local/bin and the activity finished successfully. The "which ansible-playbook" command claims it is located in /usr/local/bin so it should be running python3.6. I ran ansible-playbook --version and it returned using python2.7 but if I ran /usr/local/bin/ansible-playbook it returns python3.6

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.