DevOps & Chill n.3: Creating a LEMP Playbook with Ansible

erikaheidi

Erika Heidi

Posted on April 13, 2020

DevOps & Chill n.3: Creating a LEMP Playbook with Ansible

In the third episode of DevOps&Chill, we create an Ansible playbook to automate the steps necessary to install and set up a LEMP server on Ubunbtu 18.04, using a couple DigitalOcean droplets as nodes.

This video was edited from the original stream in order to be more succinct and in a faster pace.

The Playbook

The final playbook we created in this video is included below for easier access:

---
- hosts: all
  become: yes
  vars:
    - install_packages: [ "nginx", "php-fpm", "mysql-server", "python3-pymysql" ]
    - http_port: 80
    - http_host: "{{ ansible_facts.eth0.ipv4.address }}"
  tasks:
    - name: Update cache and Install Required Packages
      apt:
        name: "{{ install_packages }}"
        state: latest
        update_cache: yes

    - name: Removes anonymous MySQL accounts
      mysql_user:
        name: ''
        host_all: yes
        state: absent
        login_unix_socket: /var/run/mysqld/mysqld.sock
      no_log: true

    - name: Allow access on port 80
      ufw:
        rule: allow
        port: '80'
        proto: tcp

    - name: Set Up Nginx default site
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: restart nginx

    - name: Set Up php test page
      template:
        src: phpinfo.php.j2
        dest: /var/www/html/info.php

  handlers:
    - name: restart nginx 
      service:
        name: nginx
        state: restarted

Enter fullscreen mode Exit fullscreen mode

To run this playbook, you'll need an Ansible Control Node set up with at least one managed node in your inventory file. Then, you can execute this with the following command:

ansible-playbook -i hosts lemp-playbook.yml -u USER
Enter fullscreen mode Exit fullscreen mode

Where hosts is your inventory file name, and USER is the remote user you're using to connect via SSH on the remote server and run the Ansible commands. This should be either a sudo user or root.

Feel free to use the comments section to ask question and to suggest new topics for DevOps&Chill. Cheers!

💖 💪 🙅 🚩
erikaheidi
Erika Heidi

Posted on April 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related