Operations 5 min read

Master Linux Automation: A Step‑by‑Step Ansible Deployment Guide

This guide walks you through installing Ansible on Linux, configuring inventory and settings, writing a basic playbook to deploy a web server, and executing it, while also covering variables, roles, templates, handlers, conditional tasks, and error handling for robust automation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Automation: A Step‑by‑Step Ansible Deployment Guide

1. Install Ansible

First ensure Ansible is installed on your Linux host. On Debian‑based systems run:

sudo apt update
sudo apt install ansible

On RPM‑based distributions such as CentOS or RHEL use:

sudo yum install epel-release
sudo yum install ansible

2. Configure Ansible

Inventory file : By default Ansible reads /etc/ansible/hosts. Edit this file to define host groups, for example:

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

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

ansible.cfg (optional) : Located at /etc/ansible/ansible.cfg, you can adjust SSH defaults and other parameters as needed.

3. Create an Ansible Playbook

A playbook is a YAML file that describes the tasks to run on target hosts. Below is a minimal example that installs and starts a web server on both Debian‑based and RedHat‑based systems.

---
- name: Deploy web server
  hosts: webservers
  become: yes
  tasks:
    - name: Install Apache (Debian)
      apt:
        name: apache2
        state: present
      when: ansible_os_family == 'Debian'

    - name: Install httpd (RedHat)
      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. Assuming the file is saved as web-deploy.yml:

ansible-playbook web-deploy.yml

5. Manage Variables, Roles, and Templates

Variables : Define them in the playbook, inventory, or separate variable files to customize deployments.

Roles : Group related tasks, variables, and files into reusable roles for better organization.

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

6. Advanced Features

Handlers : Define actions that run only when a task reports a change, such as restarting a service.

Conditional execution : Use when statements to run tasks based on facts or variables.

Error handling : Apply ignore_errors or rescue blocks to control failure behavior.

By following these steps you can automate simple software installations as well as complex multi‑stage deployments, and further explore dynamic inventories, secret management, and tools like Ansible Tower to enhance automation efficiency.

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.

automationDeploymentDevOpsLinuxPlaybook
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.