Running ansible playbook in AWS EC2 instance using Jenkins Job from a local PC using ssh-agent

277 views
Skip to first unread message

Raj Boruah

unread,
Aug 11, 2016, 9:01:37 AM8/11/16
to devops
Hi,    

I am trying to run ansible playbook from jenkins installed in my local PC.    Ansible playbook is available in AWS EC2 instance. I am using ssh-agent plugin in     Jenkins to forward the ssh key but ansible is not able to ssh to EC2 instances.     Appreciate any help on it.    

Here is sample output the Jenkin's job run:    
Started by user RajB   
Building on master in workspace /data/jenkins/workspace   
**[ssh-agent] Using credentials ubuntu**   
[ssh-agent] Looking for ssh-agent implementation...   
[ssh-agent]   Java/JNR ssh-agent   
**[ssh-agent] Started.**   
SSH: Connecting from host [HP-Z420-Workstation]   
SSH: Connecting with configuration [aws-devops] ...   
SSH: Creating session: username [ubuntu], hostname [ec2-XXX.ap-southeast-   1.compute.amazonaws.com], port [22]   
SSH: Connecting session ...   
SSH: Connected   
SSH: Opening exec channel ...   
SSH: EXEC: channel open   
SSH: EXEC: STDOUT/STDERR from command [cd /home/ubuntu/.playbook && ansible-playbook ec2_facts.yml] ...   
SSH: EXEC: connected   

PLAY [verify ec2 facts] ********************************************************    

TASK [setup] *******************************************************************   
fatal: [x.x.1.116]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}   
fatal: [x.x.6.96]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}   
fatal: [x.x.12.51]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}   
to retry, use: --limit @ec2_facts.retry   

PLAY RECAP *********************************************************************   
x.x.1.116               : ok=0    changed=0    unreachable=1    failed=0   
x.x..10.157              : ok=0    changed=0    unreachable=1    failed=0   
x.x..12.51               : ok=0    changed=0    unreachable=1    failed=0   

SSH: EXEC: completed after 55,630 ms   
SSH: Disconnecting configuration [aws-devops] ...   
ERROR: Exception when publishing, exception message [Exec exit status not zero. Status [3]]   
Build step 'Send files or execute commands over SSH' changed build result to UNSTABLE   
[ssh-agent] Stopped.   
Notifying upstream projects of job completion   
Finished: UNSTABLE   

Brendan Murtagh

unread,
Aug 11, 2016, 10:48:47 PM8/11/16
to devops
Hi,

Here are a few things I'm thinking of:

1. Have you validated that the key configured in the SSH Agent plugin works for that instance manually?

2. Verify the EC2, Subnet, and VPC are allowing traffic inbound from the Jenkins server to the EC2 instance.

3. Can you SSH from your local machine with the same key in #1 to the EC2 instance?

Reply all
Reply to author
Forward
0 new messages