【问题标题】:Get volume id from newly created ebs volume using ansible使用 ansible 从新创建的 ebs 卷中获取卷 ID
【发布时间】:2013-09-18 20:43:22
【问题描述】:

我使用 ansible 的 ec2_vol 模块来创建一个 ebs 卷。看了源码,发现它内部调用了boto的create_volume()方法,带有用户指定的参数。我想注册ec2_vol模块的返回值,获取新创建的volumes的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() 方法返回值的适当文档。

感谢任何帮助。

谢谢,

【问题讨论】:

    标签: amazon-web-services amazon-ec2 cloud orchestration ansible


    【解决方案1】:

    根据ec2_vol module source code,模块返回:

    • 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
    

    【讨论】:

    • 是的,它奏效了...非常感谢..但是您是如何发现 ec2_volumes 字典中使用的键名是“结果”而不是其他东西...我查找了一些文档,但是没找到...
    • @Pattu 我通过执行与上述类似的操作发现,使用调试转储变量的内容。我已经提交了一个拉取请求来改进这方面的文档:github.com/ansible/ansible/pull/4099
    • @Pattu 如果这回答了你的问题,你能接受提供的答案吗?
    • 我看到了 github pull request..这听起来可能很傻,但我不明白你接受答案的意思....
    • 知道了,和_nested一起使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2018-04-26
    • 2016-07-09
    • 2021-05-22
    • 2019-04-06
    相关资源
    最近更新 更多