Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

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 4096 and 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_keys on 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 -y or via pip.
  • Create an inventory file inventory.ini listing the target under [targets] with ansible_host and ansible_user.
  • Verify connectivity with ansible all -i inventory.ini -m ping and expect a "pong" response.
  • Write a playbook first_playbook.yml that updates apt cache, installs nginx, and starts the nginx service.
  • Run the playbook with ansible-playbook -i inventory.ini first_playbook.yml and observe idempotent execution.

Evidence

First, on the control node, I generated a new key pair: ssh-keygen -t rsa -b 4096

Source

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>

Source

Install Ansible on the control node. sudo apt update && sudo apt install ansible -y

Source

Create an inventory file inventory.ini listing the target under [targets] with ansible_host and ansible_user.

Source

Run the playbook with ansible-playbook -i inventory.ini first_playbook.yml

Source

  • Record date: 2026-10-11T09:56:16.783Z