# Ansible - Roles

One of its core features is **Ansible Roles**, which help in organizing playbooks and reusable components efficiently. In this post, I’ll walk you through creating a simple Ansible role to install and configure Apache.

**<mark>Step 1: Initialize the Ansible Role</mark>**

To begin, we'll use the `ansible-galaxy` command to create the basic directory structure for our role:

```plaintext
ansible-galaxy init apache
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723845394107/8994570a-3827-4dd7-b6bd-654145d18cf2.png align="center")

This command generates a directory named `apache` with the standard structure, including folders for tasks, handlers, files, templates, and more.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723845452781/16bad1e3-cc19-4a4c-9274-f1c4fa22153a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723845515948/01a9d4fd-86f4-44a0-856f-a99c36cde795.png align="center")

**<mark>Step 2: Define the Tasks for Apache Installation</mark>**

Now, navigate to the `tasks` directory and edit the `main.yml` file to define the tasks required to install and configure `apache`:

```plaintext
vi apache/tasks/main.yml
```

Add the following content to the file:

```plaintext
# tasks file for apache
- name: Install the latest version of Apache
  apt:
    name: apache2
    state: present
- name: Start service apache2, if not started
  service:
    name: apache2
    state: started
- name: Copy file with owner and permissions
  copy:
    src: index.html
    dest: /var/www/html/index.html
    mode: '777'
```

**Explanation:**

1. **Install Apache**: We use the `apt` module to ensure Apache is installed on the target machine.
    
2. **Start Apache Service**: The `service` module checks if Apache is running and starts it if necessary.
    
3. **Copy Index File**: The `copy` module copies our custom `index.html` file to the Apache web directory with full permissions (`777`).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723845920609/fd6c9d57-19dc-420e-9806-24a1a782b764.png align="center")

**<mark>Step 3: Create the Index File</mark>**

We now need to create the `index.html` file that will be served by Apache. This file should be placed in the `files` directory of our role:

```plaintext
cd apache/files/
vi index.html
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723846137157/3fb64159-9fc0-4591-ba90-2fb1a3e22fdb.png align="center")

Add some any content to this file:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723846045412/5c2f1a49-64ee-4e07-8127-38b15d627136.png align="center")

**<mark>Step 4: Organize and Verify the Directory Structure</mark>**

Your role structure should look like this:

```plaintext
 tree apache/
```

This command will display something like:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723846245457/a86daf71-7a7b-48f0-8203-3d0b8346971c.png align="center")

**<mark>Step 5: Create the Playbook</mark>**

Finally, create a playbook that includes our Apache role:

```plaintext
vi roles.yaml
```

Add the following content:

```plaintext
- hosts: all
  remote_user: ubuntu
  become: yes
  roles:
    - apache
```

**Explanation:**

* **hosts**: Specifies the target hosts (in this case, `all`).
    
* **remote\_user**: The user Ansible will connect as (`ubuntu`).
    
* **become**: Ensures commands are executed with elevated privileges (sudo).
    
* **roles**: Includes our `apache` role.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723846529864/cf813f95-20c4-44de-a4e4-a57957eeec8e.png align="center")

**<mark>Step 6: Execute the Playbook</mark>**

Now, you can run your playbook to apply the Apache role to your target machines:

```plaintext
ansible-playbook -i inventory.html roles.yaml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723847001796/9e1ef2b3-eee0-4a39-af84-48d5bd38755b.png align="center")

If everything is set up correctly, Apache will be installed, the service will be started, and your custom `index.html` will be served.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723847823874/a9dc2665-7f8b-46e3-ac44-80752ebf8cd3.png align="center")

### **Conclusion**

By creating an Ansible role, we’ve encapsulated the Apache installation and configuration into a reusable and organized structure. This approach not only makes automation more manageable but also enhances the scalability of your infrastructure management. As your needs grow, you can extend this role with more complex configurations, handlers, templates, and variables.
