Installing application packages and services – Infrastructure as Code (IaC) with Terraform

We will use the apt module to install the packages on Ubuntu, and the service module to start and enable the service.

Let’s start by installing Apache on the web servers using the following install-webserver.

yaml playbook:
hosts: webservers
become: true tasks:
name: Install packages apt:
name:
apache2
php
libapache2-mod-php
php-mysql
update_cache: yes
cache_valid_time: 3600
state: present
name: Start and Enable Apache service
service: name=apache2 state=started enabled=yes

As this configuration is for webservers, we’ve specified that within the hosts attribute. The tasks section defines two tasks—Install packages and Start and Enable Apache service. The Install packages task uses the apt module to install apache2, php , libapache2-mod-php, and php-mysql. The Start and Enable Apache service task will start and enable the apache2 service.

Similarly, we will install and set up the MySQL service using the following install-dbserver.

yaml playbook:
hosts: dbservers
become: true tasks:
name: Install packages apt:
name:
python-pymysql
mysql-server update_cache: yes cache_valid_time: 3600 state: presentname: Start and enable MySQL service service:
name: mysql
state: started
enabled: true

This playbook will run two tasks—Install packages and Start and enable MySQL service. The Install packages task will install the python-mysql and mysql-server packages using the apt module. The Start and enable MySQL service task will start and enable the MySQL service.

Configuring applications

The next step in the chain is to configure the applications. There are two playbooks for this. The first will configure Apache on webservers, and the second will configure MySQL on dbservers.

The following setup-webservers.yaml playbook will configure webservers:
hosts: webservers
become: true tasks:
name: Delete index.html file file:
path: /var/www/html/index.html
state: absent
name: Upload application file copy:
src: index.php
dest: /var/www/html
mode: 0755 notify:
Restart Apache handlers:
name: Restart Apache
service: name=apache2 state=restarted

This playbook runs on all nodes with the webservers role, and there are three tasks in this playbook. The Delete index.html file task uses the file module to delete the /var/www/html/ index.html file from the web server. That is because we are using index.php as the index page and not index.html. The Upload application file task then uses the copy module to copy the index.php file from the Ansible control node to the web server at the /var/www/ html destination, with a mode of 0755. The Upload application file task also has a notify action that will call the Restart Apache handler if this task has a status of CHANGED. A handlers section within the playbook defines handlers that listen to notify events. In this scenario, if there is a change in the Upload application file task, the Restart Apache handler will be triggered and will restart the apache2 service.

We will use the following setup-dbservers.yaml playbook to configure MySQL on dbservers:
hosts: dbservers
become: true vars:
mysql_root_password: “Password@1” tasks:
name: Set the root password copy:
src: client.my.cnf
dest: “/root/.my.cnf”
mode: 0600 notify:
Restart MySQL
name: Create a test user mysql_user:
name: testuser
password: “Password@1”
login_user: root
login_password: “{{ mysql_root_password }}”
state: present
priv: ‘.:ALL,GRANT’
host: ‘%’
name: Remove all anonymous user accounts mysql_user:
name: ”
host_all: yes
state: absent
login_user: root
login_password: “{{ mysql_root_password }}” notify:
Restart MySQL
name: Remove the MySQL test database mysql_db:
name: test
state: absent
login_user: root
login_password: “{{ mysql_root_password }}” notify:
Restart MySQL
name: Change bind address lineinfile:
path: /etc/mysql/mysql.conf.d/mysqld.cnf
regexp: ^bind-address
line: ‘bind-address = 0.0.0.0’
notify:
Restart MySQL handlers:
name: Restart MySQL
service: name=mysql state=restarted

Thisplaybook is a bit more complicated, but let’s break it down into parts to facilitate our understanding.

There is a vars section in this playbook that defines a mysql_root_password variable. We need this password while executing MySQL tasks. The first task is to set up the root password. The best way to set that up is by defining a /root/.my.cnf file within MySQL that contains the root credentials. We are copying the following client.my.cnf file to /root/.my.cnf using the copy module:
[client]
user=root
password=Password@1

Then, the Create a test user task uses the mysql_user module to create a user called testuser. It requires values for the login_user and login_password attributes, and we are supplying root and {{ mysql_root_password }}, respectively. It then goes ahead and removes all anonymous users and also removes the test database. It then changes the bind address to 0.0.0.0 using the lineinfile module. The lineinfile module is a powerful module that helps manipulate files by first grepping a file using a regex and then replacing those lines with the line attribute’s value. All these tasks notify the Restart MySQL handler that restarts the MySQL database service.

Leave a Reply

Your email address will not be published. Required fields are marked *