cannot access ec2.instances data

289 views
Skip to first unread message

Fred Badel

unread,
Apr 7, 2014, 8:45:00 AM4/7/14
to ansible...@googlegroups.com
Hello,

I am sure that I miss something obvious... but after having tried for a few hours, I just don't know where to look for a solution ...

I have the following playbook to create a new amazon instance and do a few things with it.
The instance is created correctly, but when I try to access to ec2_info.instances. private_ip variables, the playbook fail with an undefined variable error ...

Does any one have an idea?

Many thanks in advance.

Fred

** playbook:
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: True
  vars:
      keypair: mykey
      dnsdomain: aws.domain.com
      fqdn: "{{ hostname }}.{{ dnsdomain }}"

  tasks:
      - name: create a vm
        local_action:
            ec2_url: https://ec2.eu-west-1.amazonaws.com
            module: ec2
            image: ami-75342c01
            count: 1
            instance_type: m1.large
            group:
                - J2EE
                - default
            key_name: "{{ keypair }}"
            vpc_subnet_id: subnet
            user_data: "{{ hostname }}.{{ dnsdomain }}"
            instance_tags: '{ "QA": "group", "Name": "{{ hostname }}" }'
            volumes:
                - device_name: /dev/sda1
                  volume_size: 10
                - device_name: /dev/sdb
                  ephemeral: ephemeral0
                - device_name: /dev/sdc
                  volume_size: 25
            wait: true
            wait_timeout: 500
            state: present
        register: ec2_info

      - name: register DNS name
        command: dnsupdate.py -s localhost -k Kddnsupdate.key -x add {{ fqdn }} 300 A {{ ec2_info.instances.private_ip }}
        sudo: yes
        remote_user: user
        delegate_to: dns_server

      - name: add new instance to inventory
        add_host: name={{ hostname }} groups=ci hostname={{ fqdn }} ansible_ssh_host={{ ec2_info.instances.private_ip }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem

      - name: wait for instance to start
        wait_for: state=started host={{ ec2_info.instances.private_ip }} port=22

** error message:

TASK: [register DNS name] *****************************************************
fatal: [127.0.0.1] => One or more undefined variables: 'list' object has no attribute 'private_ip'

FATAL: all hosts have already failed -- aborting

** with a "debug=ec2_info.instances":

    "ec2_info.instances": [
        {
            "ami_launch_index": "0",
            "architecture": "x86_64",
            "dns_name": "",
            "hypervisor": "xen",
            "id": "i-d43324",
            "image_id": "ami-7",
            "instance_type": "m1.large",
            "kernel": "aki-54425520",
            "key_name": "ContinuousQA",
            "launch_time": "2014-04-07T12:30:29.000Z",
            "placement": "eu-west-1b",
            "private_dns_name": "ip-172-16-4-127.aws.domain.com",
            "private_ip": "172.16.4.127",
            "public_dns_name": "",
            "public_ip": null,
            "ramdisk": null,
            "region": "eu-west-1",
            "root_device_name": "/dev/sda1",
            "root_device_type": "ebs",
            "state": "running",
            "state_code": 16,
            "virtualization_type": "paravirtual"
        }
    ]
}

Will Thames

unread,
Apr 7, 2014, 5:42:07 PM4/7/14
to ansible...@googlegroups.com
Hi,

"fatal: [127.0.0.1] => One or more undefined variables: 'list' object has no attribute 'private_ip'"

That's because ec2_info.instances is a list, not a dict.

If you use ec2_info.instances[0] you'll get the first (and in your case only) instance returned by ec2 (which can create multiple instances)

So ec2_info.instances[0].private_ip will get you what you're after.

Will

Frédéric Badel

unread,
Apr 8, 2014, 4:44:55 AM4/8/14
to ansible...@googlegroups.com
Hi Will,

I knew it was something obvious! 
It is working much better now.

Thank you very much.

Fred


-- 
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d9a3bbf5-c30c-4960-879a-e411d662d5c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages