Ansible 101 : Core Components

ELMASLOUHY Mouaad
4 min readNov 20, 2022

--

Ansible is a powerful, agentless, open source [link] tool that automate everything from code deployment to network configuration to cloud management.

Collections [link]

Ansible collections are a distribution format for Ansible content that can include playbooks, roles, modules, and plugins.

We can search for collection in the ansible galaxy [link] .

We install new collection or upgrade our installed collections by the command ansible-galaxy collection install my_namespace.my_collection:version

The ansible-galaxy[link] utility perform various Role and Collection related operations.

Installing collecion example

Modules [link]

Modules are discrete units of code that can be used from the command line or in a playbook task, modules are hosted in collections. We can execute modules from the command line ansible -m ping localhost .

The ansible-doc[link] utility displays information on modules installed in Ansible libraries. example ansible-doc ping .

ping module doc example [link]

Facts [link]

Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more. You can access this data in the ansible_facts variable within your tasks.

To show the facts about the local server you can run the command ansible -m setup localhost

Gathered information example

Inventories [link]

Ansible works against multiple hosts in your infrastructure at the same time, using a list or group of lists known as inventory, it can be written in ini, yaml, toml, json formats.

The location of the inventory file is by default in/etc/ansible/hosts and we can define our inventories in the current folder or by Using multiple inventory sources, or pull inventory from dynamic or cloud sources as described in Working with dynamic inventory.

Inventory file example

The ansible-inventory[link] utility display or dump the configured inventory as Ansible sees it.

Configuration Files [link]

Ansible supports several sources for configuring its behavior which will be searched for in the following order:

  • ANSIBLE_CONFIG (environment variable if set)
  • ansible.cfg (in the current directory)
  • ~/.ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg

The ansible-config [link] utility allows users to see all the configuration settings available, their defaults.

Configuration file example

Playbooks [link]

Playbooks are expressed in YAML format, they are composed of one or multiple ‘plays’, and plays are composed of one or more tasks.

The ansible-playbook[link] utility run ansible playbooks and executing the defined tasks on the targeted hosts.

Playbook example

Variables [link]

Ansible uses variables to manage differences between systems. You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line. You can also create variables during a playbook run by registering the return value or values of a task as a new variable.

Variable definition example

Roles [link]

Ansible role let us reuse already created vars, files, tasks, handlers, and other Ansible artifacts that has a defined directory structure with eight main standard directories.

Role Structure example

We can search for roles in the ansible galaxy [link], and we can install it by the command ansible-galaxy install my_namespace.role_name,version

Installing a role example

Thanks for Reading!

--

--

ELMASLOUHY Mouaad
ELMASLOUHY Mouaad

Written by ELMASLOUHY Mouaad

Computer science Engineer Student, A lover of everything that urges the mind to work hard such as Quantum Physics, General Medicine, Personal dev…

No responses yet