How to sort the list of integers in ansible?

How to sort the list of integers in ansible?

sort filter is used in ansible to sort the list or dictionary values.

We are going to use sort filter in ansible task to sort the list of integer values in ascending 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_list_values: "{{ list_values | sort }}"


    - name: Print the var

      debug:

        var: sorted_list_values


integer values in list_values are sorted in ascending order and assigned to new fact variable sorted_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_list_values": [

        1,

        2,

        3,

        4,

        9

    ]

}


PLAY RECAP ******************************************************************************************************************************************************************

localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0





Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^