Reputation: 372
How do I filter out (assuming the use of when) ebs volumes that are not 'dev/sda1'?
For example, the playbook is:
- 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 }}"
This does not work.
The var for volumes is something like:
[
{
"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"
}
Upvotes: 0
Views: 381
Reputation: 8026
The when statement is executed before the task (it decides whether the task should be run or not), so it can't be used to filter the results.
Using the set_fact module you can edit the results from a task and save them in a new variable. I used that in combination with the json 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 }}"
Upvotes: 3