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 (3)
Thank you for sharing such an excellent post. I really enjoyed reading it.
I’m a Python Full-Stack Engineer with over 10 years of experience designing and building scalable software solutions for clients across a variety of industries. Along the way, I’ve learned that successful projects depend not only on strong technical execution but also on creating real business value.
With my recent contract completed, I’m exploring new opportunities to collaborate with professionals who value innovation, practical problem-solving, and long-term partnerships. I enjoy discussing ideas that combine technical excellence with sound business strategy, creating outcomes that benefit everyone involved.
I believe every connection has the potential to become something meaningful. If you're interested in exchanging ideas, exploring opportunities, or simply connecting with someone who enjoys building impactful technology, I'd be happy to hear from you.
Wishing you success in your future endeavors, and I look forward to connecting.
Hey,
Thank you. I think, with the shrinking economies all over the globe, developers and engineers are forced to be creative and really create real business value. I think this is where collaboration makes the industry stronger. Sharing ideas and experience. Glad to connect. I've tried python when I first started but my core is React. However, I'm always willing to learn. Blessings to you on your next project also.
Thanks for reaching out.
If you allow, I wanna discuss further about collaboration and opportunity.
How about you?