Translation(s): English - Español - Français - српски


Ansible is a set of tools typically used for configuration management, multi-node deployment, and remote task execution. It works over SSH for managing remote nodes and it doesn't require any additional daemon to run on them. Ansible is based on Python.

Please, note that this page is not intended to be a complete Ansible guide, but rather a quick overview on how to start using Ansible in Debian. For a more detailed material on Ansible usage, please look at the official Ansible documentation page.

Ansible environment

In most cases, an Ansible environment is made by three components:

Ansible component

Description

Control node

A host in which Ansible is installed.

Inventory

A text file in the control node which contains a list of hosts to be managed by Ansible

Managed node

A host which is managed by Ansible. It can be a regular machine (virtual or physical), but also a managed networking device such as a switch or router.

Pre-requisites for using Ansible

Make sure that both the control node and the managed nodes, have the proper software requirements pre-installed:

Ansible component

Software needed

Control node

Ansible, Python interpreter, SSH client

Managed node

SSH server daemon

Ansible installation

It's assumed that you will install the needed packages from the official Debian repositories.

A Debian Control node install

Install required packages for the control node:

sudo apt install ansible

A Debian Managed node install

Install required packages for the managed node:

sudo apt install python3 python-is-python3 openssh-server

Ansible setup

Ansible configuration file

The Ansible Debian package doesn't ship with a default global Ansible configuration file. Thus, you've to create one, first.

Ansible looks for its global configuration file at many locations on the system. Creating one at your $HOME directory, should be fine in most cases. For example:

ansible-config init
# Move the created file to your $HOME
mv ./ansible.cfg ~/.ansible.cfg

The inventory file

An inventory file should exist at the control node, with a list of hosts to be managed by Ansible.

Usually, it's a file named hosts (not to be confused with /etc/hosts!) and it gets looked for in several ways:

  1. The current directory from which you execute an Ansible tool.
  2. An inventory option specified at the ansible.cfg file.

  3. From a defined environment variable (see ANSIBLE_INVENTORY) with the inventory file location path declared.

Inventory file example

In the inventory file, hosts to be managed by Ansible are organized in groups. Each group is identified with a label using square brackets. Here's an example:

[http_sites]
foo
foobar

In this case, [http_sites] is the group and each line that follows is a host declared to be managed by Ansible. Note that hostnames are being used for each host declared in this example, so it's presumed that the control node will be able to resolve them by DNS!.

Ansible authentication

Next, you should decide which authentication method the Ansible control node will use on the managed nodes.

As mentioned before, a control node with Ansible uses the SSH protocol to communicate with the managed nodes. So, both Ansible and SSH support the following authentication methods:

Authentication method

Comment

SSH key-based authentication

This is the one used by Ansible by default and the recommended one too.

Password authentication

An option can be specified at execution time from the Ansible control node to use password authentication.

Default user to perform the auth

Take into account that Ansible, by default, will use the same username you are using on the control node to perform the auth against the managed nodes.

Ansible tools

Ansible provides several CLI tools to interact and perform actions on the managed nodes.

Ansible Modules

Ansible Modules are the basic building blocks for every action supported and executed by Ansible on managed nodes.

The most basic way to use a module is by invoking (in Ansible terms) an ad hoc command.

Use of ad hoc commands

An ad hoc command is used to perform a single task on one or more managed nodes. Because ad hoc commands are quick and easy but not reusable, they are well suited for tasks that are rarely repeated.

You make use of an ad-hoc command by invoking the /usr/bin/ansible executable.

Here's a simple example:

ansible localhost -m ping

It uses the Ansible ping module on the control node. In this case, a successful response should look like this:

localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Ansible Playbooks

Ansible Roles


License/Copyright

License

CC-BY-SA 4 (or 1 from DFSGLicenses)

Authors

?MatíasTeplitzky


CategorySystemAdministration