Ansible I: Concepts and Installation

hmartinezdev profile image Héctor Martínez Originally published at hectormartinez.dev ・7 min read

Syndicated content! Check the original "Ansible I: Concepts and Installation" @ hectormartinez.dev

With this post we start a series about Ansible, the IT automation tool. This first article introduces the basic concepts of Ansible and its installation. Also we will install Docker which will help us to practice Ansible in a more realistic scenario without exiting our computer.

By the end of this article you will have:

  • Learned the basic concepts of Ansible about tasks and nodes
  • Installed the WSL and Ubuntu on Windows
  • Installed Ansible
  • Installed Docker on your computer

Note: This series is focused in Ansible on Windows but the commands can be easily ported to Linux. Ask if you have any doubt or problem!

Ansible in a few words

Ansible is an automation tool that can run actions (aka commands or tasks) in machines directly from your computer, without any special setup. To write the actions we use YAML which lowers the barrier for newcomers, since it is easy to read. Ansible has a large community and RedHat behind it so it grows more and more every day, improving the Windows support each day (which is nice for me since I work in a Windows environment).

Control Node and Managed Nodes

With Ansible you can run actions from your computer in other machines. Technically you should call Control Node to your computer and Managed Nodes to the other machines. Your computer control machines and those machines are managed by you, pretty straightforward. However here we will use the word hosts to call the Managed Hosts, since that word is too long (the community use the word hosts too, probably for the same reason).

Getting work done: Modules, Tasks, Roles & Playbooks

Modules encapsulate Ansible "actions". We can think about them as the unit of work. For example with the copy module (source and documentation) we can copy files from the control node to the hosts.

Modules need parameters to do their job. For example copy needs to know where the file that we want to copy (on your machine) and where will be copied (in the remote hosts). We can provide those using src and dest. For example:

# Welcome to YAML, use two spaces to ident
copy:  # Module name
  src: instructions.pdf  # Parameter, idented
  dest: some/remote/folder/instructions.pdf  # Parameter, idented

This alone will not work, as we should use modules inside tasks. Those basically give them a name for better comprehension, but also can be used to control the flow of execution. For example:

name: distribute PDF with instructions to all machines  # Give them an meaningful name!
copy:  # Module name
  src: instructions.pdf  # Parameter, idented
  dest: some/remote/folder/instructions.pdf  # Parameter, idented

Tasks should run inside another bigger thing called playbook. It contains tasks in a certain order and defines in which hosts those tasks will be used. An example of a playbook:

# YAML files start with three dashes, weird but true
# Playbooks are lists, so use a dash before the name of the playbook
- name: Playbook Name
  hosts: all  # We want to run against all hosts
  # Tasks is also a list, so use a dashe before each task
    - name: distribute PDF with instructions to all machines  # Task name
      copy:  # Module name
        src: instructions.pdf  # Parameter, idented
        dest: instructions.pdf  # Parameter, idented

    - name: notify everyone  # Task name
      slack:  # Module name
        channel: event-X  # Parameter, idented
        msg: Instructions are in your computers!  # Parameter, idented

This playbook will run the copy module, which will copy the instructions.pdf from our computer to the managed nodes and then will call the slack module to notify a channel about it.

Knowing who you talk to: Inventories

So how our computer knows which are the managed nodes? Ansible has the inventory. The inventory is a file in ini format that contains a list of managed nodes. Remember that hosts: all from the playbook? You can group them! For example:




So you could run your playbooks using hosts: windows or hosts: debian instead of hosts:all if you want to.

Preparing the environment

Install the Windows Subsystem for Linux (WSL)

Microsoft WSL Installation Guide

We can not install Ansible directly into Windows, but it works inside the Windows Subsystem for Linux (WSL from now on), which is kind of an "embeded" Linux inside Windows.

The WSL is a feature, so we can activate it using the interface. However we will use PowerShell to activate it. Open a PowerShell prompt with administrator rights and run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

After rebooting your computer you can install a Linux distribution from the Windows Store. I personally use Ubuntu, since I am familiar with it. Here I will be using the Ubuntu package manager, but you can find similar commands for all the package managers from other distributions.

Screenshot of Ubuntu in the Windows Store

Ubuntu in the Windows Store.

After install the distribution, type "Ubuntu" in the Search bar and open the application. The first time you will have to configure the user and the password, but it is straightforward to do it.

Screnshot of console configuring Ubuntu for the first time

First time configuration of Ubuntu

Many of the commands from now will be run inside the WSL, so be careful! If you see a bash flag over the code, it means that is run inside the WSL, as we will use bash there. If the flag is powershell we will be running in Windows

Preparing a Python virtual environment and installing Ansible

Note: Remember to run inside the distribution

Ansible runs with Python, so you will need to have Python installed. If you selected the Ubuntu distribution you will have python3 installed. If that is not the case, check how to install Python in your distribution. Then try to run:

python3 -m venv venv

This will create a virtual environment that we will use to install Ansible. Using it we will maintain our Ansible installation isolated from the global packages installed, which is nice when there are conflicting dependencies. In Ubuntu this will fail with the following error:

The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/hector/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

So run: sudo apt-get y install python3-venv and accept the installation of the proposed packages. After that you should be able run the command without problem:

python3 -m venv ansible-venv

Activate the virtual environment and install Ansible:

. ansible-venv/bin/activate
pip install ansible
ansible --help

This should return the Ansible command line help. If it worked, Ansible will probably run without problem.

Installing Docker

So now another installation before we can start playing with Ansible. Docker is a tool that use containers which isolate an environment on top of the operative system. Think about those containers as real ship containers, their contents are isolated from one another, they are inside a ship (a computer) and they are standardized.

Screnshot of a table explaning the differences between Docker and Virtual Machines

Docker is a layer on top of the operating system, while virtual machines (VM) run inside over an hypervisor and fake even the operating system

Here the paths will diverge a little depending of your flavour of Windows. If you have Windows 10 Home you will need to install Docker Toolbox. If you have Windows 10 Pro, Education or Enterprise you will need to install Docker Desktop. If you have Linux, check the Docker Installation guide for your distribution.

Docker General Install Overview

Docker ToolBox Installation

Docker for Windows Installation

Why this difference? The Docker for Windows run over Hyper-V, a Windows feature that is not avaiable in Windows 10 Home. Docker Toolbox run over VirtualBox, which can be installed without problem in any Windows 10 flavour.

The Docker installers will do the hard work for you, be sure to check the troubleshoot section in the guides if have any problem. After installing, you should be able to run the docker run hello-world that they put in the guides.

Note: We will use Docker (docker command) from the Windows side, not the Windows Subsystem for Linux. In Linux use it in the terminal, same as ansible commands.


So now you are ready to start playing with Ansible and some Docker containers. You can check the getting started guides of both tools to grasp more basic concepts and get a better idea on how to use them.

In the following article we will spin up some Docker containers and configure Ansible to run playbooks against them.

Continue reading


Editor guide