使用ansible从新创建的ebs卷中获取卷ID

Pat*_*ttu 1 cloud amazon-ec2 orchestration amazon-web-services ansible

我使用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
Run Code Online (Sandbox Code Playgroud)

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

任何帮助表示赞赏.

谢谢,

Lor*_*ein 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
Run Code Online (Sandbox Code Playgroud)