Skip to content

Instantly share code, notes, and snippets.

@VerosK
Created July 23, 2018 16:16
Show Gist options
  • Save VerosK/25a4488ed59064499c5c27a111d8e3fe to your computer and use it in GitHub Desktop.
Save VerosK/25a4488ed59064499c5c27a111d8e3fe to your computer and use it in GitHub Desktop.
Sort dict in Ansible
- hosts: localhost
vars:
images:
- name: first
date: 001
- name: second
date: 002
- name: fourth
date: 004
- name: third
date: 003
tasks:
- set_fact:
sorted: '{{ images|sort(attribute="date") }}'
reversed: '{{ images|sort(attribute="date", reverse=True) }}'
- debug:
var: sorted
- name: Show items ordered
debug:
msg: '{{ item.name }}'
with_items: "{{ sorted }}"
- name: Show items ordered reverse
debug:
msg: '{{ item.name }}'
with_items: "{{ reversed }}"
- name: Show items ordered except first one
debug:
msg: '{{ item.name }}'
with_items: "{{ sorted[1:] }}"
- name: Show items ordered except last one
debug:
msg: '{{ item.name }}'
with_items: "{{ sorted[:-1] }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment