---
title: "Efficient Deployment with Ansible Docker Container"
description: "Learn to deploy and manage Ansible Docker container efficiently with this real-world use case from ATA Learning!"
canonical: "https://adamtheautomator.com/ansible-docker-container/"
---

# Efficient Deployment with Ansible Docker Container

> Learn to deploy and manage Ansible Docker container efficiently with this real-world use case from ATA Learning!

Source: https://adamtheautomator.com/ansible-docker-container/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Efficient Deployment with Ansible Docker Container](https://adamtheautomator.com/wp-content/uploads/2022/05/Efficient-Deployment-with-Ansible-Docker-Container.jpg)

# Efficient Deployment with Ansible Docker Container

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)31 May 20225 min. read

Categories: [DevOps](/category/devops/)

Tags:[Ansible](/tag/ansible/)[Docker](/tag/docker/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating an Inventory File](#creating-an-inventory-file)
*   [Creating the Playbook to Define Tasks](#creating-the-playbook-to-define-tasks)
*   [Securing the Playbook with Ansible Vault](#securing-the-playbook-with-ansible-vault)
*   [Running the Playbook to Deploy Ansible Docker Container](#running-the-playbook-to-deploy-ansible-docker-container)
*   [Conclusion](#conclusion)

Docker containers are a popular way to package and deploy software to your servers. Docker containers offer many benefits, such as portability, isolation, and ease of use. If you wish to enhance your software development, why not deploy with an Ansible Docker container?

Ansible is a powerful tool for automating software and hardware configurations. And in this tutorial, you’ll learn how to use Ansible to deploy Docker containers efficiently.

Ready? Read on and never resort to manually deploying a container again!

## **Prerequisites**

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following.

*   A Linux machine (Ansible control node) – This tutorial uses Ubuntu 20.04, but any modern Linux distribution will work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   Two or more machines (Ansible hosts) – These hosts can be virtual machines, physical servers, or Rasberry Pis.
    
*   [Python](https://adamtheautomator.com/install-python-36/), [Docker](https://adamtheautomator.com/docker-ubuntu/), and [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html) installed on all the hosts.
    
*   [SSH keypairs](https://www.ssh.com/academy/ssh/copy-id) are set up between the control node and all the managed hosts.
    

## **Creating an Inventory File**

Before deploying containers, [Ansible](https://adamtheautomator.com/ansible-variables/) needs to identify which hosts to manage. How? You’ll create an inventory file to declare the hosts that Ansible can manage. At the same time, the file contains any variables or settings specific to those hosts.

Related:[How to Leverage Ansible Variables in Roles and Playbooks](https://adamtheautomator.com/ansible-variables/)

Inventory files can be in one of two formats, INI or YAML. But this tutorial uses the YAML format since it’s more human-readable. The inventory file is typically named _hosts_ and is stored in the Ansible project directory.

To create a _hosts_ inventory file:

1\. SSH to your control host and create a file called _hosts in the_ _/etc/ansible_ directory using your preferred text editor.

> _Note that creating a hosts file in the /etc/ansible directory requires sudo access._

Related:[Leverage SSH and PowerShell to Securely Transfer Files Now](https://adamtheautomator.com/powershell-ssh/)

2\. Next, add the following configuration to the end of the _hosts_ file. This configuration declares a group of hosts Ansible can manage.

> _The managed hosts must be reachable over SSH using the key pairs you set up in the “Prerequisites” section._

Be sure to replace the following, save the changes and close the file:

*   `server1` and `server2` with the servers’ hostnames.
    
*   <ip address> with the actual IP addresses of your managed hosts.
    

```bash
# Group name of hosts (you can rename this as you like).
[webservers]

# List of hosts (you can name these hosts "ansible_host" as you like).
server1 ansible_host=<ip address>
server2 ansible_host=<ip address>

# Sets variables applied to all hosts.
[all:vars]
# Since the default version of Python on most Ubuntu distributions is Python 2
# set Ansible to use Python 3 on all managed hosts.
ansible_python_interpreter=/usr/bin/python3
```

![Creating Inventory File (/etc/ansible/hosts)](https://adamtheautomator.com/wp-content/uploads/2022/05/image-394.png)

Creating Inventory File (_/etc/ansible/hosts_)

3\. Run the below [ansible-inventory](https://docs.ansible.com/ansible/latest/cli/ansible-inventory.html#cmdoption-ansible-inventory-host) command to verify that the inventory file (_hosts_) was created correctly.

```bash
ansible-inventory --list -y 
```

Below, you can see two groups (**webservers** and **all**), and each group contains the appropriate hosts you declared in the _hosts_ file.

![Verifying the Inventory File Exists](https://adamtheautomator.com/wp-content/uploads/2022/05/image-395.png)

Verifying the Inventory File Exists

4\. Finally, run the below command with the `ping` module to verify that Ansible can connect to your managed hosts.

```bash
ansible all -m ping -u root
```

As you can see below, the managed hosts are reachable as Ansible can connect to them. The **SUCCESS** messages indicate that the hosts are running.

![Verifying that Ansible Can Connect to the Managed Hosts](https://adamtheautomator.com/wp-content/uploads/2022/05/image-396.png)

Verifying that Ansible Can Connect to the Managed Hosts

## Creating the Playbook to Define Tasks

You now have a working inventory file, and that’s a great start. But the inventory file is not doing much right now unless you write a playbook. You’ll write a playbook that deploys a basic Docker container on your managed hosts. A playbook is a YAML file that defines a set of tasks to be carried out on managed hosts.

Related:[How to Build (And Actually Understand) a Solid Ansible Playbook](https://adamtheautomator.com/ansible-playbook-example/)

> _Ansible and Docker offer a flexible and efficient way to deploy software applications in portable containers with a playbook. As a result, the human error when manually configuring servers is eliminated, and the application environment can be replicated._

Create a new file called _playbook.yml_ in your current working directory using your preferred text editor.

Now, copy/paste the code block from a [GitHub](https://github.com/Adam-the-Automator/Scripts/blob/main/ansible-docker-container/playbook.yml) repository into the newly created _playbook.yml_ file.

> _Note that YML is indent-sensitive, so be sure to paste the code as is to the playbook.yml_ file.

Notice the code is divided into two sections:

*   The first header (`---`) contains general information which every playbook must have.
*   The second section (`tasks`) is where you write the actual tasks for Ansible to carry out on your hosts.

![Adding Code to Playbook](https://adamtheautomator.com/wp-content/uploads/2022/05/image-397.png)

Adding Code to Playbook

## **Securing the Playbook with Ansible Vault**

Now that you have a playbook, you might want to share it with other people for collaboration. But not to the point you’re also sharing sensitive information, such as passwords and API keys.

How to secure your sensitive information? Ansible Vault allows you to encrypt playbooks and variables. This way, you can share your playbooks as much as you want without sharing your secrets.

Related:[How to Secure Ansible Playbooks with Ansible Become](https://adamtheautomator.com/ansible-become/)

Suppose you want to encrypt the whole _playbook_ file so that only people with the vault password can decrypt, read, and run it. Let Ansible Vault do the trick.

1\. Run the following [ansible-vault](https://docs.ansible.com/ansible/latest/cli/ansible-vault.html) command to encrypt your playbook file (playbook.yml).

```bash
ansible-vault encrypt playbook.yml
```

When prompted, provide a secure password (vault password), as shown below. Save the password in a safe place, as you will need it later to decrypt the playbook file.

![Encrypting the Playbook](https://adamtheautomator.com/wp-content/uploads/2022/05/image-398.png)

Encrypting the Playbook

2\. Next, run the cat command to view the _playbook_ file.

```bash
cat playbook.yml
```

As you can see, the playbook file is encrypted and can only be read by someone who has the password to decrypt the file.

The $ANSIBLE\_VAULT;1.1;AES256 string at the header of the playbook file indicates that the file is encrypted with Ansible Vault using the AES256 cipher. The random string of characters at the end of the file is the encrypted version of the playbook.

![Attempting to View the Playbook File](https://adamtheautomator.com/wp-content/uploads/2022/05/image-399.png)

Attempting to View the Playbook File

3\. Lastly, run the following command to view the contents of the encrypted playbook (playbook.yml).

```bash
ansible-vault view playbook.yml
```

Provide the password you used to encrypt the file when prompted, and you’ll see the decrypted version of the file, as shown below.

Now you can share your playbook file with the right people without worrying about sharing sensitive information.

![Viewing the Encrypted Playbook](https://adamtheautomator.com/wp-content/uploads/2022/05/image-400.png)

Viewing the Encrypted Playbook

## Running the Playbook to Deploy Ansible Docker Container

Now that you have all the necessary components, you’re ready to run your playbook to deploy Docker containers on your managed hosts. Since the playbook file (_playbook.yml_) is encrypted, you’ll have to append the `--ask-vault-pass` option to ask for the vault password.

Run the following `ansible-playbook` command against your playbook (`playbook.yml`) to deploy Docker containers to your managed hosts.

```bash
ansible-playbook --ask-vault-pass playbook.yml
```

Provide the vault password, and Ansible will run the tasks in the playbook (_playbook.yml_) to create Docker containers on your hosts.

You can see below that Ansible configured both servers successfully with the following status:

*   The **ok** status indicates the task was executed without any problems.
*   The **changed** status indicates changes are made to the server configuration.<>

![Running the Playbook](https://adamtheautomator.com/wp-content/uploads/2022/05/image-401.png)

Running the Playbook

![Viewing Tasks Output](https://adamtheautomator.com/wp-content/uploads/2022/05/image-402.png)

Viewing Tasks Output

Now, SSH to each host and run the `docker` command below. This command lists all (`-a`) the running and stopped containers on the server.

```bash
docker ps -a
```

Below, you can see four containers running each host you specified in the playbook (_playbook.yml_) and their container names.

![](https://adamtheautomator.com/wp-content/uploads/2022/05/image-403.png)

Verifying Deployed Containers are Running

## Conclusion

In this tutorial, you’ve learned how to efficiently deploy Ansible Docker containers on your servers while ensuring your playbook file is encrypted using Ansible Vault. And at this point, you can now create Ansible playbooks to deploy Docker containers on your servers on your own without compromising security.

Why not use this newfound knowledge to deploy your applications on other cloud providers, such as [Amazon Web Services](https://www.ansible.com/integrations/cloud/amazon-web-services) (AWS) and [Google Cloud](https://docs.ansible.com/ansible/latest/scenario_guides/guide_gce.html) Platform (GCP)?

Or scale up your application by adding more servers to the inventory file (like hundred of servers), [grouped](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables) by region or function?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fansible-docker-container%2F&text=Efficient%20Deployment%20with%20Ansible%20Docker%20Container)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fansible-docker-container%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fansible-docker-container%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-13.webp)

### [Azure Container Apps: Instant Preview Environments per PR](/build-ephemeral-preview-environments-every-pr/)

Build isolated preview environments for every pull request using Azure Container Apps revision labels and Azure DevOps pipelines with scale-to-zero economics.

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2021/04/A-Step-by-Step-Guide-to-Getting-Started-with-Ansible-on-Windows.jpg)

### [Mastering Ansible on Windows: Your Go-To Expert Guide](/ansible-on-windows/)

Ansible on Windows made simple. A complete guide to hassle-free installation and configuration, perfect for users seeking quick and effective mastery.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
