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

Today I Learnt: Ansible — Passwordless SSH Between Two EC2s, and My First Playbook

说明与用途

在两台 EC2 实例之间设置无密码 SSH,并运行第一个 Ansible playbook 安装并启动 nginx。

问题

Enable passwordless SSH between an Ansible control node and a target EC2 instance so Ansible can run playbooks without manual password entry.

结果

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.

工作流

  • 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.

证据

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

来源

  • 记录日期: 2026-10-11T09:56:16.783Z