在Ansible中获取非root设备的volume_id

时间:2018-05-28 12:07:58

标签: amazon-web-services ansible

如何过滤掉(假设使用何时)不是'dev / sda1'的ebs卷?

例如,剧本是:

- name:                     List Volumes
  ec2_vol:
    region:                 "{{ aws_vars.region }}"
    instance:               "{{ ec2.instances[0].instance_id }}"
    state:                  list
  register:                 volumes
  when:                     volumes.device_name != '/dev/sda1'
- debug:                    msg="{{ volumes.volumes | map(attribute='id') | list }}"

这不起作用。

卷的var类似于:

[
        {
            "attachment_set": {
                "attach_time": "2018-05-28T11:17:56.000Z",
                "deleteOnTermination": "false",
                "device": "xvdf",
                "instance_id": "i-00ac0585d1d4974aa",
                "status": "attached"
            },
            "create_time": "2017-07-18T14:54:28.969Z",
            "encrypted": false,
            "id": "vol-0506598d250ffe3d3",
            "iops": 450,
            "size": 150,
            "snapshot_id": "snap-0ce6832b64cfa093d",
            "status": "in-use",
            "tags": {
                "Site Desktop 1": "IIS",
                "project-group": "DEV"
            },
            "type": "gp2",
            "zone": "eu-central-1b"
        }

1 个答案:

答案 0 :(得分:3)

when语句在 任务之前执行(它决定是否应该运行任务),因此不能用于过滤结果。

使用set_fact module,您可以编辑任务的结果并将其保存在新变量中。我将它与j son query filter

结合使用
  - name:                     List Volumes
    ec2_vol:
      region:                 "eu-west-1"
      instance:               "i-0f7gd2b0090924864"
      state:                  list
    register:                 volumes
  - set_fact: filtered="{{ volumes.volumes | json_query('[?attachment_set.device!=`/dev/sda1`]') }}"
  - debug:                    msg="{{ filtered }}"