Streamlining AWS Linux Servers Configuration with Ansible
Published: 2023-07-25 10:44:07

In the ever-evolving landscape of cloud computing, Amazon Web Services (AWS) has emerged as a powerhouse, providing robust infrastructure solutions to businesses worldwide. As organizations migrate their workloads to AWS, efficient server management becomes crucial for optimal performance and cost-effectiveness. Enter Ansible, the open-source automation tool that simplifies the management of AWS Linux servers through its powerful configuration management capabilities.

In this guide, I will walk you through the process of effectively managing your cloud servers using Ansible, we will use Ansible to automate  updates in our webserver,(APACHE) 

_Tutorial by **Noble Mutuwa Mulaudzi**_


### _Architecture diagram_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivtobntopslzyid0st9i.png)

**_Before we start let us get familiar with some Ansible terms_**

- _**Ansible playbook** : a set of organized instructions that define the desired state of remote hosts and the tasks to be executed on them_

- _**Inventory** : a configuration file that lists the hosts or groups of hosts that Ansible can manage,_

- _**Modules** : pre-built, reusable scripts that Ansible uses to interact with the remote hosts and carry out specific tasks such as installing packages, managing files, or starting services._

_In this tutorial we will be using **package** and **service** modules_

### _Pre-requisites for Windows Users_


- Install Windows Subsystem for Linux (WSL)

- Choose a Linux Distribution( I  will be using Ubuntu, installed from microsoft store)

-  Install Ansible on the Linux Distribution

- 1 or more Cloud servers with apache installed

_Yeey, we have our linux enviroment set with Ansible installed_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d0qtulg3861anq4532g.png)

> Remember to have your target server's key pairs on your enviroment where you are using Ansible.

### _Step 1: Preparing the Ansible Inventory:__

Create an inventory file (e.g., hosts) that lists the IP addresses or hostnames of your AWS Linux instances. Group them logically based on their roles, such as web_servers and database_servers. The inventory file might look like this:

```

[web_servers]
webserver1 ansible_host=server_ip ansible_user=your_user ansible_ssh_private_key_file=/path/to/key/key.pem

```

> You can add as many servers as you want

### _Step 2: Writing the Ansible Playbook:_


Next, create an Ansible playbook (e.g., your_playbook.yml) that defines the configuration tasks:

```

- name: Update and Restart Apache on EC2 instances
  hosts: web_servers
  become: yes  # This allows Ansible to become root (sudo) to perform updates

  tasks:
    - name: Update Apache
      package:
        name: httpd
        state: latest
      become: yes

    - name: Restart Apache
      service:
        name: httpd
        state: restarted
      become: yes
```

### _Step 3: Executing the Playbook:_
 
Now, execute the playbook using the following command within the Linux terminal:

```
ansible-playbook -i /path/to/your_inventory_file/inventory.ini /path/to/your_playbook/your_playbook.yml
```


_Ansible will connect to the AWS Linux instances through SSH, perform the specified tasks, and ensure that the systems are updated and equipped with the necessary software._

_Here is our result_ . and just like that we have successfully updated our webserver with the latest apache through Ansible. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfl57gdfziw53v2722gx.png)

> Note that you can use various Ansible modules to perform other Ansible tasks and automate a wide range of operations on remote hosts. Some commonly used Ansible modules include:

- **apt**: Manages packages on Debian/Ubuntu systems, allowing you to install, update, or remove packages.

- **yum**: Similar to apt, but for Red Hat/CentOS systems, managing packages accordingly.

- **copy**: Enables you to copy files from the control machine to remote hosts.

- **file**: Helps manage files and directories on the remote hosts, allowing you to set permissions, ownership, etc

- **git**: Allows you to clone Git repositories onto the remote hosts.
## Conclusion

Having completed this tutorial, you are now equipped to unleash the full power of Ansible and its modules, enabling you to achieve even greater efficiency and automation in managing AWS instances. With Ansible's flexibility and ease of use, you can confidently tackle more complex tasks and drive innovation within your cloud infrastructure. Embrace the potential of Ansible to streamline your server management and open new possibilities for your AWS environment.

#### Article by Noble Mutuwa Mulaudzi