Get the id of the root EBS volume of an EC2 instance

880 views
Skip to first unread message

Christian Kaps

unread,
Apr 11, 2014, 5:59:09 AM4/11/14
to ansible...@googlegroups.com
Hi,

is there any possibility to get the id of the root EBS volume of a newly created EC2 instance? I cannot find any documentation about it?

Thanks,
Christian

Will Thames

unread,
Apr 12, 2014, 8:14:40 PM4/12/14
to ansible...@googlegroups.com
Hi Christian,
How would you like to obtain the information - at instance creation time or at an arbitrary point in time later? (i.e. do you want to be able to get the instance ID as a value returned by the ec2 module, or from ec2_facts?)

Will

Will Thames

unread,
Apr 12, 2014, 9:00:08 PM4/12/14
to ansible...@googlegroups.com
Had a quick look at this in more detail.

Neither the ec2 nor the ec2_facts module expose this information. It is available but in particular adding it to ec2_facts module would require fundamental changes (as currently the ec2_facts module gets all of its information from logging onto the box and reading the metadata, whereas this information would only be available through talking to the API - see http://stackoverflow.com/questions/5251057/using-boto-to-find-to-which-device-and-ebs-volume-is-mounted). I'm not adverse to such a change but making it optional and non-breaking for other users of the module is probably the challenge. 

Returning the value from the ec2 module is probably quite straightforward, on the other hand.

Will

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/0vqsUHrn2AU/unsubscribe.
To unsubscribe from this group and all its topics, 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/ef9babed-2fb5-420d-b927-699bdc917673%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christian Kaps

unread,
Apr 14, 2014, 6:25:49 AM4/14/14
to ansible...@googlegroups.com
Hi Will,

Thanks for your answer.

I would like to create a tag for the root volume. So for my use case the ec2 module should return the IDs and the device names of the attached volumes after instance creation.

Should I create a future request and point to this thread?

Thanks,
Christian

Will Thames

unread,
Apr 14, 2014, 8:52:50 AM4/14/14
to ansible...@googlegroups.com
I was just looking into how I might implement this and there's an interesting side effect of ec2_vol if a volume is already attached

Assuming you run ec2 and register the result as ec2, the following works:

  - name: get root volume
    action:
      module: ec2_vol
      instance: "{{ec2.instances[0].id}}"
      device_name: '/dev/sda1'
      region: ap-southeast-2
      volume_size: 1
    register: ec2_vol

(note that volume_size doesn't matter, it's ignored but the module fails without it - with an error message that can only be described as unhelpful!)

You get as a result:

ok: [127.0.0.1] => {"changed": false, "device": "/dev/sda1", "msg": "Volume mapping for /dev/sda1 already exists on instance i-834ab5bd", "volume_id": "vol-92cee996"}

And so you can use ec2_vol.volume_id as a resource id to pass to ec2_tag.

Will

Christian Kaps

unread,
Apr 14, 2014, 11:39:16 AM4/14/14
to ansible...@googlegroups.com
Hi Will,

works like a charm.

Many thanks.
Christian
Reply all
Reply to author
Forward
0 new messages