How to sort list of strings in ansible?

How to sort list of strings in ansible?

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

In this ansible playbook, we are going to sort the list of strings in one of the task and printing the sorted strings result.

- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

	- name: initialize list of strings
  	set_fact:
    	list_values: ['2', '4', '1','9', '3']

	- name: sort defined list
  	set_fact:
    	sorted_list_strings: "{{ list_values | sort }}"

	- 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": [
    	"1",
    	"2",
    	"3",
    	"4",
    	"9"
	]
}

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

list of number foramt strings are sorted in ascending order and assigned to new fact variable sorted_list_strings and displayed the variable.

Lets use input strings in the list values and see the result getting correctly or not.

- 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 }}"

	- 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": [
    	"data1",
    	"data3",
    	"info",
    	"text1",
    	"text2"
	]
}

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
^