Expert-led training for your team
How to Run an Ansible Playbook: A Comprehensive Step-by-Step Guide

6 April 2023

How to Run an Ansible Playbook: A Comprehensive Step-by-Step Guide


Ansible is a powerful automation tool that simplifies the management and deployment of IT infrastructure. It uses a declarative language called YAML to define the state of the infrastructure and execute tasks across multiple servers. One of the core components of Ansible is the playbook, which is a collection of tasks that define the desired state of the infrastructure. In this guide, we will show you how to run an Ansible playbook step-by-step and provide examples for common use cases.

Step 1: Define your inventory

The first step in running an Ansible playbook is to define your inventory, which is a list of servers that Ansible will manage. You can define your inventory in a file called "hosts" using the INI format or the YAML format. Here is an example of an INI-style hosts file:


[web_servers] [database_servers]

Alternatively, you can define your inventory in a YAML file like this:


all: hosts: web_servers: hosts: database_servers: hosts:

Step 2: Create your playbook

Once you have defined your inventory, the next step is to create your playbook. A playbook is a YAML file that defines the tasks to be executed on your servers. Here is an example of a simple playbook that installs and starts the Apache web server:


--- - name: Install and start Apache hosts: web_servers become: true tasks: - name: Install Apache yum: name: httpd state: present - name: Start Apache service: name: httpd state: started

In this example, the "name" field is used to give the playbook a descriptive name, while the "hosts" field specifies which servers the playbook will be run on. The "become" field is used to indicate that the tasks in the playbook should be executed with elevated privileges, and the "tasks" field is used to define the individual tasks to be executed. Each task is defined with a name and a module, which is used to perform the desired action.

Step 3: Run your playbook

To run your playbook, you can use the "ansible-playbook" command, followed by the path to your playbook file. Here is an example of how to run the playbook we created in Step 2:

ansible-playbook -i hosts apache-playbook.yml

In this example, the "-i" flag is used to specify the path to the inventory file, while the ".yml" extension is used to indicate that the file is a YAML file. When you run this command, Ansible will connect to the servers specified in the inventory file and execute the tasks defined in the playbook.

Use Cases:

  1. Configuration Management: Ansible can be used to automate the configuration of servers and applications, making it easier to maintain consistency and reduce errors.

  2. Deployment Automation: Ansible can be used to automate the deployment of applications across multiple servers, reducing the time and effort required for manual deployments.

  3. Disaster Recovery: Ansible can be used to automate disaster recovery processes, making it easier to restore services in the event of an outage.


Running an Ansible playbook is a straightforward process that allows you to automate tasks across your IT infrastructure. By defining your inventory, creating a playbook, and running the playbook, you can easily manage your servers and applications in a consistent and efficient manner.

In this guide, we have provided a comprehensive step-by-step tutorial on how to run an Ansible playbook, along with examples for common use cases. We hope this guide has been helpful in getting you started with Ansible and that you can use it to streamline your IT operations.

Remember, Ansible is a powerful tool that can help you manage your IT infrastructure at scale. With the ability to automate tasks and streamline workflows, you can reduce the time and effort required to manage your servers and applications, allowing you to focus on more strategic tasks.

Keep exploring Ansible and its capabilities, and you'll soon find yourself automating tasks that you never thought were possible. Good luck and happy automating!

Here are some official Ansible documentation and links that you may find useful:

  1. Ansible official documentation:
  2. Ansible Playbook Introduction:
  3. Ansible Playbook Basics:
  4. Ansible Playbook Best Practices:
  5. Ansible Inventory Introduction:
  6. Ansible Modules Index:

These links provide detailed information on the different aspects of Ansible, including playbooks, inventory, and modules. They are great resources for learning more about Ansible and expanding your knowledge of the tool.

Additionally, the Ansible community is very active, and there are many user groups, forums, and blogs dedicated to Ansible. Some popular resources include:

  1. Ansible Galaxy:
  2. Ansible subreddit:
  3. Ansible mailing list:
  4. Ansible blogs:

These resources provide a wealth of information and support for Ansible users. Whether you're just getting started or you're an experienced user, you're sure to find useful information and insights on these platforms.

About the author: Daniel West
Tech Blogger & Researcher for JBI Training

+44 (0)20 8446 7555

[email protected]



Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us


Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course