What is an Ansible Playbook?

Copy URL

An Ansible® Playbook is a blueprint of automation tasks, which are IT actions executed with limited manual effort across an inventory of IT solutions. Playbooks tell Ansible what to do to which devices.

Instead of manually applying the same action to hundreds or thousands of similar technologies across IT environments, executing a playbook automatically completes the same action for the specified type of inventory—such as a set of routers. Playbooks also serve as frameworks of prewritten code that developers can use ad-hoc or as a starting template.

Playbooks are regularly used to automate IT infrastructure—such as operating systems and Kubernetes platforms—networks, security systems, and code repositories like GitHub. IT staff can use playbooks to program applications, services, server nodes, and other devices, without the manual overhead of creating everything from scratch.

And playbooks—as well as the conditions, variables, and tasks within them—can be saved, shared, or reused indefinitely. This makes it easier for IT teams to codify operational knowledge and ensure that the same actions are performed consistently.

Write your first playbook with this free interactive lab

Ansible Playbooks are lists of tasks that automatically execute for your specified inventory or groups of hosts. One or more Ansible tasks can be combined to make a play—an ordered grouping of tasks mapped to specific hosts—and tasks are executed in the order in which they are written. A playbook can include 1 or more plays as well as Ansible Roles—bundles of tasks and associated automation assets that can be run in multiple plays or reused across playbooks.

Tasks are executed by modules, each of which performs a specific task in a playbook. A module contains metadata that determines when and where a task is executed, as well as which user executes it. There are thousands of Ansible modules that perform all kinds of IT tasks, such as:

Cloud management

oci_vcn creates, deletes, or updates virtual cloud networks in Oracle Cloud Infrastructure environments. Similarly, vmware_cluster adds, removes, or updates VMware vSphere clusters.

User management

selogin maps Linux® operating system (OS) users to SELinux user, and gitlab_user creates, updates, or deletes GitLab users.

Networking

Dozens of modules handle application programming interfaces (APIs); Cisco IOS, NXOS, and IOS XR devices; as well as F5 BIG-IP services and the Arista EOS cloud network operating system.

Security

Openssh_cert generates an OpenSSH host or user certificates, and ipa_config manages global FreeIPA configuration settings.

Configuration management

pip manages Python library dependencies while assemble consolidates configuration files from fragments.

Communication

mail can automatically send emails based on certain criteria, and snow_record creates, deletes, or updates a single record in ServiceNow.

Learn the playbook basics with this free learning path

Red Hat resources

Ansible is capable of communicating with many different device classifications, from cloud-based REST APIs, to Linux and Windows systems, networking hardware, and much more. This is a sample of 2 Ansible modules automatically updating 2 types of servers:

---
- name: Update web servers
  hosts: webservers
  become: true
 
  tasks:
    - name: Ensure apache is at the latest version
      ansible.builtin.yum:
        name: httpd
        state: latest
    - name: Write the apache config file
      ansible.builtin.template:
        src: /srv/httpd.j2
        dest: /etc/httpd.conf
        mode: "0644"
 
- name: Update db servers
  hosts: databases
  become: true
 
  tasks:
    - name: Ensure postgresql is at the latest version
      ansible.builtin.yum:
        name: postgresql
        state: latest
    - name: Ensure that postgresql is started
      ansible.builtin.service:
        name: postgresql
        state: started

The playbook contains 2 plays: 

  • The first checks whether or not web server software is up to date and runs the update if necessary.
  • The second checks whether or not database server software is up to date and runs the update if necessary.

Get more details on this playbook

Ansible uses the YAML syntax. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym). There are also 2 different, but perfectly acceptable YAML file extensions: .yaml or .yml. 

There are 2 ways of using Ansible Playbooks: from the command line interface (CLI) or using the Red Hat® Ansible Automation Platform’s push-button deployments.

Because YAML is a human-readable language, IT professionals with specific domain expertise—like in network, security, or cloud—can create playbooks without having to learn a complicated coding language.

From the CLI

After installing the open source Ansible project or Red Hat Ansible Automation Platform—which is as straightforward as typing `sudo yum install ansible` in the Red Hat Enterprise Linux CLI—all you need to do is use the ansible-playbook command to run Ansible Playbooks.

From within the platform

The Red Hat Ansible Automation Platform web-based user interface includes push-button Ansible Playbook deployments that are used as part of larger jobs or job templates. These deployments come with additional safeguards that are particularly helpful to users who are newer to IT automation—or those without as much experience working in the CLI.

A quick guide to running your own playbook

Red Hat Ansible Automation Platform is a unified platform that can automate full processes across your IT domains for a variety of use cases—including infrastructure, hybrid cloud, security, and networks, all the way to edge locations. Ansible Automation Platform includes all the tools needed to implement enterprise-wide automation, including an event-driven solution, playbooks, and analytics. And it allows your teams to centralize and control your IT infrastructure with a visual dashboard, role-based access control, and other features that will help reduce operational complexity.

With a Red Hat subscription, you get certified, supported content from Red Hat and our partners, access to hosted management services, and life cycle technical support to scale automation across your organization. And you’ll get expert knowledge gained from our success with thousands of customers.

The introduction of Red Hat Ansible Lightspeed with IBM watsonx Code Assistant makes Ansible even more accessible to beginners and helps experienced automation teams learn, create, and maintain Ansible Automation Platform content more efficiently. This generative AI service accepts prompts entered by a user and then interacts with IBM watsonx foundation models to produce code recommendations built on Ansible best practices—helping you convert subject matter expertise into trusted, reliable Ansible code that scales across teams and domains.

Learn more about Ansible Automation Platform

 

Icon-Red_Hat-Media_and_documents-Quotemark_Open-B-Red-RGB The built-in capabilities of Red Hat Ansible Automation Platform provide an accelerator in a box. It’s the de facto standard that many of our vendors and partners also use to write scripts to install, configure, and maintain their technology.

Jesse Amerson

IT director, Ulta Beauty

Get the full story

Hub

The official Red Hat blog

Get the latest information about our ecosystem of customers, partners, and communities.

All Red Hat product trials

Our no-cost product trials help you gain hands-on experience, prepare for a certification, or assess if a product is right for your organization.

Keep reading

Why choose Red Hat Ansible Automation Platform as your AI foundation?

Red Hat® Ansible® Automation Platform establishes a solid foundation for AI implementations by simplifying the deployment, management, configuration, and lifecycle of AI models and infrastructure components.

How to build an IT automation strategy

Isolated automation can only take you so far. To expand automation to all areas of your IT operations, you need an automation strategy that unifies teams, processes, and disconnected workflows.

What is virtual infrastructure management? And how can automation help?

Virtual infrastructure management is the coordination of software, IT resources, and other tools to manage virtual machines and related IT environments throughout their entire lifecycle.

Automation and management resources

Featured product

Related articles