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

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

Описание и применение

Настройка безпарольного SSH между двумя EC2‑инстансами и запуск первого Ansible‑плейбука, который устанавливает и запускает 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