← Back to list

How to Create a Simple Playbook in Ansible (Rhel based)

Ansible is one of the most powerful tools in a DevOps engineer’s arsenal, providing an easy way to automate infrastructure and…

Jerome Decinco · 2024-12-09 10:32 · 51 claps · 2.9 min read
#ansible #httpd #configuration #automation
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

How to Create a Simple Playbook in Ansible (Rhel based)

Ansible is one of the most powerful tools in a DevOps engineer’s arsenal, providing an easy way to automate infrastructure and configuration management. At the heart of Ansible lies the playbook, a YAML-based file where you define tasks to be executed on managed hosts. If you’re just getting started, this guide will walk you through creating a simple playbook step by step.

What Is an Ansible Playbook?

An Ansible playbook is a structured set of instructions (tasks) that Ansible executes on your target machines. Think of it as a script but written in YAML, making it human-readable and easy to maintain.

A playbook typically includes:

  1. Hosts: The target machines where tasks will be executed.
  2. Tasks: The actions to be performed, such as installing packages, copying files, or restarting services.
  3. Modules: Predefined code in Ansible used to perform specific tasks (e.g., yum, apt, copy).

Prerequisites

Before creating your first playbook, ensure you have:

  1. Ansible installed on your control machine.
  • On a Linux system:
sudo dnf install ansible -y   # For RHEL-based systems 
sudo apt install ansible -y   # For Debian-based systems
  • A target machine (managed node) accessible via SSH with proper credentials.
  • A configured inventory file, listing the target hosts. Example:
[webservers] 192.168.1.100 ansible_user=rheluser1

Steps to Create a Simple Ansible Playbook

Here’s how to create a playbook to perform basic tasks, such as updating the system and installing a web server.

1. Create a Directory for Your Playbook

Organize your project by creating a dedicated directory:

mkdir ansible-playbook-webserver && cd ansible-playbook-webserver

2. Write the Playbook File

Create a YAML file named webserver.yml:

vi webserver.yml

Add the following content to your playbook:

---
# This is a simple Ansible playbook to configure a web server.
- name: Configure Web Server
  hosts: webservers
  become: true  # Run tasks as sudo/root user
  tasks:
    # Task 1: Update the package index
    - name: Update the system
      ansible.builtin.dnf:
        update_cache: yes
      when: ansible_os_family == "RedHat"
# Task 2: Install Apache web server
    - name: Install Apache
      ansible.builtin.dnf:
        name: httpd
        state: present
      when: ansible_os_family == "RedHat"
# Task 3: Start and enable Apache service
    - name: Ensure Apache is running
      ansible.builtin.service:
        name: httpd
        state: started
        enabled: true

Explanation:

  1. **hosts**: Defines which machines in the inventory will run this playbook (webservers group in this case).
  2. **become: true**: Runs tasks with elevated privileges (like sudo).

Tasks:

  • Use the dnf module to update the package index and install Apache.
  • Use the service module to ensure Apache is running and enabled on boot.

Conditionals (when): Ensures tasks run only on compatible operating systems (e.g., Rhel-based).

3. Create an Inventory File

The inventory file defines your target hosts. Create a file called inventory.ini:

vi inventory.ini

Add the following content:

[webservers]
192.168.1.100 ansible_user=rheluser1

Replace 192.168.1.100 with your target host's IP and ansible_user with the SSH user.

4. Run the Playbook

Run the playbook using the ansible-playbook command:

ansible-playbook -i inventory.ini webserver.yml

Output:

  • You’ll see Ansible executing each task in sequence. If everything is set up correctly, Apache will be installed and running on your target machine.

5. Verify the Result

Log in to the managed host and check if Apache is installed and running:

sudo systemctl status httpd

If successful, Apache should be running and enabled on startup.

Tips for Writing Playbooks

Use Variables: Define reusable variables in the playbook to make it more dynamic. For example:

vars:   
  web_package: httpd
tasks:   
  - name: Install Apache     
    ansible.builtin.dnf:       
      name: "{{ web_package }}"       
      state: present

Test with --check: Use the --check option to run your playbook in "dry-run" mode without making changes:

ansible-playbook -i inventory.ini webserver.yml --check

Break Down Plays: Instead of packing everything into one playbook, modularize tasks into reusable roles.

Use Handlers for Services: For tasks like restarting services, use handlers for better efficiency:

handlers:   
  - name: restart apache
    service:
      name: httpd
      state: restarted

Conclusion

Creating a simple Ansible playbook is a straightforward way to automate tasks and manage servers efficiently. As you become more familiar with YAML syntax and Ansible modules, you’ll be able to tackle more complex configurations and deployments.

Start small, keep your playbooks modular, and leverage Ansible’s vast library of modules to simplify your DevOps workflows. Happy automating! 🚀

Let me know if you’d like me to expand this article with more advanced examples or use cases!


메타데이터
post_id
4be29dd86e84
slug
how-to-create-a-simple-playbook-in-ansible-4be29dd86e84
url
https://medium.com/@jeromedecinco/how-to-create-a-simple-playbook-in-ansible-4be29dd86e84
canonical_url
https://medium.com/@jeromedecinco/how-to-create-a-simple-playbook-in-ansible-4be29dd86e84
author_url
https://medium.com/@jeromedecinco
status
ok
fetched_at
2026-06-27 23:56:40