Ansible Beginner’s Practical Guide: Achieve Server Configuration Automation in 3 Steps and Say Goodbye to Repetitive Manual Operations

Ansible Beginner’s Practical Guide: Achieve Server Configuration Automation in 3 Steps and Say Goodbye to Repetitive Manual Operations

Ansible is an extremely useful tool in the DevOps field that helps us easily manage server configuration automation. Imagine having to manually type commands and configure files every time you deploy an application; that would be quite troublesome. Ansible acts like a smart assistant, capable of handling these repetitive tasks with a single click, saving time and effort. Next, I will guide you through 3 steps to get started with Ansible and achieve server configuration automation.

Step 1: Install Ansible

First, you need to install Ansible on your machine. Ansible is based on Python, so make sure your Python environment is functioning properly. On Linux, you can install it using a package manager. For example, on Ubuntu, use the <span>apt</span> command:


sudo apt update

sudo apt install ansible

After installation, check the version using the <span>ansible --version</span> command. If you see the version number, it means the installation was successful. This step is not difficult, but if the version number does not display, you need to check if the installation command was typed correctly.

Step 2: Configure Ansible Inventory

To manage servers with Ansible, you first need to know which hosts to manage. This requires the inventory file <span>inventory</span>. By default, it is located at <span>/etc/ansible/hosts</span>. You can list the IP addresses of the servers you want to manage in this file. For example:


[web_servers]

192.168.1.10

192.168.1.11


[db_servers]

192.168.1.20

Here, the servers are divided into two groups: <span>web_servers</span> and <span>db_servers</span>. This way, when executing operations, you can handle different groups of servers separately. If you mix up the group names or IP addresses, Ansible will not be able to find the target hosts, so this step requires careful verification.

Step 3: Write a Playbook to Automate Tasks

The Playbook is the core of Ansible, defining a series of tasks in YAML format. For example, if we want to install the Apache service on all web servers, we can write a simple Playbook:


---
-name: Install Apache on web servers
hosts: web_servers
become: yes
tasks:
    -name: Install Apache package
      apt:
        name: apache2
        state: present

This Playbook is named “Install Apache on web servers” and specifies that it runs on hosts in the <span>web_servers</span> group. The <span>become: yes</span> indicates that administrator privileges are required. The task uses the <span>apt</span> module to install the <span>apache2</span> package, and <span>state: present</span> ensures that the package is installed. To run this Playbook, use the command:


ansible-playbook your_playbook_name.yml

If you see the task executed successfully, Apache is installed. However, if the server does not have the <span>apt</span> module or if there is an issue with the Playbook format, the task will fail. Therefore, when writing Playbooks, you need to be clear about the format and module usage.

Helpful Tips

Ansible has a vast number of modules, each with its own parameters, so it’s best to check the documentation before using them. Also, indentation in Playbooks is very important; YAML format has strict indentation requirements, and if the indentation is incorrect, the Playbook will not run.

Practical Scenario: Updating Server Packages

In actual work, updating server packages is a common task. By writing a Playbook in Ansible, you can update all server packages with a single command. For example:


---
-name: Update packages on all servers
hosts: all
become: yes
tasks:
    -name: Update all packages
      apt:
        upgrade: yes

This Playbook specifies that it runs on all hosts, and the task is to upgrade all packages. If there are many servers, manually updating them would take a lot of time, but Ansible can handle it easily.

Learning Tips

The Ansible documentation is a great helper for learning, containing numerous modules and usage examples. The more you read and practice, the more proficient you will become. Also, don’t be afraid to make mistakes; when you do, carefully check the code to understand what went wrong, as this is how you improve quickly.

Through these 3 steps, we can achieve server configuration automation with Ansible. Say goodbye to repetitive manual operations and let Ansible help us efficiently complete tasks; this is the charm of DevOps.

Leave a Comment