Skip to content

Linux for Ansible — Automating Linux Infrastructure at Scale

Ansible is an open-source automation and configuration management tool that simplifies infrastructure provisioning, application deployment, security enforcement, and operational tasks. Unlike many automation tools, Ansible is agentless, relying on SSH to communicate with managed Linux systems. Because Ansible runs primarily on Linux control nodes and manages Linux hosts, understanding Linux is essential for building reliable automation. Every DevOps engineer, Cloud Architect, Platform Engineer, Site Reliability Engineer (SRE), and System Administrator should master Linux for Ansible.


Learning Path

Linux Mastery → Module 13: Linux for DevOps → Lesson 6

Difficulty: Beginner → Advanced

Reading Time: 120 Minutes

Course Progress

Course: Linux Mastery

Module: Linux for DevOps

Lesson: 6 of 10


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand how Ansible works on Linux
  • Install and configure Ansible
  • Configure SSH authentication
  • Manage inventories
  • Create and execute playbooks
  • Organize automation using roles
  • Troubleshoot Ansible execution
  • Apply production automation best practices

Prerequisites

Complete:

  • Modules 1–12
  • Module 13 Lessons 1–5

Why Learn Linux for Ansible?

Traditional server management:

Administrator


SSH Login


Manual Configuration


Repeat on Every Server

Ansible automation:

Playbook


Linux Control Node


SSH


Hundreds of Linux Servers


Consistent Configuration

Linux provides the operating environment, SSH services, and command-line tools that make Ansible automation possible.


What is Ansible?

Ansible is an automation platform used for:

  • Configuration management
  • Application deployment
  • Infrastructure provisioning
  • Patch management
  • User management
  • Security automation
  • Cloud automation
  • Orchestration

Unlike agent-based tools, Ansible communicates directly over SSH.


Ansible Architecture

Playbook


Ansible Control Node


SSH


Managed Linux Hosts


Configuration Applied

Why Linux is Used

Linux provides:

  • SSH
  • Python runtime
  • Shell scripting
  • Package management
  • Process management
  • Filesystem permissions
  • Automation-friendly environment

Most production Ansible control nodes run Linux.


Install Ansible

Ubuntu

sudo apt update

sudo apt install ansible

RHEL

sudo dnf install ansible

Verify installation.

ansible --version

SSH Authentication

Generate an SSH key.

ssh-keygen -t ed25519

Copy the public key.

ssh-copy-id user@server

Verify access.

ssh user@server

Passwordless SSH is recommended for automation.


Inventory

The inventory defines managed hosts.

Example:

[web]

web01

web02

[database]

db01

Display inventory.

ansible-inventory --list

Test Connectivity

Ping all hosts.

ansible all -m ping

Example output:

SUCCESS

Ad-Hoc Commands

Run a command.

ansible all -m command -a "uptime"

Check disk usage.

ansible all -m command -a "df -h"

Display memory.

ansible all -m command -a "free -h"

Playbooks

Example:

---
- hosts: web

  become: true

  tasks:

    - name: Install Nginx

      package:

        name: nginx

        state: present

Run:

ansible-playbook site.yml

Variables

Example:

vars:

  package_name: nginx

Use:

name: "{{ package_name }}"

Roles

Typical structure:

roles/

└── webserver/

    ├── tasks/

    ├── handlers/

    ├── templates/

    ├── files/

    ├── vars/

    └── defaults/

Roles improve organization and reusability.


Privilege Escalation

Run tasks as root.

become: true

Equivalent Linux command:

sudo

Linux Package Management

Ubuntu:

apt:

RHEL:

dnf:

Generic:

package:

The package module automatically selects the appropriate package manager.


File Management

Copy files.

copy:

Manage templates.

template:

Create directories.

file:

Linux file permissions remain important.


Service Management

Manage services.

service:

  name: nginx

  state: started

  enabled: true

Equivalent Linux command:

systemctl start nginx

Logging

Increase output.

ansible-playbook site.yml -v

More detail.

-vv

Maximum debugging.

-vvvv

Useful Linux Commands

SSH.

ssh

Processes.

ps aux

Memory.

free -h

Disk.

df -h

Services.

systemctl

Real Production Examples

Test connectivity.

ansible all -m ping

Install packages.

ansible-playbook install.yml

Restart services.

ansible-playbook restart.yml

Display uptime.

ansible all -m command -a "uptime"

Production Perspective

Ansible automates:

  • Linux administration
  • Kubernetes clusters
  • Cloud infrastructure
  • Security hardening
  • CI/CD pipelines
  • Application deployments
  • Patch management
  • Configuration management

Linux is the primary platform for both Ansible control nodes and managed infrastructure.


Hands-on Lab

Task 1

Verify Ansible installation.

ansible --version

Task 2

Generate an SSH key.

ssh-keygen -t ed25519

Task 3

Test connectivity.

ansible all -m ping

Task 4

Run an ad-hoc command.

ansible all -m command -a "hostname"

Task 5

Create a simple inventory file.

[servers]

server1

server2

Task 6

Create a playbook that installs Git.

---
- hosts: servers

  become: true

  tasks:

    - name: Install Git

      package:

        name: git

        state: present

Execute it.

ansible-playbook install-git.yml

Task 7

Display disk usage on all servers.

ansible all -m command -a "df -h"

Task 8

Create an Ansible role that:

  • Installs Nginx
  • Starts the service
  • Enables it on boot

Command Deep Dive

Command Purpose Production Example
ansible --version Verify installation Environment validation
ansible all -m ping Test connectivity Health check
ansible-playbook Execute playbooks Infrastructure automation
ansible-inventory --list Display inventory Inventory validation
ssh-copy-id Configure SSH authentication Passwordless automation
ansible all -m command Execute remote commands Administration

Common Ansible Mistakes

Mistake Solution
Using password authentication Configure SSH keys
Hardcoding sensitive information Use Ansible Vault or external secret managers
Writing large monolithic playbooks Organize automation using roles
Ignoring idempotency Design tasks to be safely repeatable
Running everything as root Use become only when required

Production Troubleshooting Scenario

Scenario

An Ansible playbook fails on multiple servers.

Investigation:

ansible all -m ping

Several hosts are unreachable.

Next:

ssh server1

SSH authentication fails.

Further investigation:

ls ~/.ssh

The correct public key is not installed on the managed servers.

The administrator copies the public key:

ssh-copy-id user@server1

Connectivity is restored.

Verification:

ansible all -m ping

All hosts respond successfully.

Root cause:

SSH Authentication Misconfiguration

Best Practices

  • Use SSH key-based authentication.
  • Organize automation using roles.
  • Design playbooks to be idempotent.
  • Store secrets securely with Ansible Vault or an external secrets manager.
  • Keep inventories organized.
  • Test playbooks in non-production environments first.
  • Use version control for all playbooks.
  • Enable verbose logging when troubleshooting.

Common Mistakes

❌ Using passwords instead of SSH keys.

✅ Prefer SSH keys rather than using passwords.


❌ Storing secrets in plain text.

✅ Avoid this mistake: storing secrets in plain text.


❌ Writing very large playbooks without roles.

✅ Avoid this mistake: writing very large playbooks without roles.


❌ Ignoring Linux file permissions.

✅ Always review Linux file permissions.


❌ Executing automation directly in production without testing.

✅ Avoid this mistake: executing automation directly in production without testing.


Interview Questions

Beginner

  1. What is Ansible?
  2. Why is Ansible considered agentless?
  3. What is an inventory?
  4. What does ansible all -m ping do?

Intermediate

  1. What is idempotency?
  2. Why are SSH keys preferred for Ansible?
  3. What is the purpose of Ansible roles?
  4. How would you troubleshoot an unreachable host?

Architect Level

  1. How would you organize Ansible automation for thousands of Linux servers?
  2. How would you secure Ansible in enterprise environments?
  3. How would you integrate Ansible with Terraform and CI/CD pipelines?

Summary

In this lesson, you learned:

  • Linux's role in Ansible
  • SSH-based automation
  • Inventory management
  • Playbooks
  • Variables
  • Roles
  • Service management
  • Production automation best practices

Ansible leverages the power of Linux, SSH, and automation to manage infrastructure consistently and efficiently. By combining Linux administration skills with Ansible playbooks and roles, you can automate repetitive tasks, enforce configuration standards, and manage large-scale infrastructure with confidence.


Key Takeaways

  • Linux is the preferred platform for running Ansible.
  • SSH provides secure, agentless automation.
  • Inventories define managed infrastructure.
  • Playbooks automate repeatable operational tasks.
  • Roles improve maintainability and code reuse.
  • Strong Linux knowledge makes Ansible automation significantly more effective.

What's Next?

Linux for Jenkins — Building CI/CD Pipelines on Linux

You'll explore:

  • Installing Jenkins on Linux
  • Jenkins architecture
  • Linux build agents
  • Job automation
  • Pipeline execution
  • Jenkins administration
  • Production Jenkins best practices

By the end of the lesson, you'll understand how Linux powers Jenkins and how to build reliable CI/CD pipelines using Linux-based Jenkins controllers and agents.