How to use Ansible: A Reference Guide

Published on March 25, 2023 · Updated on March 24, 2023
How to use Ansible: A Reference Guide

Ansible is a powerful automation tool that enables you to manage and automate your IT infrastructure. This guide will provide you with a reference to the essential concepts and practical applications of using Ansible.

How to Use This Guide:

This guide is structured to provide you with step-by-step instructions on how to use Ansible to manage and automate your IT infrastructure. Each section of this guide will cover a specific topic, ranging from connecting to nodes to running playbooks.

Testing Connectivity to Nodes

Before running Ansible playbooks on a node, you need to test connectivity to the node. You can test connectivity using the ping module, which is included in Ansible.

ansible all -m ping

Connecting as a Different User

To connect to a node as a different user, you need to specify the remote user using the -u flag:

ansible all -m ping -u username

Using Password-Based Authentication

By default, Ansible uses SSH keys to authenticate to nodes. However, you can also use password-based authentication by specifying the password using the –ask-pass flag:

ansible all -m ping --ask-pass

Using a Custom SSH Key

If you want to use a custom SSH key to authenticate to nodes, you can specify the path to the SSH key using the –private-key flag:

ansible all -m ping --private-key=/path/to/ssh/key

Providing the sudo Password

To execute commands as sudo, you need to provide the sudo password using the –ask-become-pass flag:

ansible all -m command -a "whoami" --become --ask-become-pass

Using a Custom Inventory File

By default, Ansible uses the /etc/ansible/hosts file as the inventory file. However, you can specify a custom inventory file using the -i flag:

ansible all -m ping -i /path/to/inventory/file

Running Ad-Hoc Commands

Ad-hoc commands allow you to execute a single task on one or more nodes. For example, the following command will display the disk space on all nodes:

ansible all -m shell -a "df -h"

Running Playbooks

Playbooks are Ansible’s configuration, deployment, and orchestration language. You can run a playbook using the ansible-playbook command:

ansible-playbook playbook.yml

Getting Information about a Play

You can get information about a play by using the –list-tasks and –list-hosts flags:

ansible-playbook playbook.yml --list-tasks --list-hosts

Controlling Playbook Execution

You can control playbook execution by using tags, limiting plays to specific hosts, and running tasks in parallel. For example, to run only tasks tagged with “setup” on the “webserver” host, use the following command:

ansible-playbook playbook.yml --tags setup --limit webserver

Prerequisites

Before you begin, you should have a non-root user account with sudo privileges on your Ubuntu 20.04 server.

Installing Ansible using apt-get

To install Ansible on Ubuntu 20.04, follow these steps:

Update your package index and install the required dependencies:

sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible

Install ansible with:

sudo apt install ansible

To verify the Ansible installation you can use:

ansible --version

The output should look similar to this:

ansible 2.9.1
  config file = /etc/ansible/ansible.cfg

This show us that Ansible is indeed correctly installed.

Generate SSH keys and share it among managed nodes

After the installation is complete we can set up SSH Keys for ansible. Using SSH Keys for Ansible is recommended and allows us to not enter a password for each connection plus it adds an extra layer of security.

To generate a SSH Key, we can run the following command:

ssh-keygen -o -a 100 -t ed25519 -C "[email protected]"

This will create a ED25519 key. This is the most recommended assymetric key algorithm today!

Copy the public key to the managed nodes:

ssh-copy-id username@remote_host

Test the connection with:

ssh username@remote_host

Close the connection.

Setting Up the Inventory File

Ansible uses an inventory file to define the managed nodes and their properties. To set up the inventory file, follow these steps:

Create a new file named hosts in /etc/ansible/ directory:

sudo nano /etc/ansible/hosts

Add the IP addresses or hostnames of your managed nodes to the file, for example:

[webserver]
192.168.1.10

Save and close the file.

Running Ad-Hoc Commands

You can use Ansible to run ad-hoc commands on your managed nodes. For example, to check the uptime of the managed nodes, run the following command:

ansible all -a uptime

Create ansible cfg and inventory file

If you prefer to use a different inventory file or change the default settings of Ansible, you can create a configuration file named ansible.cfg in your working directory. You can also create a custom inventory file and specify it in the ansible.cfg file.

Create a Demo Ansible playbook

Ansible uses playbooks to define the tasks and workflows that should be executed on the managed nodes. To create a demo Ansible playbook, follow these steps:

Create a new file named demo.yml in your working directory:

nano demo.yml

Add the following content to the file:

- hosts: webserver
  tasks:
  - name: Install Nginx
    apt: name=nginx state=present

To run the playbook you type:

ansible-playbook demo.yml

Conclusion

In this tutorial, we have shown you how to install Ansible on Ubuntu 20.04 and get started with basic usage. Ansible is a powerful automation tool that can help you save time and effort in managing your infrastructure. We encourage you to explore the advanced features of Ansible and create your own playbooks and modules.

Load Comments