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

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

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |