Ansible Tutorial
reverse filter is also available in ansible to reverse the list of values like sort filter.
Lets see the example using reverse filter,
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize list of values set_fact: list_values: [2, 4, 1, 9, 3] - name: sort defined list set_fact: sorted_desc_list_values: "{{ list_values | reverse }}" - name: Print the var debug: var: sorted_desc_list_values
Output:
$ ansible-playbook sort_list.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize list of values] ******************************************************************************************************************************************** ok: [localhost] TASK [sort defined list] **************************************************************************************************************************************************** ok: [localhost] TASK [Print the var] ******************************************************************************************************************************************************** ok: [localhost] => { "sorted_desc_list_values": "" } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
when we use reverse filter, getting "
So we need use list filter to get the list of values in reverse order.
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize list of values set_fact: list_values: [2, 4, 1, 9, 3] - name: sort defined list set_fact: sorted_desc_list_values: "{{ list_values | reverse | list }}" - name: Print the var debug: var: sorted_desc_list_values
Output:
$ ansible-playbook sort_list.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize list of values] ******************************************************************************************************************************************** ok: [localhost] TASK [sort defined list] **************************************************************************************************************************************************** ok: [localhost] TASK [Print the var] ******************************************************************************************************************************************************** ok: [localhost] => { "sorted_desc_list_values": [ 3, 9, 1, 4, 2 ] } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Looks like result of list values are not sorted in descending order correctly.
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize list of strings set_fact: list_values: ['text2', 'text1', 'data3','data1', 'info'] - name: sort defined list set_fact: sorted_list_strings: "{{ list_values | reverse | list }}" - name: Print the var debug: var: sorted_list_strings
Output:
$ ansible-playbook sort_list_strings.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize list of strings] ******************************************************************************************************************************************* ok: [localhost] TASK [sort defined list] **************************************************************************************************************************************************** ok: [localhost] TASK [Print the var] ******************************************************************************************************************************************************** ok: [localhost] => { "sorted_list_strings": [ "info", "data1", "data3", "text1", "text2" ] } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Looks like result of list strings are also not sorted in descending order correctly.
Ansible Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page