Introduction
What is Ansible?
Ansible is an automation language that can describe any IT environment, whether homelab or large-scale infrastructure. It is easy to learn and reads like clear documentation.
If you manage multiple servers and find yourself doing the same configuration over and over — setting up SSH keys, disabling root users, configuring firewalls — Ansible can automate the entire process and dramatically increase your productivity.
It only requires Ansible on the Control Node and Python 3 on the Managed Node.
What is the Control Node?
The system that Ansible is installed on — it controls the remote machines.
What is the Managed Node?
The remote system or host that Ansible controls. Ansible is agentless, meaning you don't need to install Ansible on managed nodes — just Python 3.
Installing Ansible on Ubuntu (Control Node)
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
Note: Ensure Python 3 is installed on your remote server. Ubuntu 24.04 LTS ships with Python 3 by default.
Create the Inventory Folder and Hosts File
The hosts file maps the remote machines you want to control.
Folder structure:
Ansible/
├── inventory/
│ └── hosts
└── playbooks/
└── apt-update.yml
inventory/hosts
[servers]
vpsServer ansible_host=10.10.100.45
work-ToRule
10.10.45.62
You can give hosts an alias by pairing a name with an IP address. In the example above, vpsServer is an alias for 10.10.100.45.
Your First Playbook — Update Ubuntu and Set Timezone
Create playbooks/apt-update.yml:
- hosts: '*'
become: true
serial: 1
tasks:
- name: Set system timezone to Trinidad and Tobago time
community.general.timezone:
name: America/Port_of_Spain
- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Upgrade all packages to the latest version
apt:
upgrade: dist
- name: Check if reboot is required
stat:
path: /var/run/reboot-required
register: reboot_required_file
- name: Reboot the server
reboot:
msg: 'Reboot initiated by Ansible due to package upgrades'
connect_timeout: 5
reboot_timeout: 300
pre_reboot_delay: 0
post_reboot_delay: 30
when: reboot_required_file.stat.exists
Breaking Down the Playbook
| Key | Description |
|---|---|
hosts: '*' |
Target all hosts in inventory. Use an alias, DNS name, or IP to target a single host. |
become: true |
Grants Ansible sudo privileges. |
serial: 1 |
Processes servers one at a time instead of all at once. |
tasks |
A list of individual actions to run on the target hosts. |
Each task has four parts:
-
name— a plain-text description of what the task does -
Collection (
community.general) — the Ansible content bundle the module belongs to -
Module (
timezone,apt,reboot) — the tool that executes the action -
Parameters (
name: America/Port_of_Spain) — the specific options passed to the module
📚 Browse all available modules and collections at docs.ansible.com
Running the Playbook
Step 1 — Test connectivity with a ping
ANSIBLE_HOST_KEY_CHECKING=FALSE ansible -i ./inventory/hosts vpsServer -m ping --user root --ask-pass
| Flag | Description |
|---|---|
ANSIBLE_HOST_KEY_CHECKING=FALSE |
Skips SSH host key verification — useful for fresh servers |
-i ./inventory/hosts |
Points to your inventory file |
vpsServer |
The target host alias |
-m ping |
Runs the ping module to check connectivity and Python availability |
--ask-pass |
Prompts for SSH password |
Once you get a green pong response, you're ready to run the playbook.
Step 2 — Run the playbook
ansible-playbook ./playbooks/apt-update.yml --user root -e "ansible_port=22" --ask-pass --ask-become-pass -i ./inventory/hosts
| Flag | Description |
|---|---|
ansible-playbook |
Runs a full automation script instead of a single ad-hoc task |
./playbooks/apt-update.yml |
Path to your playbook file |
--user root |
SSH connection username |
-e "ansible_port=22" |
Injects extra variable to force port 22 |
--ask-pass |
Prompts for SSH login password |
--ask-become-pass |
Prompts for sudo password (redundant when logging in as root) |
-i ./inventory/hosts |
Points to your inventory file |
Execution Flow
- Ansible reads
./inventory/hoststo find the target server's IP - Prompts for SSH password
- Prompts for sudo password
- Connects to port 22 as root
- Opens
apt-update.ymland executes each task in order
Conclusion
A big shout-out to Aldo @aldo_cve for recommending Ansible in a previous post — it's been a great addition to my server management workflow.
I hope you found this walkthrough useful. Stay tuned for more posts where I share playbooks I find useful in my day-to-day infrastructure work.
Looking for developer templates built on TanStack, Directus, and Tailwind CSS? Check out my store 👇
🛒 northernrangedigital.lemonsqueezy.com
Top comments (0)