Ansible /bin/sh: 1: /usr/bin/python: not found

Ansible which is now a days become very popular among DevOps folks. Ansible is a very simple automation tool widely use for managing the infrastructure and continuous delivery. You can install ansible in Linux system and it is agentless.

Recently after installing Ansible 2.2.1 first I encountered with this error that is “module_stdout”: “/bin/sh: 1: /usr/bin/python: not found\r\n”.

sharad@ip-172-31-7-94:~$ ansible -i inventory/staging staging -m ping
172.31.22.117 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Shared connection to 172.31.22.117 closed.\r\n", 
    "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n", 
    "msg": "MODULE FAILURE"
}

As we know currently the ansible modules supports python 2.4, which is very well defined here in Ansible Official page

Setting of an inventory variable ‘ansible_python_interpreter’ on any host will allow Ansible to auto-replace the interpreter used when executing python modules. Thus, you can point to any python you want on the system if /usr/bin/python on your system does not point to a Python 2.X interpreter.

Solution: To get your ansible working you have to do two things as given below.

1. Install python 2.x in remote server.

   ### In Ubuntu/Debian system
   sudo apt-get update
   sudo apt-get install python2.7

2. Set the ‘ansible_python_interpreter’ in inventory file at Ansible Server.
Given below is sample from my inventory file.

[staging:vars]
   ansible_python_interpreter=/usr/bin/python2.7

After doing above steps. All gone successfully well. Here is the result.

sharad@ip-172-31-7-94:~$ ansible -i inventory/staging staging -m ping
172.31.22.117 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

4 thoughts on “Ansible /bin/sh: 1: /usr/bin/python: not found”

  1. As the new ubuntu 16 now ships with python3, I had to do this for every host – since ansible uses python 2.7 – fixed my issue 🙂

    ansible myhost –sudo -m raw -a “apt-get install -y python2.7 python-simplejson”

    Thought I would leave it here as thanks for pointing me in the right direction, in case it helps someone else 🙂

  2. I have tried putting the code snippet in /etc/ansible/hosts file but it says
    ”’
    ERROR! Attempted to read “/etc/ansible/hosts” as YAML: Syntax Error while loading YAML.

    The error appears to have been in ‘/etc/ansible/hosts’: line 46, column 1, but may
    be elsewhere in the file depending on the exact syntax problem.

    The offending line appears to be:

    [servers]
    35.167.173.65
    ^ here

    Attempted to read “/etc/ansible/hosts” as ini file: /etc/ansible/hosts:15: Section [staging:vars] not valid for undefined group: staging
    ”’

    The problem is still there, I had overcome it by -e ‘ansible_python_interpreter=/usr/bin/python3’ on CLI

    • Hi Ashish,

      To deal with Syntax issue, setup your vim suitable to work in Ansible. For this you can install the vim plugin.
      I will recommend this plugin created by David from honneffer.com https://github.com/pearofducks/ansible-vim .
      You should also search for other YAML syntax settings in vim.

      I believe you have latest ansible 2.2 version. When you are using this new version you should use ansible_python_interpreter so that it can work with OS which has default python as version 3.x .
      As we know Ansible has lots of module which are made on the basis of python 2.x . Python 3 and Python 2 are different so Ansible team is taking time to create more module compatible with Python 3.

      I hope you have also gone through with this Ansible url https://docs.ansible.com/ansible/python_3_support.html

      Regards
      Sharad

Comments are closed.