Goodnews Azonubi
Backend/DevOps Engineer
9 min read
Ansible is an open-source automation tool used for configuration management, application deployment, and orchestration. It allows system administrators and developers to automate repetitive tasks and also manage infrastructures more efficiently. Whether you're managing a few servers or a vast infrastructure, Ansible simplifies operations using human-readable YAML configuration files, also known as playbooks.
In this guide, we'll explore the basics of Ansible and get you started on your journey toward mastering IT automation.
Before diving into Ansible, ensure you meet the following prerequisites:
apt
service
copyBefore getting started with using Ansible you need to install it and configure the environment. Here's the steps to set it up on your local machine which will act as the control node.
sudo apt update
sudo apt upgrade
sudo apt install ansible
ansible --version
Ansible primarily uses SSH to manage remote servers. Therefore, you need remote access to the managed nodes for ansible to work effectively without requiring a password but using SSH key-based authentication.
1.. Generate SSH key pair
ssh-keygen -t rsa -b 4096 -C "ansible@control-node"
"Ansible control node").ansible_key.pub is the public key which will be copied to all the managed nodes. It will be copied to the authorized_keys file of the managed nodes. While the ansible_keyfile contains the private key and should not be exposed publicly.2.. Copy the public key to the managed nodes
ssh-copy-id -i ~/.ssh/your_public_key.pub user@managed_node_ip
Once SSH is configured, Ansible can manage the remote node.
The inventory file contains a list of hosts that Ansible will manage. You can specify hosts or groups of hosts in this file and if you don't create one ansible will use the inventory file default path /etc/ansible/hosts. Creating your own inventory file in Ansible is crucial for several reasons, especially in terms of organization, flexibility, and scalability in managing systems.
vi inventory
# Group of servers
[webservers]
192.168.1.10
192.168.1.11
# Group of databases
[dbservers]
db1.example.com
db2.example.com
Using the square bracket we can group the target servers into different categories like dbservers, webservers etc. Another way to identify them is using an Alias. We can achieve this by including an alias for each server at the beginning of the line (server1, server2, db1) and assigning the address of the server to the ansible_host parameter.
dns hostname or ip address of the target server.[webservers]
server1 ansible_host=192.168.1.101
server2 ansible_host=192.168.1.102
[dbservers]
db1 ansible_host=192.168.1.201
These are one-off tasks that you can execute without creating a playbook. Once Ansible is installed and your inventory file is set up, you can start running ad-hoc commands.
Ad-hoc commands are quick commands run on the managed nodes. They allow you to perform simple tasks without creating a complete playbook and these commands use the ansible command-line tool.
You can use the ping module to check connectivity to all hosts in your inventory:
-i flag so ansible doesn't try to use the default file in the /etc/ansible/hosts path.
ansible.cfg file in the current working directory. This does not overwrite the default path but only applies within this directory, as it has higher precedence.private_key_file which ansible uses to connect to all your managed nodes. You can view others that can be overridden by viewing the default Ansible configuration file located at: /etc/ansible/ansible.cfg.
ansible all -m ping
To check the disk space on all servers:
ansible all -m command -a "df -h"
Ansible modules are reusable units of code that can be used to perform specific tasks on managed nodes. Modules allow you to automate actions such as installing packages, managing services, copying files, and much more. Ansible modules are categorized into various groups based on their functionality
We will be using some of these modules in the next section..
Ansible Playbooks allow you to automate the configuration and deployment of applications on multiple servers in a predictable manner.
- which contains properties like name, hosts ans tasks.install_apache.yml or install_apache.yaml).nano install_apache.yml
A basic ansible playbook structure looks like this:
---
- name: Install and start Apache Web Server
hosts: webservers
become: yes # This enables privilege escalation (sudo)
tasks:
- name: Update Package
apt:
update_cache: yes # Ensures the apt cache is updated before installation
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache service
service:
name: apache2
state: started
ansible-playbook install_apache.yml
Play:
Managed Node: apache is installed on the managed node which has an IP address: 192.168.56.10
Default Page: Paste the IP address on your browser to view Apache default page
In this first part of the Ansible for Beginners series, we've covered the following key concepts:
In Part 2, we will delve deeper into more advanced topics, including Ansible variables, conditionals, loops, roles and more..
You can also check out my article on Protecting Sensitive Data using Ansible Vault
Let's connect on LinkedIn here