DEV Community

Cover image for Streamlining AWS Linux Servers Configuration with Ansible
Noble Mutuwa  Mulaudzi
Noble Mutuwa Mulaudzi

Posted on

Streamlining AWS Linux Servers Configuration with Ansible

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

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

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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

Top comments (0)