Ansible is great tool to automate the configuration of your servers. In this post I will focus on configuring a Linux web server running Apache, PHP and MariaDB – which is also know as a LAMP server.

The first playbook will be fairly basic. We just want it to install our services and start them. It’s a plain vanilla setup that will cover all basic LAMP server needs. The second playbook will cover more advanced configurations.

Basic Playbook

We will start off building a basic playbook. It is good base to start off with before going onto creating more advanced playbooks. For now, the scope will be limited to the following.

  • Install required packages to run a LAMP server.
  • Ensure the installed packages are the latest available.

Below is snippet of the initial playbook that will be used. As you can see, all it is capable of doing is installed PHP, MariaDB and Apache. It’s also target at the localhost.

---
- hosts: localhost
  tasks:
    - name: Install Apache
      yum: name=httpd state=latest
    - name: Install MariaDB
      yum: name=mariadb-server state=latest
    - name: Install PHP
      yum: name=php state=latest

Using Handlers to Start Services

Its great that our packages are installed, but in most cases we want the services to be running and to start automatically at boot. We can accomplish this by adding handlers. Below the tasks section of the playbook, add your handles. The name of the handlers will be used by the tasks when notify is invoked.

---
- hosts: localhost
  tasks:
    - name: Install Apache
      yum: name=httpd state=latest
      notify:
       - Start Apache
    - name: Install MariaDB
      yum: name=mariadb-server state=latest
      notify:
       - Start MariaDB
    - name: Install PHP
      yum: name=php state=latest

  handlers:
    - name: Start Apache
      service: name=httpd state=started enabled=yes
    - name: Start MariaDB
      service: name=mariadb state=started enabled=yes

The Output

If all went well, you will see the following output when you apply the playbook against your server. Your server is now ready to host a PHP based web application

[superuser@webnode01 ~]# ansible-playbook myplaybook.yml

PLAY [localhost] **************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [Install Apache] ********************************************************
changed: [localhost]

TASK: [Install MariaDB] *******************************************************
changed: [localhost]

TASK: [Install PHP] ***********************************************************
changed: [localhost]

NOTIFIED: [Start Apache] ******************************************************
changed: [localhost]

NOTIFIED: [Start MariaDB] *****************************************************
changed: [localhost]

PLAY RECAP ********************************************************************
localhost                  : ok=6    changed=5    unreachable=0    failed=0

Tags: , , ,