Ansible Essentials: Overview and Usage
Introduction:
Ansible is an open-source automation tool that simplifies the management and configuration of IT infrastructure through declarative code, eliminating the need for manual intervention. It utilizes SSH for communication and employs a client-less architecture.
Connection Method:
- Ansible employs SSH (Secure Shell) for communication with remote machines, ensuring secure access.
- SSH keys are used for authentication, providing a secure and automated way to connect to hosts.
Configuration Files:
- Inventory File (
/etc/ansible/hosts): Contains information about the hosts managed by Ansible. - Configuration File (
/etc/ansible/ansible.cfg): Stores configuration settings for Ansible.
Facts:
- Information collected by Ansible when executed on remote machines.
- Can be accessed using the setup module, providing details about the system's configuration.
- Allows conditional execution based on the target machine's characteristics.
Indepotence:
- Ansible ensures that running a playbook multiple times produces the same result, regardless of the initial state of the system.
- This property ensures predictability and reliability in configuration management tasks.
Documentation:
- Comprehensive documentation is available at docs.ansible.com.
- Accessible via the
ansible-doccommand, providing detailed information about modules, options, and usage examples.
Playbooks:
- YAML files used to define configurations and tasks to be executed on remote machines.
- Enables the management and automation of complex configurations.
- Allows for the transfer of files, creation of users, installation of packages, and more.
Variables:
- Define dynamic values to be used within playbooks, enhancing flexibility and re-usability.
- Can be stored in separate files and referenced within playbooks.
- Directly usable from the command line, enabling runtime customization.
Loops:
- Iterative structures allowing the execution of tasks multiple times.
- Useful for installing packages, managing users, or performing similar actions across multiple hosts.
Conditional Execution:
- Enables the execution of tasks based on specified conditions.
- Enhances playbook flexibility by allowing tasks to be performed selectively.
- Example usage includes installing packages only on Debian machines.
Install Ansible on your control node:
Ansible can be installed on various operating systems by following the official documentation:

Inventory File:
The inventory file in Ansible contains information about the hosts you want to manage. By default, Ansible looks for the inventory file at /etc/ansible/hosts, but you can specify a different inventory file using the -i option.
Here's an example of what an inventory file might look like:
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
db2.example.com
[loadbalancers]
lb1.example.comhosts
In this example:
webservers,databases, andloadbalancersare group names.- Each group contains a list of
hostnamesorIP addressesbelonging to that group. - You can also define host variables, group variables, and aliases in the inventory file.
For example, if you want to target a group named webservers, you would run:
ansible webservers -m pingAnd if you want to target individual hosts, you can specify their names:
ansible web1.example.com -m pingUnderstanding and effectively using host patterns allows you to efficiently manage your infrastructure with Ansible.
Command Structure:
ansible -m <MODULE> -a <PARAMETERS> <HOST>: Executes a module with specified parameters on the specified host(s).ansible <HOST> -m <MODULE> -a <PARAMETERS>: Executes a module with specified parameters on the specified host.
Now try to create your first configuration file based on your own servers.
Now, let's apply some command line examples:
Ping all hosts:
ansible all -m pingExecute a shell command to display the contents of /etc/hosts on Debian hosts:
ansible debian -m shell -a "cat /etc/hosts"Install the package ncdu on Debian hosts:
ansible debian -m apt -a "name=ncdu state=present update_cache=yes" --becomeRestart and enable the crond service on CentOS hosts:
ansible centos -m service -a "name=crond state=restarted enabled=yes" --becomeExplanation:
-mspecifies the Ansible module to use.-apasses arguments to the module.--becomeis used to execute tasks with escalated privileges (usually via sudo).all,debian,centosare host patterns. You can replace them with your own inventory group or individual host names.
Next Steps:
Continue exploring Ansible's capabilities by practicing with more complex playbooks, experimenting with different modules, and integrating Ansible into CI/CD pipelines. Focus on best practices for organizing playbooks, handling errors, securing your Ansible environment, and troubleshooting issues effectively. Additionally, consider exploring Ansible Tower/AWX for centralized management and scaling automation efforts.
