• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

Roger Perkin

Network Automation Architect

  • Network Automation
    • Network Automation Courses
    • What is NetDevOps?
    • Workflow Orchestration
    • Ansible Automation Platform
    • Ansible Workshop
    • What is Network Automation?
    • Network Automation Tools
    • ContainerLab
    • Ansible Training
      • What is Ansible?
      • Ansible Tutorial for Beginners
      • Ansible Network Automation
      • Ansible Hosts File
    • Python Network Automation
      • Nornir
      • Python Network Automation Course
      • Python for Network Engineers
      • Python VENV / Virtual Environment Tutorial
      • Python Tutorial for Beginners
      • pyATS
    • Network Source of Truth
      • NetBox Training
      • Infrahub
    • NetDevops
    • DevOps Tutorial
      • Git Training
      • Terraform Training
      • Linux Training
      • Kubernetes Training
      • Devops Training Course
      • Azure Devops Training
    • Terraform
    • GIT
      • Git Commands
      • What is GitHub?
    • Docker Training
    • Confluence
    • Microsoft Azure
  • Cisco
    • ISE
    • SD WAN Training
    • Password Recovery
    • Software-Upgrade-Guides
    • BGP
    • Data Center
    • WIRELESS
  • CCIE
  • Blog
  • About
    • My Red Special Guitar
  • Contact

Mastering the Ansible Playbook

Home » Network Automation » Ansible

Introduction to Ansible Playbooks

What is an Ansible Playbook?

An Ansible playbook is a YAML file that defines a series of tasks and commands that are executed on one or more remote machines.

An Ansible Playbook is a blueprint of automation tasks that you want to perform on your network infrastructure, servers or cloud or windows infrastructure.

Ansible is a configuration management tool and Playbooks are Ansible’s way of organising and automating tasks

ansible playbook logo

An Ansible playbook can include one or more “Plays” which map a group of hosts to a set of tasks. Each play specifies the hosts that it applies to, and a list of tasks that should be executed on those hosts.

“Tasks” are defined using Ansible modules, which are pre-built scripts that can be used to perform a wide range of automation tasks. Ansible Playbooks can also include variables and templates, which can be used to parameterize the tasks and make them more reusable.

Playbooks are a key component of Ansible’s configuration management and automation capabilities and are used to automate repetitive tasks, deploy software, and manage IT infrastructure.

Write an Ansible Playbook

The following Ansible playbook will demonstrate how easy it is to provision a server by installing an Apache sever and ensuring the service is started.

Run an Ansible Playbook

Create an Ansible Role

Anatomy of an Ansible Playbook

One of the most simple Ansible Playbooks is one that installs Apache on a Server

The playbook is below and then I will explain what each step is doing

---
- name: Install Apache
  hosts: all
  become: true
  tasks:
    - name: Install Apache package
      package:
        name: httpd
        state: present
    - name: Start Apache service
      service:
        name: httpd
        state: started

This playbook has two tasks:

  1. The first task installs the Apache web server package on the target host using the package module. The name parameter specifies the name of the package to be installed and the state parameter ensures that the package is present on the host.
  2. The second task starts the Apache service on the target host using the service module. The name parameter specifies the name of the service to be started and the state parameter ensures that the service is running.

This playbook is executed with the command ansible-playbook apache.yml and it will install the Apache package and start the service on all the target host which are defined in the inventory file.

Ansible-Playbook Command

ansible-playbook is a command-line tool used to run Ansible Playbooks. A playbook is a collection of instructions in YAML format that define a set of tasks to be executed by Ansible on a group of targeted hosts or machines.

The ansible-playbook command is used to execute a playbook, and it takes the path to the playbook file as an argument. It connects to the targeted hosts specified in the playbook, and runs the tasks defined in the playbook on those hosts.

The basic syntax of the ansible-playbook command is:

ansible-playbook [options] playbook.yml

For example, to run a playbook called playbook.yml, the command would be:

ansible-playbook playbook.yml

Some of the options that can be used with the ansible-playbook command include:

  • -i or –inventory to specify a custom inventory file
  • -l or –limit to limit the execution to a specific subset of hosts
  • -v or –verbose to increase the output verbosity
  • -e or –extra-vars to pass extra variables to the playbook
  • -t or –tags to run specific tasks or groups of tasks
  • –check to check the playbook without making any changes

It’s important to note that the ansible-playbook command must be run on the control machine and it will communicate with the target host over SSH, unless you have configured a different connection method.

Best Practices for Ansible Playbook Development

Playbook Version Control with Git

Ansible Playbook Examples

Ansible Playbook for Network Automation

Ansible Network Automation is a great use case for Ansible and is becoming one of the fastest growing use cases. Check the video below for a practical example

Ansible is a very popular tool for network automation. It provides a simple, easy-to-use automation language that allows network engineers to automate various network tasks such as network device configuration, monitoring, and troubleshooting.

Ansible’s use of YAML, which is easy to understand and use, makes it a popular choice for network automation. Ansible also supports a large number of network devices and platforms, including popular vendors such as Cisco, Juniper, Arista, and many more.

Ansible also supports a wide variety of modules, which can be used to automate specific network tasks such as configuring VLANs, interfaces, routing protocols, and more. Furthermore, Ansible playbooks can be used to automate complex network workflows, making it easy to manage and automate the entire network infrastructure.

Additionally, Ansible also provides a network automation platform called Ansible Automation Platform, which can be used to manage and automate network infrastructure at scale.

In summary, Ansible is a powerful and flexible tool that is well-suited for network automation, it allows network engineers to automate repetitive tasks, ensure consistency and improve the overall efficiency of the network operations.

For more information please check out my Ansible Training

Cisco Ansible Playbook Example

Playbook to backup Cisco Router here

Palo Alto Ansible Playbook Example

Playbook to configure Palo Alto Firewall here

Can we use Python in Ansible Playbook?

Yes you can, within a standard Ansible Playbook you can call a Python script to run a specific task.

Ansible Playbook Elements

  • Plays – A play is a top level specification of a group of tasks
  • Tasks – A Task defines which module to use to perform a certain task
  • Modules – Ansible modules are reusable, standalone scripts that can be used by the Ansible API, or by the ansible or ansible-playbook programs. They return information to ansible by printing a JSON string to stdout before exiting. They take arguments in one of several ways which we’ll go into as we work through this tutorial.
  • Plugins – Technically Modules are plugins, but a plugin is a piece of code that augment Ansible’s core functionality
  • Inventory – The Ansible Inventory is a file that contains a list of all the hosts you want to automate. They can put into groups i.e [DB-Servers]
  • Roles – Roles are re-useable playbooks that you can call on to perform similar tasks

Why us Ansible instead of Terraform?

Ansible and Terraform whilst they can perform some similar tasks, they are very different tools, for a full explanation please read Terraform vs Ansible

Ansible Playbook for Ping

What is the difference between Ansible & Ansible Playbooks?

ansible logo

Ansible is an open-source automation tool that allows you to automate tasks such as software deployments, server provisioning, and configuration management. It uses a simple, human-readable language called YAML to describe the tasks to be performed.
Ansible Playbooks are a collection of Ansible commands organized into a single file, written in YAML format. They allow you to define a set of tasks and their execution order, and can be used to perform complex automation tasks. Playbooks can also include variables, loops, and conditionals, making them a powerful tool for automating complex workflows. In short, Ansible is the tool and Playbooks are written in YAML to use that tool and organize the automation tasks.

What is Ansible Playbook in DevOps?

ansible logo

Ansible Playbook in DevOps. Ansible is a configuration management and orchestration tool that allows developers and operations teams to automate the deployment and configuration of applications, services, and infrastructure. In DevOps, it is used to manage and automate the entire application lifecycle, from development to production.
Ansible Playbooks are written in YAML and contain a set of instructions for Ansible to execute. They can be used to provision servers, install software, configure services, and perform other tasks that are needed to deploy and manage applications. Playbooks can also include variables, loops, and conditionals, making them a powerful tool for automating complex workflows.
DevOps teams use Ansible Playbooks to automate the deployment and configuration of their infrastructure and applications, which helps to reduce errors, improve consistency, and speed up the deployment process. This also helps teams to achieve faster time-to-market and more efficient use of resources, which ultimately results in better quality software and a more stable production environment.

Are Ansible Playbooks YAML?

Yes, Ansible Playbooks are written in YAML (YAML Ain’t Markup Language) which is a human-readable data serialization format. YAML is a simple, easy to read and write format, making it a popular choice for configuration management and automation. Ansible Playbooks are written in YAML because it is a simple, structured format that is easy to understand and use. The indentation-based syntax of YAML makes it easy to write and understand the structure of a playbook, and the simple structure of YAML allows Ansible to parse and execute the instructions in the playbook without the need for additional parsing or pre-processing.

Ansible Playbook Conclusion

An Ansible Playbook is an organised unit of scripts written in YAML that defines work for a configuration managed by the automation tool Ansible.

Recent Searches

ansible playbook examples
ansible-playbook command
ansible playbook hosts
ansible playbook variables
ansible playbook dry run
ansible-playbook command not found
ansible playbook tutorial
ansible playbook structure
ansible playbook roles
run ansible playbook
sample ansible playbook
run ansible playbook locally
test ansible playbook
pass variable to ansible playbook
ansible run playbook
ansible import_playbook
ansible include playbook
ansible sample playbook
ansible test playbook
ansible pass variable to playbook
ansible run playbook locally

Category: Ansible Network Automation
ansible course for network engineers
Get Access to my Ansible Course NOW
Previous Post:AZ-900 Exam
Next Post:How to Get Red Hat Enterprise Linux for Free?red hat developer network logo

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Let’s get started

Take a look at my premium courses on Ansible, Nornir & Git or buy them all with the Network Automation Bundle!

Network Automation Courses

Navigation

Python VENV Tutorial
Python for Network Engineers

Network Automation
Network Automation Courses
Network Discovery Tools
Network Automation Conferences
Ansible Training
What is Ansible?
Devops Tutorial
Network Source of Truth
DevOps Glossary
Network Monitoring Software

Contact

Contact

Get in touch with me here

[email protected]

  • Twitter
  • LinkedIn
  • YouTube
Buy me a coffeeBuy me a coffee

Copyright © 2025 · Roger Perkin · All Rights Reserved · Privacy Policy – Terms