Grant Sayer
Grant Sayer

Reputation: 2320

Ansible AWS EC2 Detecting Server is Running Fails

Background:

Just trying to learn how to use Ansible and have been experimenting with the AWS Ec2 module to build and deploy a Ubuntu instance on AWS-EC2. So have built a simple Playbook to create and startup an instance and executed via ansible-playbook -vvvv ic.yml

The playbook is:

---
- name: Create a ubuntu instance on AWS
  hosts: localhost
  connection: local
  gather_facts: False
  vars:
    # AWS keys for access to the API
    ec2_access_key: 'secret-key'
    ec2_secret_key: 'secret-key'
    region: ap-southeast-2
  tasks:
    - name: Create a Key-Pair necessary for connection to the remote EC2 host
      ec2_key:
        name=ic-key region="{{region}}"
      register: keypair

    - name: Write the Key-Pair to a file for re-use
      copy:
        dest: files/ic-key.pem
        content: "{{ keypair.key.private_key }}"
        mode: 0600
      when: keypair.changed

    - name: start the instance
      ec2:
        ec2_access_key: "{{ec2_access_key}}"
        ec2_secret_key: "{{ec2_secret_key}}"
        region: ap-southeast-2
        instance_type: t2.micro
        image: ami-69631053
        key_name: ic-key   # key we just created
        instance_tags: {Name: icomplain-prod, type: web, env: production}  #key-values pairs for naming etc
        wait: yes
      register: ec2


    - name: Wait for instance to start up and be running
      wait_for: host = {{item.public_dns_name}} port 22 delay=60 timeout=320 state=started
      with_items: ec2.instances

Problem:

The issue is that when attempting to wait for the instance to fire up, using the wait_for test, as described in Examples for EC-2 module it fails with the following error message:

msg: this module requires key=value arguments (['host', '=', 'ec2-52-64-134-61.ap-southeast-2.compute.amazonaws.com', 'port', '22', 'delay=60', 'timeout=320', 'state=started'])

FATAL: all hosts have already failed -- aborting

Output:

Although the error message appears on the command line when I check in the AWS-Console the Key-Pair and EC2 instance are created and running.

Query:

Wondering

  1. There is some other parameter which I need ?
  2. What is the 'key=value' msg which is the error output being caused by?
  3. Any recommendations on other ways to debug the script to determine the cause of the failure ?
  4. Does it require registration of the host somewhere in the Ansible world ?

Additional NOTES:

Testing the playbook I've observed that the key-pair gets created, the server startup is initiated at AWS as seen from the AWS web console. What appears to be the issue is that the time period of the server to spin up is too long and the script timeouts or fails. Frustratingly, is that the error message is not all that helpful and also wondering if there is any other methods of debugging an ansible script ?

Upvotes: 1

Views: 1502

Answers (2)

300D7309EF17
300D7309EF17

Reputation: 24613

this isn't a problem of "detecting the server is running". As the error message says, it's a problem with syntax.

# bad
wait_for: host = {{item.public_dns_name}} port 22 delay=60 timeout=320 state=started
# good
wait_for: host={{item.public_dns_name}} port=22 delay=60 timeout=320 state=started

Additionally, you'll want to run this from the central machine, not the remote (new) server.

local_action: wait_for host={{item.public_dns_name}} port=22 delay=60 timeout=320 state=started

Upvotes: 1

keba
keba

Reputation: 2127

Focusing on the wait_for test as you indicate that the rest is working.

Based on the jobs I have running I would think the issue is with the host name, not with the rest of the code. I use an Ansible server in a protected VPC that has network access to the VPC where the servers start up in, and my wait_for code looks like this (variable name updated to match yours):

- name: wait for instances to listen on port 22
  wait_for:
    delay: 10
    state: started
    host: "{{ item.private_ip }}"
    port: 22
    timeout: 300
  with_items: ec2.instances

Trying to use DNS instead of an IP address has always proven to be unreliable for me - if I'm registering DNS as part of a job, it can sometimes take a minute to be resolvable (sometimes instant, sometimes not). Using the IP addresses works every time of course - as long as the networking is set up correctly.

If your Ansible server is in a different region or has to use the external IP to access the new servers, you will of course need to have the relevant security groups and add the new server(s) to those before you can use wait_for.

Upvotes: 0

Related Questions