Run ssh-add with the ability to raise an error

I am trying to use Ansible to create an infrastructure for ssh connections.

- name: Copy ssh key to each server
  copy: src=static_folder_key dest=/home/ec2-user/.ssh/ mode=0600

- name: Enable ssh Agent
  shell: eval $(ssh-agent -s)

- name: Adding ssh key for static forlder project
  shell: ssh-add /home/ec2-user/.ssh/static_folder_key
  sudo: True

I create a new ssh key and copy it to my servers. Then I start the agent and then add a new key to allow the connection. But when I execute ansible, I got this error.

TASK: [git | Adding ssh key for static forlder project] *********************** 
failed: [admin_vehicles] => {"changed": true, "cmd": "ssh-add /home/ec2-user/.ssh/static_folder_key", "delta": "0:00:00.004346", "end": "2015-08-12 15:05:00.878208", "rc": 2, "start": "2015-08-12 15:05:00.873862", "warnings": []}
stderr: Could not open a connection to your authentication agent.
failed: [leads_messages] => {"changed": true, "cmd": "ssh-add /home/ec2-user/.ssh/static_folder_key", "delta": "0:00:00.004508", "end": "2015-08-12 15:05:01.286031", "rc": 2, "start": "2015-08-12 15:05:01.281523", "warnings": []}
stderr: Could not open a connection to your authentication agent.

FATAL: all hosts have already failed -- aborting

If I perform these steps manually, everything will be fine.

ssh-add /home/ec2-user/.ssh/static_folder_key 
Identity added: /home/ec2-user/.ssh/static_folder_key (/home/ec2-user/.ssh/static_folder_key)

So any tips? Maybe I missed something in my task in the playbook?

+4
source share
1 answer

The environment for each task is independent, so you cannot leave the ssh-agentsettings made in one task to another.

SSH- . ~/.ssh/config, ssh-agent ssh-add static_folder_key ansible-playbook. .

Host *
ForwardAgent yes

, ssh-agent . ~/.ssh/config ​​ ssh static-folder-host.

Host static-folder-host
Hostname static-folder-host.static-folder-domain
User static-folder-user
IdentityFile ~/.ssh/static_folder_key
+4

All Articles