# Ansible cannot ssh to ec2 instance that was dynamically created

**URL:** <https://forum.ansible.com/t/ansible-cannot-ssh-to-ec2-instance-that-was-dynamically-created/33540>\
**Category:** Ansible Developer\
**Tags:** aws\
**Created:** [August 2, 2020, 11:30pm UTC](https://forum.ansible.com/t/ansible-cannot-ssh-to-ec2-instance-that-was-dynamically-created/33540 "2020-08-02T23:30:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Des\_Sindatry](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@Des\_Sindatry](https://forum.ansible.com/u/Des_Sindatry)\
**Post date:** [August 2, 2020, 11:30pm UTC](https://forum.ansible.com/t/ansible-cannot-ssh-to-ec2-instance-that-was-dynamically-created/33540/1 "2020-08-02T23:30:03Z")

</div>

I have a playbook for ansible to create ec2 instance dynamically this works fine.  
I add them to host group. I want ansible to run a script on the dynamically created ec2 instances.  
It fails at connection waiting. I am providing the same key that ansible used to create ec2 instance.

- name: Deploy EC2 Master node  
hosts: localhost  
gather\_facts: False  
vars\_files:
- ~/aws-common/automation/ansible/config/var\_input.yml  
tasks:
- name: Get AWS credentials  
sts\_assume\_role:  
role\_arn: “{{ role\_arn }}”  
role\_session\_name: “{{ role\_session\_name }}”  
register: assumed\_role
- name: Provision a set of instances  
ec2:  
key\_name: “{{ key\_name }}”  
group: “{{ group }}”  
instance\_type: “{{ instance\_type }}”  
region: “{{ region }}”  
image: “{{ image }}”  
wait: “{{ wait }}”  
wait\_timeout: “{{ wait\_timeout }}”  
count: “{{ count }}”  
instance\_profile\_name: “{{ instance\_profile\_name }}”  
instance\_tags:  
Name: “{{ Name }}”  
deployer: “{{ deployer }}”  
resourceowner: “{{ resourceowner }}”  
monitoring: “{{ monitoring }}”  
vpc\_subnet\_id: “{{ vpc\_subnet\_id }}”  
assign\_public\_ip: “{{ assign\_public\_ip }}”  
aws\_access\_key: “{{ assumed\_role.sts\_creds.access\_key }}”  
aws\_secret\_key: “{{ assumed\_role.sts\_creds.secret\_key }}”  
security\_token: “{{ assumed\_role.sts\_creds.session\_token }}”  
volumes:
- device\_name: /dev/sda1  
volume\_type: gp2  
encrypted: true  
volume\_name: HadoopMaster-rootvolume /  
volume\_size: 100  
delete\_on\_termination: true
- device\_name: /dev/sdf  
volume\_type: gp2  
encrypted: true  
volume\_name: HadoopMaster /sso/sfw  
volume\_size: 70  
delete\_on\_termination: true

register: ec2

- name: Add new instance to host group  
add\_host:  
hostname: “{{ item.private\_ip }}”  
groupname: launched  
loop: “{{ ec2.instances }}”

- name: Wait for SSH to come up  
delegate\_to: “{{ item.private\_dns\_name }}”  
wait\_for\_connection:  
delay: 60  
timeout: 320  
loop: “{{ ec2.instances }}”

- hosts: launched  
name: Mounting the attached EBS volumes  
user: ec2-user  
gather\_facts: false  
tasks:

- name: Run a script with arguments (free form)  
script: ~/aws-common/automation/ansible/files/formatandmount.sh

[desind@comdpmactl01 files]$ cat ~/ansible.cfg  
[defaults]  
inventory = hosts  
executable=/etc/ansible-wrapper  
retry\_files\_enabled = False  
allow\_world\_readable\_tmpfiles = True  
remote\_tmp = /tmp/.ansible-${USER}/tmp  
#log\_path: “/home/desind/myAnsible/test.log”  
command\_warnings = False

[ssh\_connection]

# Adding ForwardAgent=yes so ‘copy wallet to backup directory’ works.

# The ssh chain is vspoem04-\>vspoem01-\>ansible\_fqdn

# Without ForwardAgent, the vspoem01-\>ansible\_fqdn fails

ssh\_args = -o ControlMaster=no -o ForwardAgent=yes  
pipelining = True  
host\_key\_checking = False

Command: /sso/sfw/python2/bin/ansible-playbook test2.yaml --extra-vars=‘ansible\_python\_interpreter=/usr/bin/python’ -i ansible\_ssh\_private\_key\_file=~/aws-common/automation/ansible/files/kafka.ppk --user ec2-user

---

<div class="post-metadata">

**Author:** ![Des\_Sindatry](https://avatars.discourse-cdn.com/v4/letter/d/94ad74/32.png) [@Des\_Sindatry](https://forum.ansible.com/u/Des_Sindatry)\
**Post date:** [August 5, 2020, 1:51am UTC](https://forum.ansible.com/t/ansible-cannot-ssh-to-ec2-instance-that-was-dynamically-created/33540/2 "2020-08-05T01:51:50Z")

</div>

I was able to fix my issue. The issue was that i had a wrapper and ansible expected that on ec2 instance. I commented that in ansible.cfg and that resoled my issue
