Amazon ec2 ansible获取已存在的aws ebs卷id

Amazon ec2 ansible获取已存在的aws ebs卷id,amazon-ec2,ansible,ansible-playbook,ansible-2.x,Amazon Ec2,Ansible,Ansible Playbook,Ansible 2.x,我正在尝试使用ansible获取已经存在并连接到ec2实例的aws卷id。 我有一个使用ec2_remote_facts模块的查找任务,该模块获取ec2实例的详细信息,包括卷id详细信息 任务: - name: lookup ec2 virtual machines ec2_remote_facts: aws_access_key: "{{aws_access_key}}" aws_secret_key: "{{aws_secret_key}}" region: "{

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

- 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
现在我只需要获取块设备映射->卷id,但我不知道如何只获取id

我尝试了几个任务,但都没有成功,只能获取卷id,如:

-  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

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

- debug: msg="{{ ec2_info.instances[0].block_device_mapping[0].volume_id }}"
如果有多个实例和多个块设备:

- 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 }}"

如果您可以通过名称或在卷上设置的其他标记来识别卷,则可以使用
ec2\u vol\u facts
模块:

ec2_vol_facts:
  region: <your-ec2-region>
  filters:
    "tag:Name": <your-volume-name>
    "tag:Role": <e.g. db-data>
register: ec2_vol

debug:
  msg: "Ids: {{ ec2_vol.volumes | map(attribute='id') | list | to_nice_json }}"
ec2卷事实:
地区:
过滤器:
“标记:名称”:
“标记:角色”:
注册编号:ec2_vol
调试:
msg:“id:{ec2_vol.volumes | map(attribute='id')| list | to_nice_json}”

为了标记连接到AWS ec2实例的所有卷,我使用了以下方法:

- name: Get instance ec2 facts
  ec2_remote_facts:
    region: "{{ aws_region }}"
    filters:
      "private_ip_address": "{{ inventory_hostname }}"
  register: ec2_info

- name: Tag volumes
  ec2_tag:
    region: "{{ aws_region }}"
    resource: "{{ item.volume_id }}"
    state: present
    tags:
      Name: "{{ ec2_info.instances[0].tags.Name }} - {{ item.device_name}}"
  with_items:
    - "{{ ec2_info.instances[0].block_device_mapping }}"
卷标记将由实例标记名和设备名(即/dev/sdf)组成

^可以有任何方法,您可以使用这些方法获取实例id
^当测试任务幂等元的条件时,我要做的是得到卷id(ansible 2.9.X):


ec2\u信息[0]。块设备映射[0]。卷id
?nop。我收到错误:“”字段“args”的值无效,似乎包含未定义的变量。错误是:dict对象没有元素“我不知道您正在运行什么。您包括了“示例输出”,并从该“示例输出”中获取
volume\u id
“你可以使用我建议的表达方式。谢谢你的回答,但这不起作用。我不知道为什么,如果您可以在回答中添加以下内容,即您可以使用{{ec2_info.instances[0].block_device_mapping | map(attribute='volume_id')| list}处理1个ebs设备和更多设备的1个ec2 INSTANCE。在ec2_标记模块上测试:带有_项:“{{ec2_info.instances[0]。块_设备_映射|映射(attribute='volume_id')|列表}”
- name: Get instances list
  ec2_instance_facts:
    filters:
      "tag:instance": "{{ instance_inventory | map(attribute='instance_id') | list }}"
    region: us-east-1
  register: ec2_sets

- name: Delete instance' volume(s)
  ec2_vol:
    id: '{{ item }}'
    state: absent
  loop: "{{ ec2_sets.instances | sum(attribute='block_device_mappings', start=[]) | map(attribute='ebs.volume_id') | list }}"
  when: ( ec2_sets.instances | sum(attribute='block_device_mappings', start=[]) | map(attribute='ebs.volume_id') | list | length )
- name: gather ec2 remote facts
  ec2_instance_info:
    filters:
      vpc-id: "{{ vpc }}"
      subnet-id: "{{subnet}}"
      "tag:Name": "my_instance_name"
    region: "{{ region }}"
  register: ec2_instance_info


- debug:
    msg: "index: {{item.1.tags.Name}} volume info: {{item.1.block_device_mappings[0].ebs.volume_id}}"
  with_indexed_items:
    - "{{ ec2_instance_info.instances }}"