Skip to content

Ansible Labs


Getting Started Tip

Choose the preferred way to run the labs. If you encounter any issues, please contact your instructor.

Intro

  • This tutorial is for teaching Ansible through hands-on labs designed as practical exercises.
  • Each lab is packaged in its own folder and includes the files, playbooks, and assets required to complete the lab.
  • Every lab folder includes a README that describes the lab’s objectives, tasks, and how to verify the solution.
  • The Ansible Labs are a series of Ansible automation exercises designed to teach players Ansible skills & features.
  • The inspiration for this project is to provide practical learning experiences for Ansible.

Pre-Requirements

  • This tutorial will test your Ansible and Linux skills.
  • You should be familiar with the following topics:
    • Basic Linux commands
    • Linux File system navigation
    • Basic knowledge of Docker (if you choose to run it with Docker)
    • Basic knowledge of YAML
  • For advanced Labs:
    • Ansible basics (inventory, playbooks, modules)

Usage

  • There are several ways to run the Ansible Labs.
  • Choose the method that works best for you.
    • Killercoda (Recommended)
    • 🐳 Docker
    • πŸ“œ From Source
    • Using Google Cloud Shell
  • The easiest way to get started with the labs
  • Learn Ansible in your browser without any local installation

🌐 Launch on Killercoda

Benefits:

  • No installation required
  • Pre-configured environment
  • Works on any device with a web browser
  • All tools pre-installed

Using Docker is the easiest way to get started locally with the labs:

# Change to the Labs directory
cd Labs/000-setup

# Run the setup lab using Docker Compose
docker-compose up -d

Prerequisites:

  • Docker and Docker Compose installed on your system
  • No additional setup required

For those who prefer to run it directly on their machine:

# Clone the repository
git clone https://github.com/nirgeier/AnsibleLabs.git
# Change to the Labs directory
cd AnsibleLabs/Labs
# Start with the setup lab
cd 000-setup
# Follow the instructions in the README of each lab
cat README.md
Prerequisites:

  • Ansible installed on your system
  • A Unix-like operating system (Linux, macOS, or Windows with WSL)
  • Basic command-line tools
  • Google Cloud Shell provides a free, browser-based environment with all necessary tools pre-installed.
  • Click on the Open in Google Cloud Shell button below:

Open in Cloud Shell

  • The repository will automatically be cloned into a free Cloud instance.
  • Use CTRL + click to open it in a new window.
  • Follow the instructions in the README of each lab.

Benefits:

  • No local installation required
  • Pre-configured environment
  • Works on any device with a web browser
  • All tools pre-installed
  • Free tier available

Lab Breakdown

  • If you choose to run the labs locally using Docker or From Source, follow the steps below to set up your environment.
  • Make sure you have the necessary tools installed.
  • Follow the instructions in the README of each lab.
  • Review the Dockerfile(s) and docker-compose.yml for container configurations.

02. Build From Source

  • Clone the git repo: git clone https://github.com/nirgeier/AnsibleLabs.git
  • Navigate to the Labs directory: cd AnsibleLabs/Labs/000-setup
  • The lab contains the docker-compose file along with the Dockerfile(s) The containers are based upon ubuntu and are published to DockerHub as well.
  • Build the demo containers
  • The docker-compose will create ansible-controller which will server as our controller to execute ansible playbooks on our demo servers defined by the names linux-server-X

Labs containers

Container Content
🐳 ansible-controller Linux container with ansible installed
🐳 linux-server-1 Linux container with ssh only (no ansible installed)
🐳 linux-server-2 Linux container with ssh only (no ansible installed)
🐳 linux-server-3 Linux container with ssh only (no ansible installed)
  • For the demo we will also need a shared folder(s) where the certificates and the configuration will be stored
  • The shared folder(s) will be mounted into the containers
  • The containers will have access to the shared folder(s) for reading and writing files
  • The shared folder(s) will be used to store the Ansible playbooks and inventory files
  • The shared folder(s) will be mounted at /labs-scripts in the containers


  • Ensure you have the necessary permissions to run Docker commands or Ansible playbooks on your system.
  • Enjoy, and don’t forget to star the project on GitHub!

Preface

What is Ansible?

  • Ansible is an open-source automation tool for IT tasks such as configuration management, application deployment, and task automation.
  • Ansible is Configuration Management tool which manage the state of our servers, install the required packages and tools.
  • Other optional use cases can be deployments, Provisioning new servers
  • The most powerful feature of Ansible is the ability to manage huge scale of servers regardless of their infrastructure (on prem, cloud, vm etc)
  • Ansible uses SSH to connect to servers and execute tasks defined in YAML playbooks, making it agentless and easy to use.
  • Ansible is widely used for managing infrastructure, ensuring consistency, and automating repetitive processes across various environments (on-premises, cloud, VMs, etc.).

How Ansible Works

Ansible Architecture Diagram

  • Ansible is an agentless tool.
  • Ansible uses ssh for pulling modules and for managing the nodes
  • Ansible is based upon YAML
  • An Ansible playbook is a file that contains a set of instructions that Ansible can use to automate tasks on remote hosts.
  • Playbooks are written in YAML, a human-readable markup language. A playbook typically consists of one or more plays, a collection of tasks run in sequence.

How Ansible Playbooks Work

  • Here’s a brief overview of how Ansible playbooks work:
  • Playbook Structure

    • A playbook is composed of one or more plays in an ordered list.

    • Each play executes part of the overall goal of the playbook, running one or more tasks.

    • Each task calls an Ansible module.
  • Playbook Execution

    • A playbook runs in order from top to bottom.

    • Within each play, tasks also run in order from top to bottom.

    • Playbooks with multiple plays can orchestrate multi-machine deployments.

  • Task Execution

    • Tasks are executed by modules, each of which performs a specific task in a playbook.

    • There are thousands of Ansible modules that perform all kinds of IT tasks.

  • Reusability

    • Playbooks offer a repeatable, reusable, simple configuration management and multi-machine deployment system.

    • If you need to execute a task with Ansible more than once, write a playbook and put it under source control.

  • Playbooks are one of the core features of Ansible and tell Ansible what to execute.
  • They are used in complex scenarios.
  • They serve as frameworks of pre-written code that developers can use ad-hoc or as a starting template.
  • They can be saved, shared, or reused indefinitely, making it easier for IT teams to codify operational knowledge and ensure that the same actions are performed consistently.