Two Ansible Nodes – Ubuntu 18.04 LTS (192.168.1.20) & CentOS 7 (192.168.1.30)Īnsible package is not available in default CentOS 8 package repository.Minimal CentOS 8 / RHEL 8 Server (192.168.1.10) with Internet Connectivity.Once our bootstrap play has completed, we can be sure that Python is available and we may continue to provision our server using any of Ansible's wealth of modules. Using Ansible's import_playbook directive, we can now import this play into any playbook:. Call the bootstrap play from our main playbook The yum command after the ||, on the other hand, is only run when the test evaluates to false, meaning the apt command is not available (which only happens on CentOS systems). This works by first testing for the presence of the /usr/bin/apt command which is only present on Ubuntu systems.īy chaining subsequent commands with &, we make sure that they only run when this check evaluates to true.
Raw: test -e /usr/bin/apt & (apt -y update & apt install -y python-minimal) || (yum -y install python libselinux-python) If we need to support both CentOS and Ubuntu systems in a single play, we can apply this tasks for both operating systems in one fell swoop: - name: Install Python Raw: apt -y update & apt install -y python-minimal On Ubuntu systems we use the apt command (and we have to update its cache): - name: Install Python
On CentOS systems we use yum: - name: Install Python How we install Python depends on whether our server runs CentOS or Ubuntu. We use this result to judge whether Python's installation is necessary in the next task. We save this tasks's result using Ansible's register directive. This works because only the Python 2 binary is called python (Python 3's interpreter is called python3). This task runs the command test -e /usr/bin/python on our server which checks for the /usr/bin/python binary's existence. So the first thing we do is checking whether Python is already installed or not: - name: Check for Python
Just because we do not have Python at our disposal yet does not mean that we cannot keep our tasks idempotent - we should always do that. Fact gathering relies on Python so our play will crash if it is not already installed.Įven though Ansible claims to be compatible with Python 3, some modules still have glitches - it is safer to use Ansible 2. We need to turn off Ansible's fact gathering with gather_facts: false.We need to run its tasks as root (even if we plan to provision our server with a different user) because our server has to be considered fresh out of the box and therefore probably has no other users yet.It makes sense to create a dedicated play for bootstrapping (we may even call it bootstrap.yml), for two reasons: Manually installing Python is error prone and - if we need to do it regularly or on multiple machines - boring as hell.įortunately, it is possible to use Ansible to install Python on our server even when it actually depends on it - Ansible provides the raw module which can run basic low-down SSH commands on our server even if Python is not available. Watching Ansible provision a server from top to bottom and then deploy our Rails application is not onlyincredibly rewarding but also a safety net in case of desaster: It means that we can get back to a working state in a matter of minutes.īefore Ansible can take over, a Python interpreter must be present on the target machine. Call the bootstrap play from our main playbook.