Ansible is a simple IT automation platform written in python that makes your applications and systems easier to deploy. It has become quite popular over the past few years but you may hit some trouble when trying to run Ansible on Fedora 23.
Fedora 23 is now using Python 3 as the default python version that gets installed, but Ansible still requires Python 2. Because Ansible still assumes Python 2 by default, errors similar to the following may be returned when trying to use Ansible on Fedora 23:
GATHERING FACTS * failed: [f23] => {"failed": true, "parsed": false} /bin/sh: /usr/bin/python: No such file or directory
Fortunately, there are a few steps you can add to your playbooks in order to fully workaround this problem. You can either choose to apply them in a single play or in multiple plays as shown below.
Workaround – Single All-in-One Play
In the case of a single play, which is something I use often when applying configuration to vagrant boxes, you can workaround this problem by taking the following steps:
- Explicitly disable the gathering of facts on initialization
- Use Ansible’s raw module to install python2
- Explicitly call the setup module to gather facts again
The gathering of facts that happens by default on ansible execution will try to use python 2. We must disable this or it will fail before executing the raw ssh commands to install python 2. Fortunately we can still use facts in our single play, though, by explicitly calling the setup module after python2 is installed.
So with these minor changes applied a simple all in one play might look like:
- hosts: f23 remote_user: fedora gather_facts: false become_user: root become: yes tasks: - name: install python and deps for ansible modules raw: dnf install -y python2 python2-dnf libselinux-python - name: gather facts setup: - name: use facts lineinfile: dest=/etc/some-cfg-file line="myip={{ ansible_eth0.ipv4.address }}" create=true
And the output of running the play should be successful:
PLAY [f23] **************************************************************** TASK: [install python and deps for ansible modules] *************************** ok: [f23] TASK: [gather facts] ********************************************************** ok: [f23] TASK: [use facts] ************************************************************* changed: [f23] PLAY RECAP ******************************************************************** f23 : ok=3 changed=1 unreachable=0 failed=0
Workaround – Multiple Plays
If you use multiple plays in your playbooks then you can simply have one of them do the python 2 install in raw mode while the others can remain unchanged; you don’t have to explicitly gather facts because python 2 is now installed. So for the first play you would have something like:
- hosts: f23 remote_user: fedora gather_facts: false become_user: root become: yes tasks: - name: install python and deps for ansible modules raw: dnf install -y python2 python2-dnf libselinux-python
And, re-using the code from the sample above the second play would look like:
- hosts: f23 remote_user: fedora become_user: root become: yes tasks: - name: use facts lineinfile: dest=/etc/some-cfg-file line="myip={{ ansible_eth0.ipv4.address }}" create=true
So using these small changes you should be back up and running until Ansible adds first class support for python 3.
Inspired mostly from this post by Lars Kellogg-Stedman.
Adam Williamson
If you’re using any kind of packaged deployment, of course, you can just make sure that python2 is installed in the first place. You only really need to do Clever Stuff like this if you’re using something like Atomic.
Dusty Mabe
Hey Adam,
This doesn’t really apply to Atomic since with Atomic you can’t install packages; we actually still include python2 in Atomic partly because of this inflexibility. It really applies to any case where you are starting from a pre-configured Fedora 23 environment and that environment does not have python2 installed. The most prominent example of this that I know of right now is the Fedora Cloud Base image.