In this tutorial, we'll discuss about installing and configuring Ansible on Ubuntu 22.04.
Ansible is an open-source automation tool that simplifies IT orchestration, configuration management, and application deployment. This guide will walk you through installing Ansible on Ubuntu and configuring it for automating IT tasks. We'll update the system, install Nginx and allow ports in firewall.
Installing and Configuring Ansible on Ubuntu
Prerequisites
- Ubuntu system (20.04, 22.04, or 24.04) dedicated server or KVM VPS
- Sudo privileges
Step 1: Update the System
First, ensure your system is up to date by running the following commands:
sudo apt update
sudo apt upgrade -y
Step 2: Install Ansible
Ansible is available in the default Ubuntu repositories, but it's often outdated. The best practice is to install it from the Ansible PPA to get the latest version.
Add Ansible PPA
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
Install Ansible
sudo apt install ansible -y
Verify Installation
To confirm that Ansible has been installed correctly, check its version:
ansible --version
You should see output similar to this, indicating the version of Ansible installed:
ansible [core 2.16.6]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
jinja version = 3.0.3
libyaml = True
Step 3: Basic Configuration
Ansible's default configuration file is located at /etc/ansible/ansible.cfg
. You can edit this file to set global options.
However, for simplicity and to avoid permission issues, it's often easier to create a local configuration directory in your user home.
Before you proceed with this step, please refer official documentation here. For this demostration purpose, we have used simple way. Where complex project and multiple users involved, there is other way for configuration file.
Create a Local Configuration Directory
mkdir -p ~/ansible/config
Disable default configuration file and enable our custom configuration file.
ansible-config init -t all --disabled > ansible.cfg
Create and Edit the Configuration File
nano ~/ansible/config/ansible.cfg
Add the following configuration settings after the [defaults]:
inventory = ~/ansible/config/hosts
remote_user = your-username
host_key_checking = False
Replace your-username
with your actual Ubuntu username.
Step 4: Create an Inventory File
The inventory file defines the hosts that Ansible will manage.
Create the Inventory File
nano ~/ansible/config/hosts
Add the IP addresses or hostnames of your managed nodes. For example:
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20
Step 5: Test Connectivity
Ansible uses SSH to communicate with managed nodes. Ensure you can SSH into these nodes without being prompted for a password. Setting up SSH keys is the recommended approach.
Generate SSH Key Pair (if not already done)
ssh-keygen -t rsa -b 2048
Copy SSH Key to Managed Nodes
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
Replace username with the username on the managed nodes.
Test Ansible Connectivity
ansible all -m ping
You should see output indicating success:
192.168.1.10 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
192.168.1.11 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
192.168.1.20 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
Step 6: Create a Nginx Installation Playbook
Ansible playbooks are YAML files that define tasks to be executed on managed nodes. In this playbook, we are updating the system, installing Nginx
and allowing HTTP
and HTTPS
ports in the firewall
.
Create a Playbook File
nano ~/ansible/config/playbook.yml
Add the following content to the playbook:
---
- name: Update and upgrade apt packages
hosts: all
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Installing Nginx
apt:
name: nginx
state: latest
- name: Starting Nginx Service
service:
name: nginx
state: started
enabled: yes # Enabling Nginx service if not available
- name: Allow all access to tcp port 80
community.general.ufw:
rule: allow
port: '80'
proto: tcp
- name: Allow all access to tcp port 443
community.general.ufw:
rule: allow
port: '443'
proto: tcp
Step 7: Run the Playbook
Execute the playbook to update and upgrade packages on all managed nodes.
ansible-playbook ~/ansible/config/playbook.yml
You should see output indicating success:
PLAY [Update and upgrade apt packages] *****************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [192.168.1.10]
ok: [192.168.1.11]
ok: [192.168.1.20]
TASK [Update apt cache] ********************************************************************************************
changed: [192.168.1.10]
changed: [192.168.1.11]
changed: [192.168.1.20]
TASK [Installing Nginx] ********************************************************************************************
changed: [192.168.1.10]
changed: [192.168.1.11]
changed: [192.168.1.20]
TASK [Starting Nginx Service] **************************************************************************************
ok: [192.168.1.10]
ok: [192.168.1.11]
ok: [192.168.1.10]
TASK [Allow all access to tcp port 80] *****************************************************************************
changed: [192.168.1.10]
changed: [192.168.1.11]
changed: [192.168.1.20]
TASK [Allow all access to tcp port 443] ****************************************************************************
changed: [192.168.1.10]
changed: [192.168.1.11]
changed: [192.168.1.20]
PLAY RECAP *********************************************************************************************************
192.168.1.10 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.11 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.20 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Conclusion
We have seen how install and configure Ansible on Ubuntu 22.04. You tested connectivity to managed nodes and created a simple playbook to update and upgrade packages. From here, you can expand your playbooks to automate more complex IT tasks. Ansible's extensive documentation and community resources are great places to explore further capabilities.