Operations 5 min read

Master Automated Linux Deployments with Ansible: A Step‑by‑Step Guide

This guide walks you through installing Ansible, configuring inventory and settings, writing playbooks, running deployments, and leveraging variables, roles, templates, handlers, conditionals, and error handling to fully automate Linux server provisioning and management.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Automated Linux Deployments with Ansible: A Step‑by‑Step Guide

Using Ansible on Linux provides an efficient and flexible way to automate configuration, deployment, and management through simple YAML playbooks.

1. Install Ansible

On Debian‑based systems (e.g., Ubuntu):

sudo apt update
sudo apt install ansible

On RPM‑based systems (e.g., CentOS, RHEL):

sudo yum install epel-release
sudo yum install ansible

2. Configure Ansible

The inventory file (default /etc/ansible/hosts) defines hosts and groups. Example:

[webservers]
host1.example.com
host2.example.com

[databases]
db1.example.com
db2.example.com

An optional configuration file /etc/ansible/ansible.cfg can adjust defaults such as SSH parameters.

3. Create an Ansible Playbook

A playbook describes deployment tasks in YAML. Below is a simple example that installs and starts a web server.

---
- name: Deploy web server
  hosts: webservers
  become: yes

  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
      when: ansible_os_family == 'Debian'

    - name: Install httpd
      yum:
        name: httpd
        state: present
      when: ansible_os_family == 'RedHat'

    - name: Start Apache/httpd service
      service:
        name: "{{ 'apache2' if ansible_os_family == 'Debian' else 'httpd' }}"
        state: started
        enabled: yes

4. Run the Playbook

Execute the playbook with the ansible-playbook command:

ansible-playbook web-deploy.yml

5. Manage Variables, Roles, and Templates

Variables : Define in playbooks, inventory files, or separate variable files for customization.

Roles : Collections of related tasks, variables, and files that promote reuse and organization.

Templates : Use the Jinja2 engine to generate configuration files with dynamic values.

6. Advanced Features

Handlers : Execute actions (e.g., service restart) only when a preceding task changes the system state.

Conditional execution : Apply when statements to run tasks based on facts.

Error handling : Control failures with ignore_errors and rescue blocks.

By following these steps you can effectively automate Linux server deployment with Ansible, handling everything from simple package installs to complex multi‑stage workflows, and further explore dynamic inventories, key management, and Ansible Tower for enhanced automation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

deploymentconfiguration-management
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.