1

CentOS 8 does not always come with Python pre-installed, and so Ansible will fail running on the remote machine until it's been installed. However in a classic Chicken/Egg, you can't use the Ansible dnf module to install Python.

I've been using:

- name: Install Python 3
  raw: dnf -y install python3

However the problem with this is that I either have to set changed_when: false or it will always return a changed state. I'd like the state reported properly if it's possible.

I found easy_install however this appears to only deal with Python Libraries, and not Python itself. Is there a built-in way to handle this or is raw: the only option?

oucil
  • 557
  • 6
  • 21

2 Answers2

2

Yes, the raw module is the preferred way to install Python with Ansible. You might want to include some other necessary packages for Ansible as well:

- name: Bootstrap a host without python2 installed
  raw: dnf install -y python2 python2-dnf libselinux-python

easy_install depends on Python. There is no way around raw when Python is not present. Usually I use this raw task as part of a special bootstrapping playbook executed only once. Another reason to define this task outside the other roles and plays is that you cannot use fact gathering when Python is not present on the target system.

Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
0

Borrowing a trick from this blog post, if you pass raw a command that includes a test for the package you are installing, then you can properly set the Ansible changed status.

- name: Bootstrap a host without python2 installed
  raw: bash -c "test -e /usr/bin/python2 || (dnf install -y python2 python2-dnf libselinux-python)"
  register: output
  changed_when: output.stdout

If /usr/bin/python2 exists, then output.stdout will be blank and Ansible will report the task as "ok".

If /usr/bin/python2 does not exist, then dnf will install the packages generating a bunch of output and output.stdout will not be blank causing Ansible to report "changed".

Not sure if /usr/bin/python2 is the right file to be looking for but hopefully you get the idea.

PaulR
  • 331
  • 3
  • 8