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

29 views
Skip to first unread message

Gabriel Rios

unread,
Jul 30, 2019, 1:45:37 PM7/30/19
to Ansible Project
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:

---
- hosts: all
vars_files:
- winrms.yml
tasks:
- name: Saving facts into win_pf variable
win_pagefile:
register: win_pf
- name: Set number of disks from where to retrieve facts
set_fact:
listapag: "{{win_pf.pagefiles|length - 1}}"

Here is where I struggle:

Attempt 1:

- name: save m_size
set_fact:
list_ms: "{{win_pf.pagefiles[item].maximum_size}}"
with_sequence:
- start=0 end={{lista}}

Gives error:
TASK [save m_size] ********************************************************************************************* fatal: [10.240.30.249]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute u'0'\n\nThe error appears to be in '/home/gabriel/Conex_ansible/mainwin12.yml': line 19, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: save m_size\n ^ here\n"}

I tried to put item in {{}}, "" or both, but it gave similar error messages.


Attempt 2:
- name: save m_size
set_fact:
list_ms: "{{win_pf.pagefiles}} + [{{item}}]"
with_sequence:
- start=0 end={{lista}}
or

- name: save m_size
set_fact:
list_ms: "{{win_pf.pagefiles}}"
with_sequence:
- start=0 end={{lista}}

Give:

TASK [show m_size] ***************************************************************************************************************************************************************************** ok: [10.240.30.249] => { "list_ms": [ { "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" }, 1 ] }

But the form
- name: save m_size
set_fact:
list_ms: "{{win_pf.pagefiles}} + [{{item}}] + {{.maximum_size}}"
with_sequence:
- start=0 end={{lista}}

Gives the error:
fatal: [10.240.30.249]: FAILED! => {"msg": "template error while templating string: unexpected '.'. String: {{win_pf.pagefiles}} + [{{item}}] + {{.maximum_size}}"}

Attempt 3:
#I tried without .maximum_size first, as in Attempt 2
- name: Show maximum_size
debug:
msg: "{{item}}"
with_sequence:
- start=0 end={{listapag}} format=win_pf.pagefiles[%1x]

Just gives:

TASK [Show maximum_size] *********************************************************************************************************************************************************************** ok: [10.240.30.249] => (item=win_pf.pagefiles[0]) => { "msg": "win_pf.pagefiles[0]" } ok: [10.240.30.249] => (item=win_pf.pagefiles[1]) => { "msg": "win_pf.pagefiles[1]" }

Kai Stian Olstad

unread,
Jul 30, 2019, 2:33:54 PM7/30/19
to ansible...@googlegroups.com
On 30.07.2019 19:45, Gabriel Rios wrote:
> 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.

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


--
Kai Stian Olstad

Gabriel Rios

unread,
Jul 30, 2019, 4:08:28 PM7/30/19
to Ansible Project
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?

Kai Stian Olstad

unread,
Jul 30, 2019, 4:29:06 PM7/30/19
to ansible...@googlegroups.com
On 30.07.2019 22:08, Gabriel Rios wrote:
> 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] }}'

Gabriel Rios

unread,
Jul 31, 2019, 1:30:46 PM7/31/19
to Ansible Project
Thanks a lot. Already tested it and it works perfectly.
Reply all
Reply to author
Forward
0 new messages