Install Ansible on Ubuntu Server 24.04 LTS
This guide provides a step-by-step installation of Ansible Automation Tool on Ubuntu Server 24.04 LTS systems.
1. Prerequisites
- Ubuntu Server 24.04 LTS
- Sudo or root privileges
- Internet connectivity
- SSH enabled
- Minimum 2 GB RAM
2. Set Hostname
sudo hostnamectl set-hostname ansible.example.com
3. Update System Packages
sudo apt update && sudo apt upgrade -y
sudo reboot
4. Install Required Packages
sudo apt install -y \
software-properties-common \
wget \
curl \
git \
vim \
python3 \
python3-pip \
openssh-client
5. Add Ansible Repository
sudo add-apt-repository --yes --update ppa:ansible/ansible
6. Install Ansible
sudo apt install -y ansible
7. Verify Ansible Installation
ansible --version
8. Generate SSH Key
ssh-keygen -t rsa -b 4096
9. Copy SSH Key to Remote Host
ssh-copy-id user@remote-server
10. Create Ansible Inventory File
sudo mkdir -p /etc/ansible
sudo vi /etc/ansible/hosts
11. Example Inventory Configuration
[webservers]
192.168.1.101
192.168.1.102
[dbservers]
192.168.1.201
[all:vars]
ansible_user=root
12. Test Ansible Connectivity
ansible all -m ping
13. Create Sample Playbook
vi install_nginx.yml
14. Example Ansible Playbook
---
- name: Install NGINX Web Server
hosts: webservers
become: yes
tasks:
- name: Install NGINX
apt:
name: nginx
state: present
update_cache: yes
- name: Start NGINX Service
service:
name: nginx
state: started
enabled: yes
15. Run Playbook
ansible-playbook install_nginx.yml
16. List Managed Hosts
ansible all --list-hosts
17. Configure UFW Firewall (Optional)
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
18. Install Additional Ansible Tools
sudo apt install -y \
python3-jmespath \
sshpass
19. Install Ansible Collections
ansible-galaxy collection install community.general
20. Optional Post Installation Tasks
- Configure Ansible Roles
- Configure Vault Encryption
- Setup AWX Dashboard
- Configure CI/CD Pipelines
- Create Reusable Playbooks