Operations 6 min read

Master Ansible Playbooks: From Basics to Running Apache Automation

This guide explains Ansible playbooks, their required hosts and tasks sections, optional fields like name and become, YAML indentation rules, module usage, and demonstrates creating and executing a playbook that installs and starts Apache on a target host.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Ansible Playbooks: From Basics to Running Apache Automation

Playbook is a file composed of one or more plays; a play is an ordered set of tasks executed on specific hosts or host groups. Each playbook must contain two required parts:

hosts: the group of hosts on which the playbook runs

tasks: the tasks to be executed on the hosts

Optional fields may also be included, such as:

name: the play's name, displayed during execution

become: privilege escalation, similar to the become setting in the configuration file; it can be enabled per play.

Playbooks are written in YAML format, typically saved with a .yml extension. YAML uses space indentation; elements at the same level must have identical indentation, and child items must be indented more than their parents.

it@workstation:~/ansible$ vim test.yml
it@workstation:~/ansible$ cat test.yml
---
- name: Install Apache
  hosts: servera
  tasks:
    - name: Install apache httpd
      apt:
        name: apache2
        state: present
    - name: Copy using inline content
      copy:
        content: Welcome to
        dest: /var/www/html/index.html
    - name: Start apache service
      service:
        name: apache2
        state: started
        enabled: yes

A playbook starts with --- to mark the beginning of the file. The second line name defines the play's name; the third line hosts specifies the target hosts; the fourth line tasks lists the tasks to execute.

Through indentation, the tasks are divided into three modules, each with a name (optional but recommended for clarity). The modules used are:

apt: installs packages

copy: copies files or content

service: manages services such as starting or restarting

You can retrieve more information about modules with ansible-doc, list all modules with ansible-doc -l, and filter results using grep (e.g., ansible-doc -l | grep apt). Detailed help for a specific module is obtained via ansible-doc Module_Name.

it@workstation$ ansible-playbook test.yml
BECOME password:

PLAY [Install Apache] ******************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [servera]

TASK [Install apache httpd] ************************************************************************
changed: [servera]

TASK [Copy using inline content] *****************************************************************
changed: [servera]

TASK [Start apache service] **********************************************************************
ok: [servera]

PLAY RECAP *************************************************************************************
servera                : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Because become is configured, the execution prompts for the become password. During execution, the play's name is displayed, facts are gathered (providing system information), and each task shows its name and status: ok means no change, while changed indicates a modification. The final recap summarizes the number of changed, failed, skipped, and other outcomes.

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.

DevOpsYAMLAnsiblePlaybook
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.