How to fix missing Python for Ansible in Fedora Vagrant

Recently, I started to use Vagrant to test Ansible playbooks on Fedora machines. I’m using the Fedora 28 cloud base image. However, when I tried to provision my Vagrant box, I was warned the Python binary is missing.

$ vagrant provision
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
fatal: [default]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 192.168.121.3 closed.\r\n", "module_stdout": "\r\n/bin/sh: /usr/bin/python: No such file or directory\r\n", "msg": "MODULE FAILURE", "rc": 127}
	to retry, use: --limit @playbook.retry

Problem: Python 3 by default

This error appears because Fedora 28 does not provide a Python 2 binary by default. Only Python 3 is provided on the base cloud image. I verified this by SSHing into the Vagrant box.

[jflory@vagrant-host vagrant]$ vagrant ssh
[vagrant@localhost ~]$ dnf list installed | grep -i python

Annoyingly, I must install Python 2 manually in the box each time it fails to provision. Surely, there is an easier way? Fortunately, StackOverflow came to the rescue.

Solution: ansible.extra_vars

It’s possible to tell Vagrant where the Python binary is located. You can pass the path to the python3 binary manually in your Vagrantfile.

# Provisioning configuration for Ansible.
config.vm.provision :ansible do |ansible|
  ansible.playbook = "playbook.yml"
  ansible.extra_vars = { ansible_python_interpreter:"/usr/bin/python3" }
end

Adding these changes to your Vagrantfile allows Ansible to successfully run on the Fedora Vagrant guest. Python is successfully located.

This is an annoying workaround, but it solves the issue and lets you successfully test and iterate changes on Fedora systems. Here’s hoping the Fedora cloud image maintainers add a default binary for /usr/bin/python to point to /usr/bin/python3 in the future.

4 Comments

Matthew R Rose 2018 September 18 Reply

This `Fedora cloud image maintainers add a default binary for /usr/bin/python to point to /usr/bin/python3 in the future.` is the wrong solution. If Ansible is compatible with python3, it should try python3 by default. “python” is reserved for python2 according to python.

If the Fedora maintainers adopted your solution, it would break when installing python2.

Justin W. Flory 2018 September 18 Reply

I learned this will eventually happen, but not until Fedora 32. Vagrant does not yet seem to support a Python 3 Ansible, despite there being a ansible-python3 package. An alternative solution would be to set up Ansible in the Vagrant VM to alias /usr/bin/ansible to /usr/bin/ansible-3.

Sorin Ionuț Sbârnea 2018 September 19 Reply

Ansible is compatible with both py2 and py3 and because is a configuration management tool it will have to be compatible for a good number of years even after the 2020, as there are lots of embedded systems that will never have python interpreter upgraded.

Also the fact that a user would have to add extra configuration to specify the interpreter based on the OS-version mixed of the “controlled decide” seems more like a hack to me than a solution.

This is why I raised https://bugzilla.redhat.com/show_bug.cgi?id=1630882 which asks for having a symlink from python to python3 on Fedora when there is only one interpreter installed. This should apply to Fedora 27 and newer. If you agree with this please express your support on the bug, even if at this moment is closed (this may chance fast based on the community feedback).

Justin W. Flory 2018 September 20 Reply

Thanks for taking this discussion upstream! I saw the support on your RFE on the Ansible project. It will be great if that makes it in for 2.8. I’ll also comment on the bug as well.

Drop a line

Translate

@jwf_foss on Twitter

My Tweets

Social media

Top Posts

Recent Posts

Categories

Archives

@jwf_foss on Twitter

My Tweets

Social media

Top Posts

Recent Posts

Categories

Archives

Exit mobile version