Upgrading PHP to 7.3 with Ansible
Tags: devops, php, ansible, sysadmin
Published: 2019-06-03 13:42:51

At my company, one of our services is to develop and maintain Wordpress websites for our clients. We host our clients' website using Digitalocean and we want to separate customer environment easily so we use different droplet for each customer. However, the pre-installed version from the DigitalOcean image is 7.0.X and it was deprecated.

## Save by Ansible
Ansible is an IT Automation tool by Redhat which lets you automate IT tasks with ease. And did I mention, **its free** 

While you can do a load of things using Ansible, we will only focus on upgrading PHP on our servers. Be sure to check out what you can do with ansible afterward.

## Installation

First, you gotta install Ansible control node. On a bigger scale, this would be on a server who manages other ansible nodes (other servers) through SSH. However, for our case; we will use our own computer as a control node.

Ansible works on any machine that can run Python 2 or 3, **except for Windows**. Use the Linux subsystem or something else if you are running Windows.

For Mac users, the preferred way is to install ansible using pip

```bash
pip install --user ansible
```


If you are using Ubuntu 16.04 or 18.04 run the command below

```bash
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
```

For other distros or OS, check out [the official installation tutorial](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-the-control-node)

## Setup your inventory
In Ansible, you identify the location of your servers in the inventory file located at `/etc/ansible/hosts`

Create your inventory file

```
[wordpress]
website-a ansible_host=122.X.X.X ansible_port=2222 ansible_user=someuser
website-b ansible_host=122.X.X.X
```
Above, you created a hosts group called *wordpress* which contains website-a and website-b

You can also set *ansible_password=* but I recommended you to set up the public key

## Create your playbook

Next up we'll create an orchestration file to tell ansible what to do. Create a file name ``upgrade-php.yml``

```yaml
- hosts: wordpress
  vars:
    php_version: '7.3'
    php_versions_install_recommends: false
  roles:
    - role: geerlingguy.repo-remi
      when: ansible_os_family == 'RedHat'
    - geerlingguy.php-versions
    - geerlingguy.php
```

What happening here is we're telling ansible to run set of tasks (roles) geerlingguy.php-versions, and geerlingguy.php.

Just like NPM or pip, you need to download the package from an online repo in order to use it. Run these commands

```shell
ansible-galaxy install geerlingguy.repo-remi
ansible-galaxy install geerlingguy.php-versions
ansible-galaxy install geerlingguy.php
```

This awesome role by [geerlingguy](https://github.com/geerlingguy) purge old PHP versions, add the appropriate repository, install your PHP and install all the recommendations modules. You can review what the role does [here](https://github.com/geerlingguy/ansible-role-php-versions/blob/master/tasks/main.yml)

## Upgrade the server PHP
Finally, we are ready to upgrade PHP on our servers. Run this command:
```shell
ansible-playbook upgrade-php.yml
```

Wait for the playbook to finish and your PHP version should be upgraded to 7.3 🎉🎉🎉

Ansible has a lot more awesome features that make your sysadmin mundane tasks go away in one command. I'm new to ansible and wish to have known about it sooner. If you have any cool ansible project, then please tell me about it in the comment.