DEV Community

Narongdej Sarnsuwan
Narongdej Sarnsuwan

Posted on

Upgrading PHP to 7.3 with Ansible

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.


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

pip install --user ansible
Enter fullscreen mode Exit fullscreen mode

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

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Enter fullscreen mode Exit fullscreen mode

For other distros or OS, check out the official installation tutorial

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

website-a ansible_host=122.X.X.X ansible_port=2222 ansible_user=someuser
website-b ansible_host=122.X.X.X
Enter fullscreen mode Exit fullscreen mode

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

- hosts: wordpress
    php_version: '7.3'
    php_versions_install_recommends: false
    - role: geerlingguy.repo-remi
      when: ansible_os_family == 'RedHat'
    - geerlingguy.php-versions
    - geerlingguy.php
Enter fullscreen mode Exit fullscreen mode

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

ansible-galaxy install geerlingguy.repo-remi
ansible-galaxy install geerlingguy.php-versions
ansible-galaxy install geerlingguy.php
Enter fullscreen mode Exit fullscreen mode

This awesome role by 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

Upgrade the server PHP

Finally, we are ready to upgrade PHP on our servers. Run this command:

ansible-playbook upgrade-php.yml
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

sanbabba profile image

where is playbook?