使用 ansible 从新创建的 ebs 卷中获取卷 ID [英] Get volume id from newly created ebs volume using ansible

查看:33
本文介绍了使用 ansible 从新创建的 ebs 卷中获取卷 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在我的剧本看起来像

- 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() 方法返回值的任何适当文档.

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.

感谢任何帮助.

谢谢,

推荐答案

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

  • volume_id
  • 设备

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

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.

这是一个打印出卷 ID 的示例(警告:我还没有测试过).

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

这篇关于使用 ansible 从新创建的 ebs 卷中获取卷 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆