Bootstrap FreeKB - Ansible - Return the lowest value in array using the min filter
Ansible - Return the lowest value in array using the min filter

Updated:   |  Ansible articles

min is a Jinja2 filter, used to return the lowest value in an array (also known as a list). The max filter is used to return the largest value. Let's say you are using the vars plugin to create an array of integers, like this.

vars:
  integers: 
    - 2
    - 4
    - 3
    - 1

 

Or the set_facts module, like this.

- set_fact:
    integers:
      - 2
      - 4
      - 3
      - 1

 

The debug module can be used to output the entire array, like this.

- name: output the contents of the 'integers' array
  debug: 
    msg: "{{ integers }}"

 

Which should return the following.

TASK [output the contents of the 'integers' array]
ok: [server1.example.com] => {
    "msg": [
        "2"
        "4",
        "3",
        "1"
    ]
}

 

The min filter can be used to get the lowest value from the array, like this.

- name: output the min value in the 'integers' array
  debug: 
    msg: "{{ integers | min }}"

 

Which should return the following.

TASK [output the min value in the 'integers' array]
ok: [server1.example.com] => {
    "msg": [
        "1"
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 45a133 in the box below so that we can be sure you are a human.