1

我使用 ansible 的 ec2_vol 模块来创建一个 ebs 卷。看了源码,发现它内部调用了boto的create_volume()方法,带有用户指定的参数。我想注册ec2_vol模块的返回值并获取新创建的卷的volume_ids。

截至目前,我的剧本看起来像

- 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

我想知道如何获取新创建的卷的卷 ID。我看到 run_instances() 方法的返回对象有一个属性实例,其中包含一个实例列表。但我找不到任何关于 create_volume() 方法返回值的适当文档。

任何帮助表示赞赏。

谢谢,

4

1 回答 1

8

根据ec2_vol模块源码,模块返回:

  • volume_id
  • device

在您的情况下,您通过 with_items 创建多个卷,因此ec2_volumes您注册的变量将是一个字典,其中包含一个名为的键results,其中包含每个单独的 ec2_vol 调用的结果列表。

这是一个打印出卷 id 的示例(警告:我尚未对此进行测试)。

- 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
于 2013-09-18T12:43:22.373 回答