Introduction
Ansible is an effective configuration management, application distribution, and automation tool. It is agentless, user-friendly, and supports multiple environments, making it the most suitable for automating IT infrastructure activities. In this document, we are discussing how to set up Ansible in a Linux-based system such as RHEL, CentOS, or Ubuntu.
1. Understanding Ansible: A Brief Overview
Ansible makes IT automation easier by pushing information to the servers through SSH. It doesn’t have any agents and does not require specific software on your managed nodes yet, the automation tasks can be described in human-readable YAML format. The result is that this method reduces complexity while improving the maintainability of your automation scripts.
Key Features of Ansible:
- Agentless Architecture: No need for additional software on the managed nodes.
- Declarative Syntax: Easy-to-read YAML files define your infrastructure as code (IaC).
- Scalability: Manage thousands of servers with the same simplicity as managing one.
- Integration: Seamlessly integrates with CI/CD pipelines and cloud providers like AWS, Azure, and Google Cloud.
2. Prerequisites
Before you begin, ensure that you have the following:
- A Linux server (RHEL/CentOS 7/8 or Ubuntu 20.04+).
- A non-root user with sudo privileges.
- Python installed (Ansible requires Python 2.7 or 3.5+).
3. Installing Ansible
On RHEL/CentOS
- Add the EPEL repository:
sudo yum install epel-release -y
- Install Ansible:
sudo yum install ansible -y
Verify the Installation
After installation, verify Ansible is installed correctly by checking its version:
ansible –version
You should see the output displaying the version of Ansible and the Python interpreter it’s using.
4. Configuring Ansible
The /etc/ansible/ansible.cfg file is where the Ansible configuration is managed. It is in this file that you can specify different settings such as default modules, SSH settings, and inventory file location. By default, there are many utilities to suffice usual setups, although they may be altered if necessary.
Setting Up the Inventory
Ansible works by connecting to your nodes (servers) listed in an inventory file. By default, the inventory is located at /etc/ansible/hosts.
Here’s a simple example of an inventory file:
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
This file groups your servers into categories (webservers and dbservers) that you can reference in your Ansible playbooks.
Configuring SSH Access
Ansible uses SSH to communicate with the servers listed in your inventory. Ensure you have SSH access configured:
- Generate SSH keys (if not already created):
ssh-keygen
- Copy your public key to the remote servers:
ssh-copy-id user@remote_server
Ansible Configuration File
The Ansible configuration file, /etc/ansible/ansible.cfg, can be customized to fit your environment. Here are some key settings:
- Inventory File Location: Set a custom path for the inventory file.
inventory = /path/to/your/inventory
- SSH User: Set the default SSH user for all connections.
remote_user = your_user
- Private Key File: Specify the path to your SSH private key.
private_key_file = /path/to/private/key
- Timeout: Adjust the SSH connection timeout.
timeout = 30
5. Running Your First Playbook
A playbook is a YAML file containing one or more “plays” that define automation tasks. Here’s an example of a basic playbook:
– name: Install and start Apache
hosts: webservers
become: yes
tasks:
– name: Install Apache
yum:
name: httpd
state: present
– name: Start Apache
service:
name: httpd
state: started
To run the playbook:
ansible-playbook -i /path/to/your/inventory /path/to/playbook.yml
- 6. Advanced Configuration
As you become more familiar with Ansible, you may want to explore advanced features:
- Roles: Organize your playbooks into reusable components.
- Vault: Secure sensitive data (e.g., passwords) using Ansible Vault.
- Callbacks and Plugins: Customize behavior with Ansible’s plugins and callbacks.
Conclusion
There’s nothing complicated about deploying and configuring Ansible, which makes it simple for you to automate your IT infrastructure. Regardless of whether there are only several of them or more than a thousand, Ansible will enable you to simplify the management process. Try out some basic playbooks first, and once you become more accustomed to it, dig deeper into what else is possible with Ansible to make the most out of it.