Trying to get aws instance id

314 views
Skip to first unread message

Christian Del Pino

unread,
Feb 9, 2016, 10:54:51 AM2/9/16
to Ansible Project
Hello,

I am having some difficulties trying to obtain the instance id for an ec2 instance that is being created. Here is what I have at the moment to create the ec2 instance:

### Create NAT Instance ###
- name: Launch NAT instance
  ec2:
    vpc_subnet_id: "{{ name_subnet.subnet.id }}"
    region: "{{ aws_region }}"
    keypair: "{{ keypair.key.name }}"
    group_id: "{{ natsecuritygroup.group_id }}"
    image: "{{ nat_aws_image }}"
    instance_type: "{{ nat_aws_instance_type }}"
    instance_tags:
      type: "nat"
      Name: "NAT"
    exact_count: 1
    count_tag:
      type: "nat"
      Name: "NAT"
    wait: yes
  register: nat
  tags:
    - vpcsetup

I am trying to get the instance id from the registered value nat. Here is what I get when I debug the nat variable:

ok: [localhost] => {
    "nat": {
        "changed": false, 
        "instance_ids": null, 
        "instances": [], 
        "tagged_instances": [
            {
                "ami_launch_index": "0", 
                "architecture": "x86_64", 
                "block_device_mapping": {
                    "/dev/xvda": {
                        "delete_on_termination": true, 
                        "status": "attached", 
                        "volume_id": "vol-xxxxxx"
                    }
                }, 
                "dns_name": "ec2-xxxxxxx.compute-1.amazonaws.com", 
                "ebs_optimized": false, 
                "groups": {
                    "sg-xxxxxx": "nat-sg"
                }, 
                "hypervisor": "xen", 
                "id": "i-xxxxxxxxxxxxxxxx", 
                "image_id": "ami-xxxxxxxxx", 
                "instance_type": "m4.large", 
                "kernel": null, 
                "key_name": "kp", 
                "launch_time": "2016-02-09T04:42:36.000Z", 
                "placement": "us-east-1a", 
                "private_dns_name": "ip-xxxxx.ec2.internal", 
                "private_ip": "xxxxx", 
                "public_dns_name": "ec2-xxxxx.compute-1.amazonaws.com", 
                "public_ip": "xxxxxx", 
                "ramdisk": null, 
                "region": "us-east-1", 
                "root_device_name": "/dev/xvda", 
                "root_device_type": "ebs", 
                "state": "running", 
                "state_code": 16, 
                "tags": {
                    "Name": "NAT", 
                    "type": "nat"
                }, 
                "tenancy": "default", 
                "virtualization_type": "hvm"
            }
        ]
    }
}

I see that the instance_ids is set to null, but there is a value for id. How do I get the id value? I tried several variations to get the value, but no luck. I keep getting an error saying either:

{"failed": true, "msg": "ERROR! 'list object' has no attribute 'id'"}

or

{"failed": true, "msg": "ERROR! 'dict object' has no attribute 'id'"}

Thanks in advance.

Arbab Nazar

unread,
Feb 9, 2016, 1:29:42 PM2/9/16
to Ansible Project
 - name: Wait for SSH to come up
   debug: 
      msg: "{{ item.id }}" 
   with_items: nat.instances
Reply all
Reply to author
Forward
0 new messages