🐥

Ansibleの「Failed to connect to the host via ssh: Permission denied」の対処法

2024/07/25に公開

はじめに

AnsibleでPlaybook実行時に下記のようにFailed to connect to the host via ssh: Permission deniedが発生した場合の対処方法をよく忘れるので備忘録として残す。

fatal: [ansible-dev-target1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}
fatal: [ansible-dev-target2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}
fatal: [ansible-test-target1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}
fatal: [ansible-test-target2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).", "unreachable": true}

前提

  • 接続先の秘密鍵が既に用意されている状態(``/.ssh/`直下)
  • Ansibleのインベントリファイル用意済み

対処方法

SSHエージェントを起動させて秘密鍵を登録する。

  • SSHエージェント起動
コマンド
ssh-agent bash
  • SSHエージェントに秘密鍵を登録する。
コマンド
ssh-add ~/.ssh/<秘密鍵>
実行例
$ ssh-add ~/.ssh/rhel-key.pem
Identity added: /home/ec2-user/.ssh/rhel-key.pem (/home/ec2-user/.ssh/rhel-key.pem)
$

参考

https://docs.github.com/ja/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent?platform=linux#adding-your-ssh-key-to-the-ssh-agent

https://www.udemy.com/share/1087CO3@TmhhLtEJ5bOzzpq5JDmE8Tezapeomzw5NXFLDuiUKwkL74BBRGz0r8KoGaEz1c_0/

Discussion