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

Solution 1:

I stumbled upon this error running ansible on Ubuntu 15.10 server, because it ships with Python 3.4.3 and ansible requires Python 2.

This is how my provision.yml looks now:

- hosts: my_app
  sudo: yes
  remote_user: root
  gather_facts: no
  pre_tasks:
    - name: 'install python2'
      raw: sudo apt-get -y install python

  tasks:
    - name: 'ensure user {{ project_name }} exists'
      user: name={{ project_name }} state=present
  • Don't forget the -y (says yes to all questions) option with apt-get (or raw module will get stuck silently)

  • gather_facts: no line is also critical (because we can't gather facts without python)

Solution 2:

Ansible 2.2 features a tech preview of Python 3 support. To take advantage of this (so you don't have to install Python 2 on Ubuntu 16.04), just set the ansible_python_interpreter config option to /usr/bin/python3. This can be done on a per-host basis in your inventory file:

[db]
123.123.123.123 ansible_python_interpreter=/usr/bin/python3

Solution 3:

Solution 1:

If you're using Ansible >2.2.0, you can set the ansible_python_interpreter configuration option to /usr/bin/python3:

ansible my_ubuntu_host -m ping -e 'ansible_python_interpreter=/usr/bin/python3'

or in your inventory file:

[ubuntu_hosts]
<xxx.xxx.xxx.xxx>

[ubuntu_hosts:vars]
ansible_python_interpreter=/usr/bin/python3

Solution 2:

If you're using Ansible <2.2.0 then you can add these pre_tasks to your playbook:

gather_facts: False
pre_tasks:
  - name: Install python for Ansible
    raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
    register: output
    changed_when: output.stdout != ""
    tags: always
  - setup: # aka gather_facts

UPDATE With ansible 2.8.x, you don't need to worry about it, it's working out of the box for python > 3.5 for both controller and target machine(s)

Solution 4:

You can use the raw module to install Python on the remote hosts:

- raw: sudo apt-get install python-simplejson