本文介绍了ansible 获取已经存在的 aws ebs 卷 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ansible 获取已经存在并附加到 ec2 实例的 aws 卷 ID.我有一个使用 ec2_remote_facts 模块的查找任务,该模块获取 ec2 实例的详细信息,包括卷 ID 详细信息任务:

I'm trying to get the aws volume id which already exist and attached to ec2 instance using ansible.I have a lookup task using the ec2_remote_facts module that get details of the ec2 instance including the volume id detailsthe task:

- name: lookup ec2 virtual machines
  ec2_remote_facts:
    aws_access_key: "{{aws_access_key}}"
    aws_secret_key: "{{aws_secret_key}}"
    region: "{{ec2_region}}"
    filters:
      instance-state-name: running
      "tag:Name": "{{server_name}}"
      "tag:Environment": "{{environment_type}}"
      "tag:App": "{{app_name}}"
      "tag:Role": "{{role_type}}"
  register: ec2_info

示例输出:

    "msg": [
        {
            "ami_launch_index": "0",
            "architecture": "x86_64",
            "block_device_mapping": [
                {
                    "attach_time": "2017-01-12T17:24:17.000Z",
                    "delete_on_termination": true,
                    "device_name": "/dev/sda1",
                    "status": "attached",
                    "volume_id": "vol-123456789"
                }
            ],
            "client_token": "",
            "ebs_optimized": false,
            "groups": [
                {
                    "id": "sg-123456789",
                    "name": "BE-VPC"
                }
            ],
..... and more

现在我只需要获取块设备映射 -> volume_id 但我不知道如何只获取 ID

now I need to get only the block device mapping -> volume_id but I don't know how to get only the ID

我尝试了几项无法仅获取卷 ID 的任务,例如:

I have tried several tasks that didn't work to get only the volume id like:

-  debug: msg="{{item | map(attribute='block_device_mapping') | map('regex_search','volume_id') | select('string') | list }}"
   with_items: "{{ec2_info.instances | from_json}}"

这并不奏效:

- name: get associated vols
  ec2_vol:
    aws_access_key: "{{aws_access_key}}"
    aws_secret_key: "{{aws_secret_key}}"
    region:  "{{ec2_region}}"
    instance: "{{ ec2_info.isntances.id }}"
    state: list
    region: "{{ region }}"
  register: ec2_vol_lookup


- name: tag the volumes
  ec2_tag:
    aws_access_key: "{{aws_access_key}}"
    aws_secret_key: "{{aws_secret_key}}"
    region:  "{{ec2_region}}"
    resource: "{{ item.id }}"
    region:  "{{ region }}"
    tags:
      Environment: "{{environment_type}}"
      Groups: "{{group_name}}"
      Name: "vol_{{server_name}}"
      Role: "{{role_type}}"
  with_items: "{{ ec2_vol_lookup.volumes | default([]) }}"

有什么想法吗?

ansible 版本:2.2.0.0

ansible version: 2.2.0.0

推荐答案

如果你有单实例和单块设备,使用:

If you have single instance and single block device, use:

- debug: msg="{{ ec2_info.instances[0].block_device_mapping[0].volume_id }}"

如果你有很多实例和很多块设备:

If you have many instances and many block devices:

- debug: msg="{{ item.block_device_mapping | map(attribute='volume_id') | list }}"
  with_items: "{{ ec2_info.instances }}"

单实例多设备的情况:

- debug: msg="{{ ec2_info.instances[0].block_device_mapping | map(attribute='volume_id') | list }}"

这篇关于ansible 获取已经存在的 aws ebs 卷 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:06