Installation d'une stack LAMP sur Raspberry-Pi avec Ansible

Installation automatisée d'une stack LAMP sur un Raspberry-Pi [Raspbian] avec Ansible.

Publié le 25/11/2016

Installation automatisée d'une stack LAMP sur un Raspberry-Pi [Raspbian] avec Ansible.

La procédure fonctionne également sur Debian Jessie à condition d'installer le paquet sudo.

Installation des dépendances nécessaires


sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install ansible sshpass

Paramétrage utilisateur [Debian]

Ajout de l'utilisateur aux sudoers si différent de l'utilisateur pi

nano /etc/sudoers notre_utilisateur ALL=(ALL) NOPASSWD:ALL

Paramétrage d'Ansible

Création d'un dossier qui va contenir la configuration de notre hôte


cd
mkdir webapp

Ce dossier va contenir les fichiers suivants :

  • - ansible.cfg
  • - hosts
  • - playbook.yml
  • - virtualhost.conf

Création du fichier ansible.cfg


cd webapp
nano ansible.cfg


[defaults]
hostfile = ./hosts
host_key_checking = False

Création du fichier hosts

nano hosts raspberry-pi ansible_ssh_host=192.168.0.x ansible_ssh_user=pi ansible_ssh_pass=raspberry host_key_checking=false

Création du fichier playbook.yml

nano playbook.yml


---
- hosts: all
  sudo: true
  vars:
    domain: webapp.local
    admin: contact@domain.tld
    directory: webapp/web
  handlers:
      - name: restart apache2
        service: name=apache2 state=restarted
  tasks:
    - name: Mise à jour de l'apt cache
      apt: update_cache=yes
    - name: installer aptitude
      apt: name=aptitude state=present
    - name: Mise à jour système
      apt: upgrade=full

    - name: installer curl
      apt: name=curl state=present

    - name: installer python-mysqldb
      apt: name=python-mysqldb state=present

    - name: installer htop
      apt: name=htop state=present

    - name: installer zip
      apt: name=zip state=present

    - name: installer iotop
      apt: name=iotop state=present

    - name: installer iptraf
      apt: name=iptraf state=present

    - name: installer git
      apt: name=git state=present

    - name: installer apache2
      apt: name=apache2 state=present

    - name: installer apache2-doc
      apt: name=apache2-doc state=present

    - name: installer apache2-mpm-prefork
      apt: name=apache2-mpm-prefork state=present

    - name: installer apache2-utils
      apt: name=apache2-utils state=present

    - name: installer libexpat1
      apt: name=libexpat1 state=present

    - name: installer ssl-cert
      apt: name=ssl-cert state=present

    - name: create webapp vhost
      template: src=virtualhost.conf dest=/etc/apache2/sites-available/{{ domain }}.conf

    - name: a2ensite webapp
      command: a2ensite {{ domain }}
      args:
        creates: /etc/apache2/site-enabled/{{ domain }}.conf
      notify:
        - restart apache2

    - name: activation mod-rewrite
      apache2_module: name=rewrite state=present
      notify:
        - restart apache2

    - name: installer PHP
      apt: name=php5 state=present
    - name: installer libapache2-mod-php5
      apt: name=libapache2-mod-php5 state=present
    - name: installer php5-common
      apt: name=php5-common state=present
    - name: installer php5-curl
      apt: name=php5-curl state=present
    - name: installer PHP php5-dev
      apt: name=php5-dev state=present
    - name: installer php5-gd
      apt: name=php5-gd state=present
    - name: installer php5-intl
      apt: name=php5-intl state=present
    - name: installer php-pear
      apt: name=php-pear state=present
    - name: installer php5-imagick
      apt: name=php5-imagick state=present
    - name: installer php5-imap
      apt: name=php5-imap state=present
    - name: installer php5-json
      apt: name=php5-json state=present
    - name: installer php5-mcrypt
      apt: name=php5-mcrypt state=present
    - name: installer php5-memcache
      apt: name=php5-memcache state=present
    - name: installer php5-mysql
      apt: name=php5-mysql state=present
    - name: installer php5-pspell
      apt: name=php5-pspell state=present
    - name: installer php5-recode
      apt: name=php5-recode state=present
    - name: installer php5-xmlrpc
      apt: name=php5-xmlrpc state=present
    - name: installer PHP php5-xsl
      apt: name=php5-xsl state=present

    - name: Modify date.time zone php.ini apache
      replace:
        dest=/etc/php5/apache2/php.ini
        regexp=';date.timezone ='
        replace='date.time = Europe/Paris'
        backup=yes

    - name: Modify date.time zone php.ini apache
      replace:
        dest=/etc/php5/cli/php.ini
        regexp=';date.timezone ='
        replace='date.time = Europe/Paris'
        backup=yes

    - name: installer mysql
      apt: name=mysql-server state=present

    - name: Add custom mysql config collation-server
      lineinfile:
        dest=/etc/mysql/my.cnf
        insertafter=EOF
        line="collation-server = utf8_general_ci"

    - name: Add custom mysql config character-set-server
      lineinfile:
        dest=/etc/mysql/my.cnf
        insertafter=EOF
        line="character-set-server = utf8"

    - name: restart mysql
      service: name=mysql state=restarted

    - name: create webapp db
      mysql_db:
        name=webapp
        state=present

Création du fichier virtualhost.conf

nano virtualhost.conf


<VirtualHost *:80>
        ServerName {{ domain }}
        ServerAdmin {{ admin }}

        DocumentRoot /var/www/{{ directory }}
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/{{ directory }}/>
                DirectoryIndex app.php
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Exécution du playbook

ansible-playbook playbook.yml