Install Apache Server on Amazon Linux Using Ansible

Install Apache Server on Amazon Linux Using Ansible

Introduction:

In this article, you will see how to create an apache web server using Ansible. The Apache server is an open-source web server and it's widely used in Linux servers. Ansible is an automation tool which can be used for configuration automation.

Prerequisites:

EC2 instance with amazon linux

Ansible installed on the local machine

(Refer to the documentation for installation https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-and-upgrading-ansible)

How Ansible works?

Ansible is an agentless automation tool. That means unlike puppet or chef you don’t need to install any agent on remote machines. Ansible connects to the remote machines using ssh. Therefore it requires the IP addresses and private ssh key files to connect to the remote machines. This data can be provided to the ansible using an inventory file.

Ansible uses playbooks to perform actions on the remote machines. These playbooks are YAML files which contain plays (instructions).

Project directory:

Step 1:

Create an inventory file for ansible like the following

File name: hosts

Step 2:

Create an Ansible playbook file named apache.yaml

File name: apache.yaml

The ‘become: yes’ attribute ensures to run the following plays as the root user. Here we are using the ‘yum’ module to install the latest httpd server. The ‘systemd’ module is used to start the httpd service and to make sure that httpd autostarts on every reboot. The ‘update_cache: yes’ is equivalent to ‘yum update’.

Documentation for the modules:

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html

Step 3:

Run the ansible playbook using the following command.

$ ansible-playbook Apache.yaml -i hosts

And you’ll see the following output.

Step 4 (optional):

To disable the ssh host key checking you can add an ansible.cfg file with the following content. It will not prompt you for ssh key confirmation.

File name: ansible.cfg

Step 5:

Now go to port 80 of your remote server’s IP address.

http://<IP address>:80.

You will see a screen like this.

If you want to read in-depth about Ansible, here I've found a good article: https://spacelift.io/blog/ansible-tutorial