Configure your first Linux servers.
Use Ansible to connect to two existing Ubuntu or Debian servers, install Nginx, and ensure the service is running. Learn inventory, SSH, YAML playbooks, modules and idempotency through one guided lab.
How the lab works
Ansible runs on your control node and connects to managed servers over SSH. It reads the inventory to identify hosts and the playbook to determine what configuration to apply.
Your computer / Linux environment
Stores Ansible, the inventory, configuration and playbook. It initiates connections to the managed hosts.
Two existing Ubuntu/Debian servers
Receive tasks over SSH. Ansible installs Nginx and makes sure its service is enabled and started.
Prepare your lab environment
This project configures existing machines. It does not create cloud infrastructure or virtual machines.
Control node
Linux or macOS environment with Ansible installed, terminal access and an SSH client. Windows users can use WSL2.
Managed nodes
Two running Ubuntu/Debian servers, reachable over SSH, with Python 3 available and a user authorised to use sudo.
Install Ansible on the control node
On an Ubuntu control node, install Ansible using the distribution package manager:
sudo apt update sudo apt install ansible -y ansible --version
For other operating systems, follow the official Ansible installation guide.
Build the three project files
Create one folder and add the configuration, inventory and playbook. Select each tab to inspect its contents and copy the code.
ansible-beginner/ ├── ansible.cfg ├── inventory.ini └── site.yml
ansible.cfg
Sets the default inventory file and keeps SSH host-key verification enabled.
inventory.ini
Defines the webservers group, server addresses, SSH user and private-key path.
site.yml
Describes the desired server state: Nginx installed, enabled and running.
Why YAML?
YAML is a human-readable data format used here to express Ansible plays, tasks and module parameters.
Run the automation step by step
Run these commands from inside the ansible-beginner directory on your control node.
Step 1 — Create the project folder
mkdir ansible-beginner cd ansible-beginner
Create the three files shown in the Project Files section and paste in their contents.
Step 2 — Generate an SSH key
ssh-keygen -t ed25519 -f ~/.ssh/ansible_lab
Use a passphrase if appropriate for your environment. The public key ends in .pub; keep the private key protected.
Step 3 — Copy the public key to each host
ssh-copy-id -i ~/.ssh/ansible_lab.pub automation@SERVER_1_IP ssh-copy-id -i ~/.ssh/ansible_lab.pub automation@SERVER_2_IP
Replace the placeholders with the actual addresses. Confirm each host's SSH fingerprint before accepting it.
Step 4 — Test SSH and Ansible connectivity
ssh -i ~/.ssh/ansible_lab automation@SERVER_1_IP ansible webservers -m ansible.builtin.ping
Repeat the SSH test for the second host. Ansible's ping module checks remote Ansible execution and should return pong for each host.
Step 5 — Check the playbook syntax
ansible-playbook site.yml --syntax-check
Fix YAML indentation or syntax errors before running tasks.
Step 6 — Execute the playbook
ansible-playbook site.yml
Ansible connects to both managed nodes, installs Nginx and ensures the service is enabled and started. The SSH user must have working sudo access.
Confirm the desired state
Check Ansible connectivity again
ansible webservers -m ansible.builtin.ping
Check that Nginx is active
ansible webservers -b -m ansible.builtin.command -a "systemctl is-active nginx"
Both hosts should report active. You can also open each server's IP address in a browser if inbound HTTP traffic on port 80 is allowed.
ansible-playbook site.yml a second time. If the servers already match the desired state, Ansible should report no further changes for those tasks.Troubleshooting guide
UNREACHABLE
Check IP address, network route, SSH service, firewall rules and key authentication.
Permission denied
Check the SSH username, public-key installation and file permissions on the private key.
sudo failure
Confirm the remote account is authorised to use sudo and that the privilege escalation settings match the host.
Module / Python error
Confirm Python 3 is available on each managed node and that the target OS is supported.
Test what you've learned
Answer these questions to check your understanding of the beginner lab.
become: true enable?