Tutorial series

How To Write Ansible Playbooks

How To Write Ansible Playbooks
<- Back to all series

Author(s)

Erika Heidi

By Erika Heidi

Developer Advocate

Introduction

Introduction

Ansible is a modern configuration management tool that doesn’t require the use of an agent software on remote nodes. Instead, it uses only SSH and Python to communicate and execute commands on managed servers.

Ansible allows users to manage servers in two different ways: via ad hoc commands, and via playbooks. Playbooks are YAML files containing a list of ordered tasks that should be executed on a remote server to complete a task or reach a certain goal, such as to set up a LEMP environment. Ansible playbooks allow you to fully automate server setup and application deployment, using an accessible syntax and an extensive library of built-in resources.

This series will walk you through some of Ansible’s main features which you can use to write playbooks for server automation. At the end, you’ll create a playbook to automate setting up a remote Nginx web server and deploy a static HTML website to it. The playbook examples used in this series can be found in our ansible-practice repository at the DigitalOcean Community organization on GitHub.

Prerequisites

In order to follow along with the practical examples in this series, you’ll need:

  • One Ansible Control Node: The Ansible control node is the machine we’ll use to connect to and control the Ansible hosts over SSH. Your Ansible control node can either be your local machine or a server dedicated to running Ansible, though this series assumes your control node is an Ubuntu 20.04 system. Make sure the control node has a non-root user with sudo privileges. To set this up, you can follow Steps 2 and 3 of our Initial Server Setup Guide for Ubuntu 20.04. However, please note that if you’re using a remote server as your Ansible Control node, you should follow every step of this guide. Doing so will configure a firewall on the server with ufw and enable external access to your non-root user profile, both of which will help keep the remote server secure.
  • An SSH keypair associated with your control node’s non-root user. To set this up, you can follow Step 1 of our guide on How to Set Up SSH Keys on Ubuntu 20.04.
  • One or more Ansible Hosts: An Ansible host is any machine that your Ansible control node is configured to automate. This guide assumes your Ansible hosts are remote Ubuntu 20.04 servers. Make sure each Ansible host has the Ansible control node’s SSH public key added to the authorized_keys of a system user. This user can be either root or a regular user with sudo privileges. To set this up, you can follow Step 2 of How to Set Up SSH Keys on Ubuntu 20.04.
  • Ansible installed and configured on your control node. To set up Ansible, please follow our guide on How to Install and Configure Ansible on Ubuntu 20.04.
  • A working Ansible inventory file. This inventory file should be set up on your control node and should contain all of your Ansible hosts. The guide How To Set Up Ansible Inventories explains in detail how you can create an Ansible inventory file.

Once you have met these prerequisites, run a connection test as outlined in our guide on How To Manage Multiple Servers with Ansible Ad Hoc Commands to make sure you’re able to connect and execute Ansible instructions on your remote nodes.

Tutorials in series

Tutorial

Creating and Running your First Ansible Playbook

Tutorial

How To Define Tasks in Ansible Playbooks

Tutorial

How To Use Variables in Ansible Playbooks

Tutorial

How To Access System Information (Facts) in Ansible Playbooks

Tutorial

How To Use Conditionals in Ansible Playbooks

Tutorial

How To Use Loops in Ansible Playbooks

Tutorial

Understanding Privilege Escalation in Ansible Playbooks

Tutorial

How To Install and Manage System Packages in Ansible Playbooks

Tutorial

How To Create and Use Templates in Ansible Playbooks

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.