dsaydon
dsaydon

Reputation: 4769

Ansible: create a list by extracting several attributes from another list

I have a list like this:

first list:

volumes:
      - {device_name: '/dev/sda1', volume_type: 'gp2', volume_size: '80', delete_on_termination: true ,os_device_name: '/dev/xvda', mount_path: '/'}
      - {device_name: '/dev/xvdb', volume_type: 'standard', volume_size: '500', delete_on_termination: true, os_device_name: '/dev/xvdb', mount_path: '/data' }

and I want to create a new list with the attributes:

device_name,volume_type,volume_size,delete_on_termination

to be look like this in one line:

second list:

volumes:
      - {device_name: /dev/sda1, volume_type: 'gp2', volume_size: '80', delete_on_termination: true}
      - {device_name: '/dev/xvdb', volume_type: 'standard', volume_size: '500', delete_on_termination: true}

why?

I need to create an ec2 instance with the ec2 module with several volumes in the same volumes paramer

- name: Create new ec2 instance
  ec2:
    ....
    volumes: "{{volumes}}"
    ....

FYI: the second list is working as expected but I can't find a way to extract the only specific attributes that I need from the first list

any idea how?

Upvotes: 0

Views: 354

Answers (1)

techraf
techraf

Reputation: 68479

I have a dict like this:

volumes:
      - ...
      - ...

This is not a dictionary, but a list.


And this is the solution:

- set_fact:
    volumes: "{{ volumes | json_query(jsonquery) }}"
  vars:
    jsonquery: "[].{device_name: device_name, volume_type: volume_type, volume_size: volume_size, delete_on_termination: delete_on_termination}"

Upvotes: 1

Related Questions