how to force ansible to use python3

Viewed 1997

I am running CentOS7. I have installed dnfand updated the default version on Centos to python3. I have added the ansible_python_interpreter=/bin/python3 on the inventory. Still when I run ansible -- version it still says python2.7. I have also done a yum erase ansible and then a dnfinstall ansible. Finally I uninstalled ansible and installed it via pip3 install ansible. Even after doing all this my version of python is still 2.7. What am I missing to get ansible to use python3 as it's default version?

2 Answers

Follow the steps here which instruct on how to configure the python interpreter with the following variable:

ansible_python_interpreter=/usr/bin/python3

If you're using AWX you can create a group in your inventory which represents your python 3 hosts and set the variable at the group level. Or you can do it in global vars, or in the playbooks themselves.

There's some prerequisite work to do to enable python 3 in ansible. It's covered in the doc linked above.

just to expand on @colyn1337 answer I showed some sample code that works for me. This path is on my MAC but find out where your Python3 resides and point it to that directory.

---                                                                         
 - name: load files from AWS S3 bucket
   hosts: localhost
   connection: local
   become: true
   vars:
     ansible_python_interpreter: /Library/Frameworks/Python.framework/Versions/3.9/bin/python3
   tasks:
Related