2

I am writing my first ansible playbook and try to configure php 7 by using already existing roles. I found 2 roles that nearly suit my needs but both of them do not offer the flexibility I want for creating php.ini files.

  1. https://github.com/itcraftsmanpl/ansible-role-php7

This role uses a task called php-fpm.yml to change lines in a already existing php.ini file using

- name: Ensure timezone is set in fpm php.ini
  lineinfile: dest=/etc/php/7.1/fpm/php.ini
              regexp='date.timezone ='
              line='date.timezone = {{ php_timezone }}'
  1. https://github.com/geerlingguy/ansible-role-php/

This role uses a template for generating the php.ini and inserts certain predefined variables to make it configurable.

date.timezone = {{ php_date_timezone }}

Question:

Is it possible to add a new configuration directive (let's say for setting mysqli.max_persistent=XYZ) without overwriting the files of the above mentioned roles? Or do I have to stick to those config options the roles provide?

How should I in general extend a already existing role without tampering the file base of the role?

My current playbook.yml is as simple as this:

- name: Install PHP 7
  hosts: all
  become: yes

  roles:
   - ansible-role-php7
maxhb
  • 8,554
  • 9
  • 29
  • 53

2 Answers2

4

You can make use of include_role in your implementation.

For example /roles/myphp/tasks/main.yml:

- include_role:
    name: ansible-role-php7

- lineinfile:
    dest: /etc/php/7.1/fpm/php.ini
    regexp: 'mysqli.max_persistent ='
    line: 'mysqli.max_persistent = {{ mysqli_max_persistent }}'
  notify: restart php7-fpm
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
2

I had the same problem and I also solved this by using include_role in my custom php role. The main php role is installed using ansible-galaxy, so my playbook structure looks like this:

roles/
  php/
    defaults/
      main.yml
    templates/
      xdebug_config.ini.j2
    tasks:
      main.yml
requirements.yml
site.yml

In the requirements.yml file I specify all galaxy roles to install:

- src: geerlingguy.php

I then install the required role by executing:

ansible-galaxy install -r requirements.yml

In my site.yml I then use my own role which extends the geerlingguy.php role:

---
# Sample playbook

- name: "Development playbook"
  hosts: all
  become: yes
  roles:
    - role: php
      tags: php

In the roles/php/tasks/main.yml we then use include_role and setup our extended tasks for this role:

---
# Extends the geerlingguy.php role.

- name: Include base php role.
  include_role:
    name: geerlingguy.php

- name: Setup xdebug configuration.
  template:
    src: xdebug_config.ini.j2
    dest: /etc/php/7.0/fpm/conf.d/xdebug_config.ini
    mode: 0777

You can use a ansible.cfg file to make sure that ansible can find the roles installed using ansible-galaxy, for instance:

[defaults]
roles_path=/etc/ansible/roles
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93