Question

I used ansible's ec2_vol module to create an ebs volume. I saw the source code and found that it internally calls create_volume() method of boto with user specified parameters. I want to register the return value of ec2_vol module and get the volume_ids of newly created volumes.

As of now my playbook looks like

- name: Attach a volume to previously created instances
  local_action: ec2_vol instance={{item.id}} volume_size=5 aws_access_key={{aa_key}} aws_secret_key={{as_key}} region={{region}}
  with_items: ec2.instances
  register: ec2_volumes
  ignore_errors: yes

- name: Stop the instances
  local_action: command aws ec2 stop-instances --profile=xervmon --instance-id={{item.id}}
  with_items: ec2.instances
  ignore_errors: yes

- name: Detach volume from instances
  local_action: command aws ec2 detach-volume --profile=xervmon --volume-id=????                                                
  ignore_errors: yes

I would like to know how to get the volume ids of newly created volumes. I saw that run_instances() method's return object has an attribute instances whcih contain a list of instances. But I couldn't find any proper documentation of create_volume() method's return value.

Any help is appreciated.

Thanks,

Was it helpful?

Solution

According to the ec2_vol module source code, the module returns:

  • volume_id
  • device

In your case, you are creating multiple volumes via with_items, so the ec2_volumes variable that you register will be a dictionary with a key named results that holds the list of results from each of the individual ec2_vol invocations.

Here's an example that prints out the volume ids (warning: I haven't tested this).

- name: Attach a volume to previously created instances
  local_action: ec2_vol instance={{item.id}} volume_size=5 aws_access_key={{aa_key}} aws_secret_key={{as_key}} region={{region}}
  with_items: ec2.instances
  register: ec2_volumes

- name: Print out the volume ids
  debug: msg={{ item.volume_id }}
  with_items: ec2_volumes.results
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top