Ansible II (Inventory files, commands & playbooks)

Alex Izuka
5 min readOct 20, 2023

We started a series explaining all you need to know about Ansible. We will proceed further in the series by explaining some things to know about Inventory files, some basic Ansible commands, and playbooks.

Defining and managing inventory files

Defining and managing inventory files in Ansible is essential for specifying your target hosts and organizing them into groups. Inventory files are typically written in INI or YAML format. We will explore both methods below.

INI Format Inventory: Create an INI format inventory file, typically named inventory.ini, to define and manage your target hosts and groups. We explain below how to go about it.

# This is a comment

[web]
webserver1 ansible_host=192.168.1.101 ansible_user=ansibleuser
webserver2 ansible_host=192.168.1.102 ansible_user=ansibleuser

[db]
dbserver1 ansible_host=192.168.1.201 ansible_user=ansibleuser

In this INI format inventory:

  • [web] and [db] are group names.
  • webserver1, webserver2, and dbserver1 are the hostnames or aliases for your target hosts.
  • ansible_host specifies the IP address or hostname of the target host.
  • ansible_user specifies the SSH user Ansible should use to connect to the host.

YAML format: YAML format inventory files provide more flexibility and are usually named inventory.yml. We explain with an example below how it’s written.

all:
children:
web:
hosts:
webserver1:
ansible_host: 192.168.1.101
ansible_user: ansibleuser
webserver2:
ansible_host: 192.168.1.102
ansible_user: ansibleuser
db:
hosts:
dbserver1:
ansible_host: 192.168.1.201
ansible_user: ansibleuser

In this YAML format inventory:

  • all is the top-level group.
  • web and db are child groups under all.
  • webserver1, webserver2, and dbserver1 are hostnames.
  • ansible_host specifies the target host's IP address or hostname.
  • ansible_user specifies the SSH user to use.

Ansible’s ad-hoc commands

Ansible’s ad-hoc commands are a powerful feature that allows you to perform quick, one-off tasks on remote systems without the need for writing full-blown playbooks. Ad-hoc commands are especially useful for simple and immediate tasks, like gathering information, making small changes, or executing a single command on remote hosts. They follow this basic syntax:

ansible <target_host_pattern> -m <module_name> -a "<module_arguments>"
  • <target_host_pattern>: Specifies the target hosts, either as individual hostnames or as patterns defined in your inventory file.
  • <module_name>: Identifies the Ansible module to use for the task.
  • <module_arguments>: Contains the module-specific arguments and options.

Here are a few examples of how you can use Ansible ad-hoc commands for simple tasks:

  1. Gathering System Facts: You can use the setup module to collect various system facts from remote hosts. For example, to get facts about all hosts in your inventory you can use the command below.
ansible all -m setup

2. Running Shell Commands: You can use the command module to execute shell commands on remote hosts. For example, to list the contents of a directory:

ansible <target_host> -m command -a "ls /path/to/directory"

3. Managing Packages: You can use the yum or apt modules to install or remove packages. For example, to install a package on CentOS:

ansible <target_host> -m yum -a "name=<package_name> state=present"

4. Copying Files: The copy module allows you to copy files from the control machine to remote hosts. For example, to copy a local file to a remote host:

ansible <target_host> -m copy -a "src=/path/to/local/file dest=/path/on/remote/file"

5. Managing Services: You can use the service module to start, stop, or restart services. For instance, to restart the Apache web server:

ansible <target_host> -m service -a "name=httpd state=restarted"

6. Managing Users: The user module enables user management tasks. For example, to create a new user:

ansible <target_host> -m user -a "name=<username> password=<hashed_password>

7. Gathering Disk Space Information: You can use the shell module to run a shell command that gathers disk space information. For example, to check disk usage:

ansible <target_host> -m shell -a "df -h"

Remember to replace <target_host> with the hostname or target host pattern from your inventory.

Playbooks & their YAML structure

Playbooks in Ansible are configuration files written in YAML (Yet Another Markup Language) format. They define a set of tasks to be executed on remote hosts, as well as the hosts on which these tasks should be performed. Playbooks are at the core of Ansible’s automation and orchestration capabilities. They allow you to describe the desired state of the system and the steps to achieve that state. They are declarative in operation; which means you define the endpoint of what you want on the YAML file and it outputs that for you, more reason Ansible is called IaC (Infrastructure as Code).

YAML structure of Ansible playbooks

  1. Playbooks Begin with a List of Plays: A playbook can contain one or more plays. Each play represents a set of tasks to be executed on a group of hosts. We illustrate that below.
---
- name: Playbook Name
hosts: target_group
tasks:
- name: Task 1
# Task details
- name: Task 2
# Task details
  • name: A human-readable name for the playbook, which helps to identify its purpose.
  • hosts: Specifies the group(s) of hosts where the tasks in this play will be executed.

2. Play-Level Variables (Optional): You can define variables specific to a play that overrides or complements host or group variables. We illustrate that below.

---
- name: Playbook Name
hosts: target_group
vars:
var_name: var_value
tasks:
- name: Task 1
# Task details

3. Tasks: The tasks section contains a list of tasks to be executed. Each task consists of:

  • A name field for describing the task.
  • A module to be executed (e.g., shell, command, apt, copy).
  • Module-specific parameters and arguments.

We illustrate the usage below.

---
- name: Playbook Name
hosts: target_group
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: present
- name: Start Apache service
service:
name: apache2
state: started

Each module has specific parameters (or arguments) that you configure based on the task you want to perform. For example, the apt module takes parameters like name and state, and the service module takes parameters like name and state.

4. Handlers (Optional): Handlers are special tasks that are executed when notified by other tasks. They are typically used for tasks that should only run if changes have been made.

---
- name: Playbook Name
hosts: target_group
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: present
notify:
- Start Apache
handlers:
- name: Start Apache
service:
name: apache2
state: started

Conclusion

We have looked at how to manage inventory files, ad hoc commands, and the structure of playbooks written in YAML files. You can check the first part of this series here where we discussed Ansible installation and configuration.

--

--