Ansible Tutorial
- 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 | sort(reverse=True) }}" - 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": [ 9, 4, 3, 2, 1 ] } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Lets see how to sort the list of strings in reverse order or descending order in ansible tasks?
- 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 | sort(reverse=True) }}" - 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": [ "text2", "text1", "info", "data3", "data1" ] } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Ansible Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page