Ansible: Automating Apache Server Setup with Ansible Playbook

Avnish Kumar Thakur
4 min readJan 11, 2024

--

Introduction:

In my last vlog [click here], we manually configured an Apache server using Ad-hoc commands. Now, let’s take it a step further and automate the setup using Ansible playbooks. Follow along with these simple steps to streamline your server configuration process.

Suggestion:

While the provided steps will work smoothly, I recommend using this document as a reference and attempting the process yourself. Embracing the learning process will not only enhance your understanding but also empower you to navigate potential challenges in your journey with Ansible.

Step 1: Create the Ansible Playbook

Let’s start by structuring our Ansible playbook. Open your favourite text editor and create a file, let’s call it test.yml. Here's the basic structure:

---
- name: Setup httpd server
hosts: all
tasks:
- name: Install httpd
ansible.builtin.package:
name: httpd
state: present

- name: Copy file from base system to target node
ansible.builtin.copy:
src: /root/test.html
dest: /var/www/html/lw.html

- name: Start service httpd, if not started
ansible.builtin.service:
name: httpd
state: started
enabled: true

This playbook does three main tasks: installs Apache (httpd), copies an HTML file, and ensures the Apache service is started. Let’s explain every point.

  1. name: Setup httpd server: This is a user-defined name for the play. It helps to provide a clear and descriptive label for this play, which is setting up the Apache server.
  2. hosts: all: This specifies the target hosts or servers where the playbook will be applied. In this case, it targets all hosts (all).
  3. tasks: This section contains a list of tasks that Ansible will execute on the target hosts.

Task 1:

  • name: Install httpd: A user-friendly name for the task.
  • ansible.builtin.package: Ansible module used for package management.
  • name: httpd: Specifies the name of the package to be installed (httpd - Apache web server).
  • state: present: Ensures that the specified package is present on the system.

Task 2:

  • name: Copy file from base system to target node: Another descriptive name for the task.
  • ansible.builtin.copy: Ansible module for copying files.
  • src: /root/test.html: Source file path on the control node (the machine running Ansible).
  • dest: /var/www/html/lw.html: Destination path on the target node where the file will be copied.

Task 3:

  • name: Start service httpd, if not started: Task name indicating the intention.
  • ansible.builtin.service: Ansible module for managing services.
  • name: httpd: Specifies the name of the service to be managed (httpd).
  • state: started: Ensures the service is started.
  • enabled: true: Ensures that the service starts on boot.

Step 2: Syntax Check

Before running the playbook, it’s always a good practice to check its syntax. Run the following command:

ansible-playbook --syntax-check test.yml

Ensure there are no syntax errors before proceeding.

Step 3: Run the Ansible Playbook

Execute the playbook with the following command:

ansible-playbook test.yml

Watch as Ansible automatically handles the installation, file copying, and service startup on the target nodes.

Bonus: Exploring Ansible Documentation

Curious about other Ansible modules? You can explore the documentation using these commands:

ansible-doc --list
ansible-doc --copy

These commands provide a list of available modules and detailed documentation for the copy module, helping you discover additional capabilities.

Conclusion:

With Ansible’s simplicity and power, this playbook serves as a foundation for automated server provisioning and maintenance in diverse environments.

What Next:

In the upcoming vlog, I will optimize this playbook, introducing advanced concepts such as variables and Jinja templating. Stay tuned for an exciting journey into the world of Ansible, where we’ll delve into these powerful features to further optimize and customize our automation processes.

Happy automating!

--

--

Avnish Kumar Thakur

Software Engineer | Tech Enthusiast | Linux, DevOps, ML, Python, Java, C | AWS | Generative AI | Researcher | Vlogger | Uncovering Tech's Depths