Installing PHP7 on Ubuntu with Ansible

In a bid to keep updated with the latest and greatest in PHP-land, I have been slowly updating my Ansible set-up to accommodate PHP7.

Fortunately, pretty much all of the hard work has been taken care of by Jeff Geerling, with his fantastic geerlingguy/php role. A mighty big thank you is in order.

But I’m writing this as I struggled with a particular issue getting my newly updated playbook to work properly.

The issue I had was that I had the following:

---
- hosts: php_servers
  sudo: True

  tasks:
    - name: "Add repository for PHP 7.0."
      apt_repository: 
        repo="ppa:ondrej/php" 
        update_cache=yes

  roles:
    - role: geerlingguy.php
      php_version: "7.0"
      php_packages:
        - php7.0-common
        - php7.0-cli
        - php7.0-dev
        - # snip
      php_date_timezone: "Europe/London"

And whilst everything looked good, whenever I ran the playbook:

ansible-playbook playbook/symfony-dev.yml -i hosts -l test-box.dev -k -K -s

The cheeky little blighter would not run the task to add the repository before it would try and run the roles content. Needless to say, red error text appeared in quantity.

Annoyingly, I could comment out the roles section, and the task would run – but as soon as I put the role definition back in, that would always run first.

I must admit, this was a new one on me.

However, the solution is incredibly simple:

---
- hosts: php_servers
  sudo: True

  pre_tasks:
    - name: "Add repository for PHP 7.0."
      apt_repository: 
        repo="ppa:ondrej/php" 
        update_cache=yes

  roles:
    - role: geerlingguy.php
      php_version: "7.0"
      php_packages:
        - php7.0-common
        - php7.0-cli
        - php7.0-dev
        - php7.0-fpm
        - libpcre3-dev
        - php7.0-gd
        - php7.0-curl
        - php7.0-imap
        - php7.0-json
        - php7.0-opcache
        - php7.0-xml
        - php7.0-mbstring
      php_date_timezone: "Europe/London"
      php_mysql_package: php7.0-mysql
      php_fpm_daemon: php7.0-fpm
      php_webserver_daemon: nginx
      php_conf_paths:
        - /etc/php/7.0/fpm
        - /etc/php/7.0/apache2
        - /etc/php/7.0/cli
      php_extension_conf_paths:
        - /etc/php/7.0/fpm/conf.d
        - /etc/php/7.0/apache2/conf.d
        - /etc/php/7.0/cli/conf.d
      php_fpm_pool_conf_path: "/etc/php/7.0/fpm/pool.d/www.conf"

Please note: this config is still very much a work in progress for me. I found this particular set of config from this ticket on Jeff Geerling’s Ansible Role PHP repo.

Yes, there are pre_tasks and post_tasks available – new ones on me, but right there in the docs. You may need to ctrl+f to find the exact examples, as I can’t directly link to the specific section unfortunately.

Very useful anyway, and with that, my task started behaving exactly as I wanted.

Ansible is absolutely awesome. If you haven’t already done so, be sure to check out my Ansible tutorial course here at Code Review Videos. It’s completely free.

 

 

Published by

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

One thought on “Installing PHP7 on Ubuntu with Ansible”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.