Atlas Labs
Published on

Automating Systems with Ansible

Authors
  • avatar
    Name
    Khoa (Atlas Labs)
    Occupation
    Full-stack developer

Introduction

Ansible is a powerful automation tool designed to help developers and DevOps professionals simplify configuration management and application deployment. Unlike many other tools, Ansible does not require installing agents on servers, making the integration process much simpler.

Why choose Ansible?

  • Excellent scalability.
  • Easy-to-understand interface, suitable for beginners.
  • Large supporting community, with plenty of documentation and resources.

This article is designed specifically for beginners learning about DevOps, helping you easily approach Ansible without requiring deep expertise.

SEO Keywords: Ansible, automation, DevOps, configuration management, application deployment


Installing Ansible

Supported Operating Systems

Ansible runs on many popular operating systems such as:

  • Linux (Ubuntu, CentOS, Red Hat Enterprise Linux, etc.)
  • MacOS
  • Windows (WSL can be used for Linux-based operations)

Installation Methods

  1. On Linux
    • Ubuntu: sudo apt update && sudo apt install ansible
    • CentOS/Red Hat: sudo yum install ansible
  2. On MacOS
    • Using Homebrew: brew install ansible
  3. From source
    • Clone the source code from GitHub: git clone https://github.com/ansible/ansible.git
    • Follow the build steps.

Verify Installation

After installation, you can use the following command to check:

ansible --version

Basic Concepts in Ansible

Inventory

The Inventory in Ansible is a file containing the list of managed servers.

  • Create an inventory file:
[webservers]
192.168.1.10
192.168.1.11

[dbservers]
192.168.1.20
  • How to use it:
ansible all -i inventory -m ping

Modules

Modules are command blocks used to execute specific tasks. Some popular modules:

  • file: Manage files and directories.
  • apt: Manage software packages on Ubuntu.
  • yum: Manage software packages on CentOS/Red Hat.

Playbook

A Playbook is a YAML file used to describe automation steps:

- hosts: webservers
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

Writing Your First Ansible Playbook

Real-world Scenario

Installing Nginx on a list of servers.

Playbook Analysis

- hosts: webservers
  become: yes
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Start nginx service
      service:
        name: nginx
        state: started
  • Directive: hosts, become, tasks
  • Task: Each step to be executed.
  • Handlers: Handle post-task completion.

Execute the Playbook

ansible-playbook -i inventory playbook.yml

Advanced Concepts

Variables

Variables make playbooks more flexible:

- hosts: webservers
  vars:
    app_name: nginx
  tasks:
    - name: Install {{ app_name }}
      apt:
        name: "{{ app_name }}"

Templates

Use Jinja2 to create flexible configuration files:

- template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf

Roles

Roles help organize Playbooks according to a logical structure, making them easier to maintain.

roles/
  nginx/
    tasks/
    templates/
    vars/

Integrating Ansible with Other Tools

Ansible Tower

Ansible Tower provides a web interface for centrally managing Ansible.

CI/CD Integration

Ansible integrates easily into CI/CD pipelines like Jenkins or GitLab CI/CD:

  • Use the Ansible plugin in Jenkins.
  • Add Ansible to GitLab Runner jobs.

Conclusion

Through this article, you have gained a basic understanding of Ansible, from the concepts of inventory and playbooks to advanced concepts like templates and roles.

References:

Practice regularly to improve your skills and use Ansible confidently in real-world projects!