Ravi Patel
Ravi Patel

Reputation: 141

getting Ansible bad python interpreter error?

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.

Upvotes: 3

Views: 21238

Answers (4)

Princewill
Princewill

Reputation: 31

This could also happen when you have a newer version of Python installed (upgraded) since the last time you ran Ansible.

This fixed it for me. brew reinstall ansible ansible-lint

Upvotes: 0

Noushad
Noushad

Reputation: 6781

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)]

Upvotes: 2

Ravi Patel
Ravi Patel

Reputation: 141

/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
  }

Upvotes: 10

Jitendra Bhalothia
Jitendra Bhalothia

Reputation: 407

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:

  1. Check the python version command: which python /usr/bin/python
  2. 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.

Upvotes: 1

Related Questions