How to save an array of facts using a debug: msg and with_sequence?

Hi,

The following are facts from win_pagefile query. From these, I want to retrieve the “maximum_size” fact of every element in the “pagefiles” array, then subtract the “initial_size” fact and divide that by 1024 in order to get every pagefile disk (in GB) to help automate a report on windows servers.

First, I am attempting to get only the “maximum_size” fact of every element in the array. Below are attempts at retrieving this fact using the with_sequence loop…wthout success. Please, help me to review which syntax would work or if there’s any other way. I’d be really grateful.

ok: [10.240.30.249] => {
“win_pf”: {
“automatic_managed_pagefiles”: false,
“changed”: false,
“failed”: false,
“pagefiles”: [
{
“caption”: “c:\ ‘pagefile.sys’”,
“description”: “‘pagefile.sys’ @ c:\”,
“initial_size”: 0,
“maximum_size”: 0,
“name”: “c:\pagefile.sys”
},
{
“caption”: “e:\ ‘pagefile.sys’”,
“description”: “‘pagefile.sys’ @ e:\”,
“initial_size”: 3583,
“maximum_size”: 9133,
“name”: “e:\pagefile.sys”
}
]
}
}

Below there are a number of ways I have tried:

There is no need to use with_sequence you can just use win_pf with with_items directly.

  - debug: msg="{{ (item.maximum_size - item.initial_size) / 1024 }}"
    with_items: "{{ win_pf.pagefiles }}"

This gives me 2 values:

TASK [debug] ***********************************************************************************************************************************************************************************
ok: [10.240.30.249] => (item={u’caption’: u"c:\ ‘pagefile.sys’“, u’maximum_size’: 0, u’initial_size’: 0, u’description’: u”‘pagefile.sys’ @ c:\“, u’name’: u’c:\pagefile.sys’}) => {
“msg”: “0.0”
}
ok: [10.240.30.249] => (item={u’caption’: u"e:\ ‘pagefile.sys’”, u’maximum_size’: 9133, u’initial_size’: 3583, u’description’: u"‘pagefile.sys’ @ e:\", u’name’: u’e:\pagefile.sys’}) => {
“msg”: “5.419921875”
}

How can I store them in 1 variable using set_fact?

There are many ways to store a value in a vaiable, if there is a list you are after something like this should work.

  - set_fact:
      myvar: '{{ myvar | default() + [(item.maximum_size - item.initial_size) / 1024] }}'
    with_items: "{{ win_pf.pagefiles }}"

Thanks a lot. Already tested it and it works perfectly.