Ansible fails with bin sh 1 usr bin python not found

When running Ansible, you may encounter an error message stating “bin/sh: 1: /usr/bin/python: not found”. This error occurs when Ansible is unable to locate the Python interpreter on the remote host. In this article, we will explore three different ways to solve this issue.

Solution 1: Specify the Python Interpreter

One way to resolve this error is by explicitly specifying the path to the Python interpreter in your Ansible playbook. To do this, you can add the following line at the beginning of your playbook:

---
- hosts: your_host
  become: true
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: Your task
      # Your task details here

In the above code snippet, we set the variable “ansible_python_interpreter” to the path of the Python interpreter on the remote host. This ensures that Ansible uses the correct interpreter and avoids the “not found” error.

Solution 2: Install Python on the Remote Host

If the remote host does not have Python installed, you can resolve the issue by installing Python on the host. You can use the “raw” module in Ansible to execute a shell command and install Python. Here’s an example:

---
- hosts: your_host
  become: true
  tasks:
    - name: Install Python
      raw: apt-get install -y python

In the above code snippet, we use the “raw” module to execute the shell command “apt-get install -y python” on the remote host. This command installs Python, resolving the “not found” error.

Solution 3: Use a Different Python Interpreter

If the remote host has a different Python interpreter installed, you can specify that interpreter in your Ansible playbook. This can be useful if you want to use a specific version of Python or a virtual environment. Here’s an example:

---
- hosts: your_host
  become: true
  vars:
    ansible_python_interpreter: /path/to/your/python
  tasks:
    - name: Your task
      # Your task details here

In the above code snippet, we set the variable “ansible_python_interpreter” to the path of the desired Python interpreter on the remote host. This allows Ansible to use the specified interpreter and avoid the error.

After exploring these three solutions, it is evident that Solution 1, which involves explicitly specifying the Python interpreter, is the better option. This solution ensures that Ansible uses the correct interpreter and avoids any potential conflicts or issues with different Python versions or installations. It provides a more reliable and consistent approach to resolving the “bin/sh: 1: /usr/bin/python: not found” error.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents