Today I Learnt: Ansible — Passwordless SSH Between Two EC2s, and My First Playbook
Description and use
Setting up passwordless SSH between two EC2 instances and running the first Ansible playbook to install and start nginx.
Problem
Enable passwordless SSH between an Ansible control node and a target EC2 instance so Ansible can run playbooks without manual password entry.
Outcome
Passwordless SSH between the control and target EC2 instances was achieved, enabling Ansible to run playbooks automatically. The first playbook installed and started nginx on the target, demonstrating agentless, idempotent configuration management.
Workflow
- Spin up two EC2 instances (control and target).
- Generate an RSA key pair on the control node with
ssh-keygen -t rsa -b 4096and no passphrase. - Copy the public key to the target using
ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@<target-private-ip>or manually append to~/.ssh/authorized_keyson the target. - Test passwordless SSH with
ssh ubuntu@<target-private-ip>; expect no prompt. - Install Ansible on the control node via
sudo apt update && sudo apt install ansible -yor via pip. - Create an inventory file
inventory.inilisting the target under[targets]withansible_hostandansible_user. - Verify connectivity with
ansible all -i inventory.ini -m pingand expect a "pong" response. - Write a playbook
first_playbook.ymlthat updates apt cache, installs nginx, and starts the nginx service. - Run the playbook with
ansible-playbook -i inventory.ini first_playbook.ymland observe idempotent execution.
Evidence
First, on the control node, I generated a new key pair: ssh-keygen -t rsa -b 4096
Getting that public key onto the target machine is the part that actually makes the login passwordless. The easy way: ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@<target-private-ip>
Install Ansible on the control node. sudo apt update && sudo apt install ansible -y
Create an inventory file inventory.ini listing the target under [targets] with ansible_host and ansible_user.
Run the playbook with ansible-playbook -i inventory.ini first_playbook.yml
- Record date: 2026-10-11T09:56:16.783Z