Automating LAMP Stack Installation and Uninstallation with Ansible

 

 Installation and uninstallation of a LAMP (Linux, Apache, MySQL, PHP) stack using Ansible. Ansible is an open-source automation tool that allows you to define and manage infrastructure as code.

 


 

The LAMP stack is a popular software bundle used to deploy web applications. It consists of the following components:

  • Linux: The operating system (in this case, we’re using an Amazon Linux instance).
  • Apache: The web server responsible for serving web content.
  • MySQL/MariaDB: The relational database management system for storing application data.
  • PHP: The server-side scripting language for dynamic web content.

We’ll use Ansible to automate the installation and configuration of these components.

 

Prerequisites

  • An ec2/vm/compute instance with Ansible installed.
  • Access to the AWS/Azure/GCP Secrets Manager service.

 

complete playbook

 

---
- name: "Installing LAMP stack and configuring"
hosts: amazon
become: true
vars_files:
- httpd.vars
- mariadb.vars
- packages.vars
tasks:
# Task 1: Installing Packages
- name: "Installing Packages"
yum:
name: "{{ packages }}"
state: present
tags:
- lamp
- never

# Task 2: Enabling/Restarting services
- name: "Enabling/Restarting services"
service:
name: "{{ item }}"
state: restarted
enabled: true
with_items: "{{ services }}"
tags:
- lamp
- never

# Task 3: Creating httpd.conf from template
- name: "Creating httpd.conf from httpd.conf.tmpl"
template:
src: httpd.conf.tmpl
dest: /etc/httpd/conf/httpd.conf
tags:
- lamp
- never

# Task 4: Creating Virtual host
- name: "Creating Virtual host"
template:
src: virtualhost.conf.tmpl
dest: /etc/httpd/conf.d/default.conf
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
tags:
- lamp
- never

# Task 5: Creating docroot for the website
- name: "Creating docroot for {{ hostname }}"
file:
path: "/var/www/html/default/"
state: directory
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
tags:
- lamp
- never

# Task 6: Setting python3 mysql module PyMySQL
- name: "Setting python3 mysql module PyMySQL"
pip:
name: PyMySQL
tags:
- lamp
- mariadb
- never

# Task 7: Setting ROOT password for mariadb-server
- name: "Setting ROOT password for mariadb-server"
ignore_errors: true
mysql_user:
login_user: "root"
login_password: ""
login_unix_socket: /var/lib/mysql/mysql.sock
name: "root"
password: "{{ mariadb_root_password }}"
tags:
- lamp
- mariadb
- never

# Task 8: Creating DB for the website
- name: "Creating DB for blog - {{blog_db_name}}"
mysql_db:
login_user: "root"
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /var/lib/mysql/mysql.sock
name: "{{ blog_db_name }}"
state: present
tags:
- lamp
- mariadb
- never

# Task 9: Creating extra user for WordPress
- name: "Creating extra user for WordPress - {{extra_user_name}}"
mysql_user:
login_user: "root"
login_password: "{{ mariadb_root_password }}"
login_unix_socket: /var/lib/mysql/mysql.sock
name: "{{ extra_user_name }}"
password: "{{ extra_user_password }}"
host: "%"
priv: "{{ blog_db_name }}.*:ALL"
tags:
- lamp
- never
- mariadb

# Task 10: Downloading WordPress
- name: "Downloading WordPress using URL"
get_url:
url: "{{ wp_url }}"
dest: "/tmp/wordpress.tar.gz"
tags:
- lamp
- mariadb
- wordpress
- never

# Task 11: Extracting WordPress
- name: "Extracting WordPress"
unarchive:
src: "/tmp/wordpress.tar.gz"
dest: "/tmp/"
remote_src: true
tags:
- lamp
- mariadb
- wordpress
- never

# Task 12: Copying WordPress files to docroot
- name: "Copying WordPress files to docroot"
copy:
src: "/tmp/wordpress/"
dest: "/var/www/html/default/"
remote_src: true
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
tags:
- lamp
- mariadb
- wordpress
- never

# Task 13: Copying wp-config.php file to remote server
- name: "Copying wp-config.php file to remote server"
template:
src: "./wp-config.php.tmpl"
dest: "/var/www/html/default/wp-config.php"
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
tags:
- lamp
- mariadb
- wordpress
- never

# Task 14: Post Installation Cleanup
- name: "Post Installation Cleanup"
file:
path: "{{ item }}"
state: absent
with_items:
- "/tmp/wordpress/"
- "/tmp/wordpress.tar.gz"
tags:
- wordpress

# Task 15: Removing Packages
- name: "Removing Packages"
yum:
name: "{{ packages }}"
state: absent
tags:
- cleanup
- never

# Task 16: Removing Directories
- name: "Removing Directories"
file:
path: "{{ item }}"
state: absent
with_items:
- /etc/httpd/conf/httpd.conf
- /etc/httpd/conf.d/default.conf
- /var/www/html/default/
tags:
- cleanup
- never
 

The playbook consists of several tasks, each responsible for a specific action.

  • Task 1: Installing Packages: Installs the necessary packages for the LAMP stack.
  • Task 2: Enabling/Restarting services: Ensures that the required services are enabled and restarted.
  • Task 3: Creating httpd.conf from template: Generates the Apache HTTP server configuration file from a template.
  • Task 4: Creating Virtual host: Creates a virtual host configuration file for the default website.
  • Task 5: Creating docroot for the website: Creates the document root directory for the website.
  • Task 6: Setting python3 mysql module PyMySQL: Installs the PyMySQL module for Python.
  • Task 7: Setting ROOT password for mariadb-server: Sets the root password for the MariaDB server.
  • Task 8: Creating DB for the website: Creates the database for the website.
  • Task 9: Creating extra user for WordPress: Creates an additional user with privileges for the WordPress database.
  • Task 10: Downloading WordPress: Retrieves the WordPress source code from a specified URL.
  • Task 11: Extracting WordPress: Extracts the WordPress archive.
  • Task 12: Copying WordPress files to docroot: Copies the WordPress files to the document root directory.
  • Task 13: Copying wp-config.php file to remote server: Creates the wp-config.php file for WordPress.
  • Task 14: Post Installation Cleanup: Cleans up temporary files and directories.
  • Task 15: Removing Packages: Uninstalls the packages installed during the installation process.
  • Task 16: Removing Directories: Removes the directories created during the installation process.
 

 

Comments

Popular posts from this blog

RHEL - How to back out a failed patch

Vathsa's- Linux - SysOps and DevOps

Local Yum Repository for Oracle Linux 8