Drupal sur plateforme LAMP sur Ubuntu

Source

Source : https://github.com/goffinet/guide-ansible-playbooks/tree/master/ansible-linux/11-lamp-drupal-ubuntu

1. Défintion du jeu

---
- hosts: all
  become: yes

  vars_files:
    - vars.yml

  pre_tasks:
    - name: Update apt cache if needed.
      apt: update_cache=yes cache_valid_time=3600

  handlers:
    - name: restart apache
      service: name=apache2 state=restarted

2. Variables

Le livre de jeu est accompagné d’un fichier vars.yml qui contient des variables :

---
# The core version you want to use (e.g. 8.5.x, 8.6.x).
drupal_core_version: "8.6.x"

# The path where Drupal will be downloaded and installed.
drupal_core_path: "/var/www/drupal-{{ drupal_core_version }}-dev"

# The resulting domain will be [domain].test (with .test appended).
domain: "{{ inventory_hostname }}"

# Your Drupal site name.
drupal_site_name: "Drupal Test"

3. Pré-tâches (pre_tasks)

Le cache apt sera mis-à-jour avant l’exécution de toute tâche.

---
- hosts: all
  pre_tasks:
    - name: Update apt cache if needed.
      apt: update_cache=yes cache_valid_time=3600

4. Gestionnaires (handlers)

Un “handler” est une tâche qui s’exécute sur un appel “notify” mais une seule fois dans le jeu.

---
- hosts: all
  handlers:
    - name: restart apache
      service: name=apache2 state=restarted
  tasks:
    - name: Enable Apache rewrite module (required for Drupal).
      apache2_module: name=rewrite state=present
      notify: restart apache

5. Mise en forme

Remise en forme de l’indentation et placement des guillemets :

    - name: Add ondrej repository for later versions of PHP.
      apt_repository: repo='ppa:ondrej/php' update_cache=yes

en :

    - name: "Add ondrej repository for later versions of PHP."
      apt_repository:
        repo: 'ppa:ondrej/php'
        update_cache: yes

6. Boucles dans des listes

Correction : la boucle with_item est dépréciée au profit de la boucle loop.

On peut appeler une variable valorisée en liste dans l’exécution d’un module :

- name: "loop illustration"
  hosts: localhost
  vars:
    fruits:
      - apple
      - orange
      - pineapple   
  tasks:
    - name: "Print my Fruits"
      debug:
        msg: "{{ item }}"
      loop: "{{ fruits }}"

Il serait intéressant de mettre en variable toutes les listes qui sont dans les tâches.

7. Module APT

Notons que le module apt supporte directement les listes sans passer par une boucle.

[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via squash_actions is deprecated. Instead of
using a loop to supply multiple items and specifying `name: {{ item }}`, please use `name: [u'python-apt', u
'python-pycurl']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be
disabled by setting deprecation_warnings=False in ansible.cfg.

8. Register

Using register with a loop

---
- name: "loop with register"
  hosts: localhost
  vars:
    fruits:
      - apple
      - orange
      - pineapple   
  tasks:
    - name: "loop + register"
      shell: "echo {{ item }}"
      loop: "{{ fruits }}"
      register: echo
    - name: "print result"
      debug:
        msg: "{{ echo }}"

Mieux :

---
- name: "loop with register"
  hosts: localhost
  vars:
    fruits:
      - apple
      - orange
      - pineapple
  tasks:
    - name: "loop register"
      shell: "echo {{ item }}"
      loop: "{{ fruits }}"
      register: echo
    - name: "print result"
      debug:
        msg: "{{ item.stdout_lines }}"
      loop: "{{ echo.results }}"

10. Templates

On trouvera un seul modèle Jinja2 dans templates/drupal.test.conf.j2 assez simple :

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName {{ domain }}.test
    ServerAlias www.{{ domain }}.test
    DocumentRoot {{ drupal_core_path }}
    <Directory "{{ drupal_core_path }}">
        Options FollowSymLinks Indexes
        AllowOverride All
    </Directory>
</VirtualHost>

11. Liste de tâches

  • Installation LAMP de base
  • Configuration d’Apache
  • Configuration de PHP
  • Configuration de MYSQL
  • Installation de Composer et de Drush
  • Installation de drupal avec Git et Drush

1. Installation LAMP de base

    - name: "Get software for apt repository management."
      apt:
        name:
          - python-apt
          - python-pycurl
        state: present
    - name: "Add ondrej repository for later versions of PHP."
      apt_repository:
        repo: 'ppa:ondrej/php'
        update_cache: yes
    - name: "Install Apache, MySQL, PHP, and other dependencies."
      apt:
        name:
          - git
          - curl
          - unzip
          - sendmail
          - apache2
          - php7.1-common
          - php7.1-cli
          - php7.1-dev
          - php7.1-gd
          - php7.1-curl
          - php7.1-json
          - php7.1-opcache
          - php7.1-xml
          - php7.1-mbstring
          - php7.1-pdo
          - php7.1-mysql
          - php-apcu
          - libpcre3-dev
          - libapache2-mod-php7.1
          - python-mysqldb
          - mysql-server
          - ufw
        state: present
    - name: "Disable the firewall (since this is for local dev only)."
      service:
        name: ufw
        state: stopped
    - name: "Start Apache, MySQL, and PHP."
      service:
        name: "{{ item }}"
        state: started
        enabled: yes
      loop:
        - apache2
        - mysql

2. Configuration d’Apache

Illustration de :

    - name: "Enable Apache rewrite module (required for Drupal)."
      apache2_module:
        name: rewrite
        state: present
      notify: "restart apache"
    - name: "Add Apache virtualhost for Drupal 8."
      template:
        src: "templates/drupal.test.conf.j2"
        dest: "/etc/apache2/sites-available/{{ domain }}.test.conf"
        owner: root
        group: root
        mode: 0644
      notify: "restart apache"
    - name: "Symlink Drupal virtualhost to sites-enabled."
      file:
        src: "/etc/apache2/sites-available/{{ domain }}.test.conf"
        dest: "/etc/apache2/sites-enabled/{{ domain }}.test.conf"
        state: link
      notify: "restart apache"
    - name: "Remove default virtualhost file."
      file:
        path: "/etc/apache2/sites-enabled/000-default.conf"
        state: absent
      notify: "restart apache"

3. Configuration de PHP

Exemple d’utilisation du module lineinfile - Manage lines in text files.

    - name: "Adjust OpCache memory setting."
      lineinfile:
        dest: "/etc/php/7.1/apache2/conf.d/10-opcache.ini"
        regexp: "^opcache.memory_consumption"
        line: "opcache.memory_consumption = 96"
        state: present
      notify: "restart apache"

4. Configuration de MYSQL

Exemple d’usage du module mysql_db - Add or remove MySQL databases from a remote host et du module mysql_user - Adds or removes a user from a MySQL database

    - name: Remove the MySQL test database.
      mysql_db:
        db: test
        state: absent
    - name: "Create a MySQL database for Drupal."
      mysql_db:
        db: "{{ domain }}"
        state: present
    - name: "Create a MySQL user for Drupal."
      mysql_user:
        name: "{{ domain }}"
        password: "1234"
        priv: "{{ domain }}.*:ALL"
        host: localhost
        state: present

5. Installation de Composer et de Drush

Illustration :

On utilise le module command - Executes a command on a remote node qui exécute un script php et le module shell - Execute commands in nodes pour exécuter un binaire du shell (ici mv).

Installation de composer qui est nécessaire à Drus, l’outil cli de Drupal.

    - name: "Download Composer installer."
      get_url:
        url: https://getcomposer.org/installer
        dest: /tmp/composer-installer.php
        mode: 0755
    - name: "Run Composer installer."
      command: >
        php composer-installer.php
        chdir=/tmp
        creates=/usr/local/bin/composer
    - name: "Move Composer into globally-accessible location."
      shell: >
        mv /tmp/composer.phar /usr/local/bin/composer
        creates=/usr/local/bin/composer

Dans la suite, on installe Drush, notamment avec le module git - Deploy software (or files) from git checkouts.

    - name: "Check out drush 8.x branch."
      git:
        repo: https://github.com/drush-ops/drush.git
        version: 8.x
        dest: /opt/drush
    - name: "Install Drush dependencies with Composer."
      shell: >
        /usr/local/bin/composer install
        chdir=/opt/drush
        creates=/opt/drush/vendor/autoload.php
    - name: "Create drush bin symlink."
      file:
        src: /opt/drush/drush
        dest: /usr/local/bin/drush
        state: link

6. Installation de drupal avec Git et Drush

    - name: "Check out Drupal Core to the Apache docroot."
      git:
        repo: https://git.drupal.org/project/drupal.git
        version: "{{ drupal_core_version }}"
        dest: "{{ drupal_core_path }}"
    - name: "Install Drupal dependencies with Composer."
      shell: >
        /usr/local/bin/composer install
        chdir={{ drupal_core_path }}
        creates={{ drupal_core_path }}/vendor/autoload.php
    - name: "Install Drupal."
      command: >
        drush si -y --site-name="{{ drupal_site_name }}"
        --account-name=admin
        --account-pass=admin
        --db-url=mysql://{{ domain }}:1234@localhost/{{ domain }}
        --root={{ drupal_core_path }}
        creates={{ drupal_core_path }}/sites/default/settings.php
      notify: restart apache
    # SEE: https://drupal.org/node/2121849#comment-8413637
    - name: "Set permissions properly on settings.php."
      file:
        path: "{{ drupal_core_path }}/sites/default/settings.php"
        mode: 0744
    - name: "Set permissions properly on files directory."
      file:
        path: "{{ drupal_core_path }}/sites/default/files"
        mode: 0777
        state: directory
        recurse: yes

7. Exercice