In this tutorial, we're getting started with Ansible installation and basic configuration on Ubuntu 24.04 server.
Ansible is a powerful open-source automation tool that simplifies IT tasks like configuration management, application deployment, and orchestration. It uses SSH to connect to remote machines, allowing you to automate tasks without installing agents on target systems. In this guide, you will learn how to install Ansible on popular Linux distributions, set up an inventory file, and run some basic Ansible commands.
Prerequisites:
- A Ubuntu 24.04 installed dedicated server or KVM VPS
- A root user access or normal user with sudo rights
- Basic knowledge of Linux commands
Ansible Installation and Basic Configuration on Ubuntu
Step 1: Installing Ansible on Popular Linux Distributions
Ansible is available in the package repositories of many Linux distributions, but the official Ansible PPA (Personal Package Archive) or repository often has the latest stable version. Here’s how to install Ansible on some popular Linux distributions:
To install the latest version of Ansible on Ubuntu or Debian, use the official Ansible PPA:
Update the package list:
sudo apt update
Install the dependencies:
sudo apt install software-properties-common
Add the Ansible PPA:
sudo add-apt-repository --yes --update ppa:ansible/ansible
Install Ansible:
sudo apt install ansible -y
Verify the installation:
ansible --version
Step 2: Set Up SSH Access
We need to setup a SSH access between the remote servers to ensure you can SSH into your managed nodes without a password prompt. This can be achieved by setting up SSH keys.
Generate SSH Keys (if not already done)
On your system, generate an SSH key pair (if you don’t have one already):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This will create a pair of files:
- ~/.ssh/id_rsa (private key)
- ~/.ssh/id_rsa.pub (public key)
Copy the Public Key to Remote Servers
You need to copy the public key to the remote servers to enable passwordless SSH access. Use the ssh-copy-id command:
ssh-copy-id username@remote_server_ip
Replace username with the remote user’s name and remote_server_ip
with the IP address of your remote server. Repeat this step for each remote server.
Test SSH access to ensure you can connect without a password:
ssh username@remote_server_ip
If you can connect without being prompted for a password, SSH access is correctly set up. Now exit the current connection and back to main server.
Step 3: Configuring Ansible Inventory Files and Host Groups
Ansible uses an inventory file to keep track of all the machines it manages. The default inventory file is located at /etc/ansible/hosts
. However, you can create your own custom inventory file.
3.1 Creating a Basic Inventory File
You can define hosts and groups in the inventory file using a simple text format:
Create a new inventory file:
nano ~/my_ansible_inventory
Add hosts to the inventory. Here's an example:
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20
192.168.1.21
[all:vars]
ansible_user=your_ssh_user
ansible_ssh_private_key_file=~/.ssh/id_rsa
- Groups: In the example above, webservers and dbservers are groups. Hosts can belong to multiple groups.
- Variables: The [all:vars] section defines variables for all hosts. You can also set group-specific variables.
Step 4: Basic Ansible Commands
Now that Ansible is installed and configured, let's explore some basic commands to get started.
4.1 Ping All Hosts
Use the ping module to check connectivity to all the hosts in your inventory:
ansible all -m ping -i ~/my_ansible_inventory
- -i ~/my_ansible_inventory: Specifies the inventory file.
- all: Targets all hosts.
- -m ping: Uses the ping module to check connectivity.
4.2 Running Shell Commands on Remote Hosts
To execute a shell command on all hosts, use the shell module:
ansible -i ~/my_ansible_inventory all -m shell -a "uptime"
- -m shell: Specifies the shell module.
- -a "uptime": Executes the uptime command.
4.3 Installing Packages on Remote Hosts
To install a package (e.g., nginx) on the webservers group:
ansible -i ~/my_ansible_inventory webservers -m apt -a "name=nginx state=present" --become
- -m apt: Uses the apt module (for Debian-based systems).
- --become: Executes the command with sudo privileges.
4.4 Copying Files to Remote Hosts
To copy a local file to a remote host, use the copy module:
ansible -i ~/my_ansible_inventory webservers -m copy -a "src=~/localfile.txt dest=/tmp/remotefile.txt"
- src: Source file path on your local machine.
- dest: Destination path on the remote machine.
4.5 Gathering Information about Remote Hosts
To gather system information, use the setup module:
ansible -i ~/my_ansible_inventory all -m setup
This command provides detailed information about each host, such as OS version, hardware specs, and network interfaces.
Step 5: Creating and Running a Simple Ansible Playbook
Ansible Playbooks are YAML files that define a series of tasks to be executed on managed hosts.
5.1 Example Playbook
Here’s a basic playbook example to install nginx on all web servers:
Create a playbook file:
nano install_nginx.yml
Add the following content:
---
- name: Configure web servers
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx service
service:
name: nginx
state: started
- name: Configure database servers
hosts: dbservers
become: yes
tasks:
- name: Install MySQL server
apt:
name: mysql-server
state: present
- name: Start MySQL service
service:
name: mysql
state: started
Save and exit
In this playbook:
- name: Describes the purpose of the playbook or task.
- hosts: Specifies the group of hosts where the tasks will be executed.
- become: Indicates that the tasks should be executed with elevated privileges (sudo).
- tasks: Contains a list of actions to be performed.
Run the playbook:
ansible-playbook -i ~/my_ansible_inventory install_nginx.yml
Explanation
- hosts: Specifies the target group.
- become: yes: Runs tasks with elevated privileges.
- tasks: List of tasks to be executed.
Conclusion
We've seen getting started with Ansible installation and basic configuration on Ubuntu 24.04 server. With this guide, you now have a foundational understanding of how to install Ansible, set up inventory files, and run basic commands. You can expand on this knowledge by exploring more Ansible modules and writing advanced playbooks for complex automation tasks.