I'm running into an error I've never seen before. Here is the command and the error:
$ ansible-playbook create_api.yml
PLAY [straw] ******************************************************************
GATHERING FACTS ***************************************************************
failed: [104.55.47.224] => {"failed": true, "parsed": false}
/bin/sh: 1: /usr/bin/python: not found
TASK: [typical | install required system packages] *****************************
FATAL: no hosts matched or all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/john/create_api.retry
104.55.47.224 : ok=0 changed=0 unreachable=0 failed=1
Here is the create_api.yml file:
---
- hosts: api
remote_user: root
roles:
- api
And here is the hosts file:
[api]
104.55.47.224
I can remove the roles section and it won't make it to the first TASK, it will instead make it will only make it to the line /bin/sh: 1: /usr/bin/python: not found
. What could be going on here?
NOTE: In case anyone is pinging the IP address and failing to get a response, you should know I've changed the IP address since pasting code.
EDIT python was installed locally, the problem was that it was not installed on the remote machine, which was running Ubuntu 15.04
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: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)THose using Packer may find below solution helpful
let's assume that you use ansible provisioner of packer, your config may look like below
you could install python using shell provisioner first then configure ansible_python_intepreter option as shown below
You can indicate to Ubuntu 18.04 that you want to use python3 as the the first priority for
/usr/bin/python
.I personally found 3 possible solutions to this problem that work well in different situations:
Option 1 - Set
ansible_python_interpreter: /usr/bin/python3
for hosts that havepython3
installed by defaultI think this is the superior method for solving the problem if you have a way to group your hosts by whether or not they have
python3
installed by default. As far as I'm aware,python3
is available on all Ubuntu releases 16.04 and higher.python3
, you could add the variable to yourgroup_vars/all.yml
(or equivalent):python3
and you have a way to tag them when using dynamic inventory (e.g. AWS tagging forec2.py
), you could apply the variable to certain hosts like this:python3
, you could do something like this:I like this option the most because it requires no changes on the remote host and only minor changes to variables, as opposed to options 2 and 3, which require additions to every playbook.
Option 2 - Install Python 2 using
raw
This option requires putting a play at the top of every playbook with
gather_facts: false
that usesraw
to installpython
:ignore_errors: true
is required if you plan to run the play on hosts that don't haveapt-get
installed (e.g. anything RHEL-based), otherwise they will error out in the first play.This solution works, but is the lowest on my list for a few reasons:
apt
is on the system and ignores errors (as opposed to option 3)apt-get
commands are slow (as opposed to option 3)Option 3 - Symlink
/usr/bin/python -> /usr/bin/python3
usingraw
I haven't seen this solution proposed by anyone else. It's not ideal, but I think it's superior to option 2 in a lot of ways. My suggestion is to use
raw
to run a shell command to symlink/usr/bin/python -> /usr/bin/python3
ifpython3
is on the system andpython
is not:This solution is similar to option 2 in that we need to put it at the top of every playbook, but I think it's superior in a few ways:
python3
is present andpython
is not -- it won't override Python 2 if it's already installedapt
is installedapt-get
Obviously if you need Python 2 installed at
/usr/bin/python
, this solution is a no go and option 2 is better.Conclusion
python3
, making option 1 much more difficult and error-prone./usr/bin/python
.Sources
/usr/bin/python: not found
error in Ansibleraw
Module on Ansible DocsSolution 1:
If you're using
Ansible >2.2.0
, you can set theansible_python_interpreter
configuration option to/usr/bin/python3
:or in your inventory file:
Solution 2:
If you're using
Ansible <2.2.0
then you can add thesepre_tasks
to your playbook: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)I was able to fix the same problem by installing Python on target machine i.e. the machine which we want to SSH to. I had used following command: