Ansible - Return the number of items in a variable or array using the length filter

length is a Jinja2 filter, used to return the number of items in a variable or array. For example, let's say you've created the following variables and arrays using the vars plugin.

vars:
  foo: "Hello World"
  emptyfoo: ""
  bar:[ 'Hello', 'World' ]
  emptybar: []

 

The length filter can be used to return the number of items in each variable and array.

- debug:
    var: foo | length

- debug:
    var: emptyfoo | length

- debug:
    var: bar | length

- debug:
    var: emptybar | length

 

Something like this should be returned.

TASK [debug]
ok: [server1.example.com] => {
    "foo | length": "11"
}

TASK [debug]
ok: [server1.example.com] => {
    "emptyfoo | length": "0"
}

TASK [debug]
ok: [server1.example.com] => {
    "bar | length": "2"
}

TASK [debug]
ok: [server1.example.com] => {
    "emptybar | length": "0"
}

 



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 7bafc 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   |