Operations 5 min read

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

This guide walks you through installing Ansible on Linux, configuring inventory and settings, writing playbooks to install and start web servers, and executing them, while also covering variables, roles, templates, handlers, conditionals, and error handling for robust automated deployments.

Raymond Ops
Raymond Ops
Raymond Ops
Master Automated Linux Deployments with Ansible: Step‑by‑Step Guide

Using Ansible on Linux enables efficient, flexible automation of deployment, configuration, and management through simple YAML playbooks.

1. Install Ansible

On Debian/Ubuntu:

sudo apt update
sudo apt install ansible

On RPM‑based systems (CentOS, RHEL):

sudo yum install epel-release
sudo yum install ansible

2. Configure Ansible

Inventory file : default location /etc/ansible/hosts. Example:

ansible.cfg (optional) : located at /etc/ansible/ansible.cfg, used to adjust defaults such as SSH parameters.

3. Create a Playbook

A playbook is a YAML file that defines tasks. Example that installs and starts a web server on both Debian‑based and RedHat‑based hosts:

---
- 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 with the ansible-playbook command, e.g.:

ansible-playbook web-deploy.yml

5. Manage Variables, Roles, and Templates

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

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

Templates : Jinja2 templates generate configuration files with dynamic values.

6. Advanced Features

Handlers : run only when a task changes state, such as restarting a service.

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

Error handling : employ ignore_errors and rescue blocks to control failures.

By following these steps you can automate simple software installations to complex multi‑stage deployments with Ansible, and later explore dynamic inventories, secret management, and Ansible Tower for even greater 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.

automationDeploymentConfiguration ManagementLinuxAnsiblePlaybook
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.