getting Ansible bad python interpreter error?
Solution 1
/usr/local/bin/ansible
has PATH "/usr/local/opt/python/bin/python2.7"
on the first line. and in /usr/local/opt/python/bin/
directory I had python3.6 instead of python2.7.
So I changed PATH on file vi /usr/local/bin/ansible
from #!/usr/local/opt/python/bin/python2.7
to #!/usr/local/opt/python/bin/python3.6
and that fixed the issue
Verification :
$ ansible --version
ansible 2.5.0
config file = None
configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/<username>/Library/Python/3.6/lib/python/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.6.4 (default, Mar 1 2018, 18:36:50) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
$ ansible localhost -m ping
localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
$ ansible localhost -m setup -a 'filter=ansible_distribution'
localhost | SUCCESS => {
"ansible_facts": {
"ansible_distribution": "MacOSX"
},
"changed": false
}
Solution 2
In my case I am using pyenv to manage my python versions and $PATH and symlinks were all correct pointing to the correct paths.
Check your python paths
$ pyenv which python
/Users/<username>/.pyenv/versions/3.7.3/bin/python
$ which python
/Users/<username>/.pyenv/shims/python
Check ansible configuration
ansible configuration at /usr/local/bin/ansible
pointed the correct python version 3.7
#!/usr/local/opt/python/bin/python3.7
.
.
.
but ansible --version
returned python 2.7 as its interpreter
$ ansible --version
ansible 2.9.12
configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/<username>/.pyenv/versions/2.7.16/lib/python2.7/site-packages/ansible
executable location = /Users/<username>/.pyenv/versions/2.7.16/bin/ansible
python version = 2.7.16 (default, Apr 2 2020, 13:02:51) [Clang 11.0.3 (clang-1103.0.32.29)]
Ansible for Python3
Official ansible docs said to use pip3 to install ansible for python3 and I uninstalled ansible and reinstalled using pip3 but the interperter still pointed to python2.7.
Finally I manually added .ansible.cfg file in my home path and configured python interpreted manually by adding
ansible_python_interpreter=/usr/bin/python
Example config file for ansible.cfg
Now ansible is configured for python3 correctly
ansible 2.9.12
config file = /Users/<username>/.ansible.cfg
configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /Users/<username>/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ansible
executable location = /Users/<username>/.pyenv/versions/3.7.3/bin/ansible
python version = 3.7.3 (default, Apr 2 2020, 13:02:51) [Clang 11.0.3 (clang-1103.0.32.29)]
Solution 3
Changing the python version might be pushing into some compatibility issues
It happens, when we have multiple python versions installed in our OS.
Simple steps for troubleshooting:
- Check the python version
command:
which python /usr/bin/python
- Create a soft link to the path
command :
ln -s /usr/bin/python /usr/local/opt/python/bin/python2.7
I hope it will fix the error.
Related videos on Youtube
Ravi Patel
Updated on May 28, 2022Comments
-
Ravi Patel almost 2 years
I installed ansible on MAC High Sierra 10.13.3 and when I am trying to run
"ansible --version" I am receiving following error
-bash: /usr/local/bin/ansible: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory
Please let me know if you have ran into same issue or have solution.
-
Konstantin Suvorov almost 6 yearsPython 3 support is still in a tech preview status, so you'd better just install python2.7 into your system and not edit source files.