Access by ssh to an ec2 instance after creating it

40 views
Skip to first unread message

SysAdmin EM

unread,
Apr 28, 2022, 8:11:23 AM4/28/22
to ansible...@googlegroups.com
Hi, I am creating an ec2 instance in AWS, but after creating it I need to log in via SSH and run some tasks on it.

The instance takes a few minutes to be ready, as I do not know how long it takes to be ready and nor the IP assigned by Amazon, my question as SSH login to a newly created instance without knowing its IP and its creation status?
Regards,

Rilindo Foster

unread,
Apr 28, 2022, 8:22:41 AM4/28/22
to ansible...@googlegroups.com
Hi there! That is more of an AWS question than an Ansible question. I might suggest posting the question on the AWS forums here:

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGUDtnnG7CC2Nf1myR23aapyx5zcmsuMQ2keKFLVRZomkg1U6g%40mail.gmail.com.

Walter Rowe

unread,
Apr 28, 2022, 1:37:01 PM4/28/22
to Ansible Project
The community.aws.ec2_instance module returns facts about the ec2 including interfaces and IPs. Register these facts and you can access it.

```
- community.aws.ec2_instance:
    aws_access_key: "{{ access_key }}"
    aws_secret_key: "{{ secret_key }}"
    security_token: "{{ session_token }}"
    name: "{{ ec2_name }}"
    key_name: "{{ ec2_key }}"
    vpc_subnet_id: "{{ subnet_id }}"
    security_group: "{{ secgroup_id }}"
    network:
      assign_public_ip: false
    image_id: "{{ ami_image_id }}"
    instance_type: "{{ ec2_size }}"
    volumes: "{{ disk_list }}"
    region: "{{ aws_region }}"
  register: newec2

- set_fact:
    ip_addr: newec2.instances[0].network_interfaces[0].private_ip_address
```

Walter Rowe

unread,
Apr 28, 2022, 1:39:37 PM4/28/22
to Ansible Project
Adding to my earlier reply .. we then use a wait_for task to wait for the EC2 to be ready for SSH.

```
    - name: Wait for connection SSH port 22 to become open
      wait_for:
        port: 22
        connect_timeout: 10
        host: "{{ ip_addr }}"
        search_regex: OpenSSH
        # wait for 30 secs before starting to poll
        delay: 30
        # wait no more than 10 mins and fail
        timeout: 600
        sleep: 5
      delegate_to: localhost
      become: no
```

Dick Visser

unread,
Apr 29, 2022, 7:51:28 AM4/29/22
to ansible...@googlegroups.com
On 2022-04-28 (Thu) 19:39, 'Walter Rowe' via Ansible Project wrote:
> Adding to my earlier reply .. we then use a wait_for task to wait for
> the EC2 to be ready for SSH.

We use the ec2_instance module as well, but we use the build-in 'wait'
parameter which works well:

https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_instance_module.html#parameter-wait



--
Dick Visser
GÉANT
OpenPGP_signature

Walter Rowe

unread,
Apr 29, 2022, 1:59:19 PM4/29/22
to Ansible Project
Our wait step is in a different playbook downstream in a workflow. The upstream step in the workflow can create an EC2 or an ESX VM. We did it this way to be universally applicable. We have a workflow step to create a vm, a workflow step to create a DNS record for that VM, then a workflow step to customize that VM based on parameters coming into the workflow - apache, nginx, tomcat, mysql, etc. The create-vm step is a wrapper playbook that sources other playbooks based on IaaS platform (location) the user selected. We support ESX and AWS, but with this method could easily add Azure and Google and other cloud service providers or hypervisor platforms.

CREATE-VM.YML

---
##
## NOTES:
##
## this playbook provides a common interface to launching VMs in any IaaS provider service
##
## we use a common location variable with three-letter prefix for provider:
##
## - aws - amazon
## - azr - azure
## - esx - vmware
## - gcp - google
## - ora - oracle
##
## using this prefix we source a provider-specific vars and tasks file.
## first we have to insure we have a valid provider prefix in location.
##
- name: validate provider / location
  hosts: localhost
  gather_facts: no
  vars_files:
    - "server-specs.yml"                                  # shared provisioning vars
  tasks:
    - name: this task will skip if provider is valid
      fail: msg="Invalid provider {{ location[0:3] }}"
      when: location[0:3] not in providers_keys

##
## now we can safely provision because we can source a valid vars and tasks file
##
- name: provision new {{ os_type }} machine
  hosts: localhost
  gather_facts: no
  vars:
    my_family: "{{ 'windows' if os_type[0:7] == 'windows' else 'linux' }}"
    my_provider: "{{ location[0:3] }}"

  vars_files:
    - "server-specs.yml"                                  # shared provisioning vars
    - "{{ my_provider }}-vars.yml"                        # provider-specific vars

  tasks:

    - name: building {{ vm_guest_name }} at {{ providers[my_provider] }}
      import_tasks: "{{ my_provider }}-create-vm.yml"     # provider-specific tasks

Reply all
Reply to author
Forward
0 new messages