Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Install and Configure Ansible on Ubuntu 20.04?
Ansible is an open-source automation tool that simplifies software provisioning, configuration management, and application deployment. It uses a simple YAML syntax to describe tasks in playbooks and can manage multiple servers from a central location. This article will guide you through installing and configuring Ansible on Ubuntu 20.04.
Step 1: Update System Packages
Before installing Ansible, update your system packages to the latest versions. Open your terminal and execute the following commands:
sudo apt update sudo apt upgrade -y
These commands will update and upgrade all installed packages on your system.
Step 2: Install Ansible
Once system packages are updated, install Ansible using the apt package manager:
sudo apt install ansible -y
This command will install Ansible and its dependencies on your Ubuntu 20.04 system.
Step 3: Configure Ansible Hosts File
Ansible uses an inventory file (hosts file) to determine which servers to manage. The default location is /etc/ansible/hosts. Add the IP addresses or hostnames of servers you want Ansible to manage.
Open the hosts file using the following command:
sudo nano /etc/ansible/hosts
Add your servers using this format:
[webservers] server1 ansible_host=192.168.1.10 server2 ansible_host=192.168.1.11 [databases] db1 ansible_host=192.168.1.20
Here, webservers and databases are group names, and ansible_host specifies the IP address or hostname of each server. Save and close the file.
Step 4: Test Ansible Configuration
Test if Ansible is properly installed and configured by pinging all servers in your inventory:
ansible -m ping all
If configured correctly, this command will return a success message for each server. For authentication, you may need to set up SSH keys or specify connection parameters.
Step 5: Create Your First Playbook
A playbook is a YAML file containing tasks that Ansible executes on managed servers. Create a sample playbook to install Apache:
nano ~/apache-playbook.yaml
Add the following content:
---
- name: Install and start Apache
hosts: webservers
become: true
tasks:
- name: Install Apache2 package
apt:
name: apache2
state: present
update_cache: yes
- name: Start Apache2 service
service:
name: apache2
state: started
enabled: yes
This playbook targets the webservers group, uses become: true for elevated privileges, and includes tasks to install and start Apache.
Step 6: Run the Playbook
Execute your playbook with the following command:
ansible-playbook ~/apache-playbook.yaml
Ansible will connect to all servers in the webservers group and execute the defined tasks.
Step 7: Additional Configuration Options
Enhance Ansible's functionality by modifying the /etc/ansible/ansible.cfg file:
sudo nano /etc/ansible/ansible.cfg
Useful configuration options include:
[defaults] inventory = /etc/ansible/hosts remote_user = ansible host_key_checking = False timeout = 30
Key Features and Best Practices
Use SSH Key Authentication Set up passwordless SSH keys for secure, automated connections to managed servers.
Organize with Roles Structure complex playbooks using Ansible roles for better organization and reusability.
Implement Variables Use variables to make playbooks flexible and avoid hardcoding values.
Secure Sensitive Data Use Ansible Vault to encrypt passwords, API keys, and other sensitive information.
Test Before Production Use the
--checkflag to perform dry runs and validate playbooks before execution.
Common Ansible Commands
| Command | Description |
|---|---|
ansible all -m ping |
Test connectivity to all hosts |
ansible-playbook playbook.yaml --check |
Dry run of playbook |
ansible-inventory --list |
Display inventory information |
ansible all -m setup |
Gather facts about all hosts |
Conclusion
Ansible provides a powerful, agentless approach to automation and configuration management. By following these installation and configuration steps, you can begin automating server management tasks efficiently. Start with simple playbooks and gradually incorporate advanced features like roles, variables, and vault encryption as your automation needs grow.
