Since the result is produced by a jinja2 statement, the representation is a
string. You should still convert to an integer when using the result, ie in
a "when condition".
I use something like this to only install a package when there's enough
free disk space:
---
- hosts: winhosts
name: Install {{ packagename }}
gather_facts: false
vars:
packagename: name-of-the-package
tempdir: C:\Windows\Temp\{{ packagename }}
required_space: 20
tasks:
- name: execute powershell
win_command: powershell.exe "Get-PSDrive C | Select-Object Free |
ConvertTo-Json"
register: getpsdrive
- name: Register freediskspace as a fact
set_fact:
freediskspace: "{{ (getpsdrive.stdout | from_json).Free // 1073741824
}}"
- block:
- name: Create a temporary directory
...
- name: Copy stuff to the temporary directory
...
- name: 'Install {{ packagename }}'
...
- name: 'Remove {{ packagename }} install files'
...
when:
- freediskspace | int > required_space
Bests,
Bram
Op donderdag 14 juli 2016 21:57:16 UTC+2 schreef J Hawkesworth:
>
> Thanks for this, that's a nice trick. I have a feeling there ought to be
> a simpler way of doing it but haven't thought what that might be yet.
> Jon
>
> On Thursday, July 14, 2016 at 7:27:10 AM UTC+1, ishan jain wrote:
>>
>> Thanks Jon, this solved my issue. I didn't know about this convertto-Json
>> thing and it seems to be very useful. The only problem is the raw module
>> which would still require lot of string operations to find the desired
>> value.
>>
>> Using "Get-PsDrive C |select-object Free|ConvertTo-json", i get the value
>> in Ansible like this:
>>
>> TASK [debug]
>> *******************************************************************
>> ok: [AC05] => {
>> "drive | to_json": {
>> "changed": false,
>> "rc": 0,
>> "stderr": "",
>> "stdout": "{\r\n \"Free\": 38710173696\r\n}\r\n",
>> "stdout_lines": [
>> "{",
>> " \"Free\": 38710173696",
>> "}"
>> ]
>> }
>> }
>>
>>
>> This is still not very useful. So i used *from_json* to parse the output.
>>
>> - debug: var="(drive.stdout | from_json).Free"
>>
>> and thus got the output in proper format:
>>
>> TASK [debug]
>> *******************************************************************
>> ok: [AC05] => {
>> "(drive.stdout | from_json).Free": "38709694464"
>> }
>>
>>
>> On Wednesday, 13 July 2016 18:01:11 UTC+5:30, J Hawkesworth wrote:
>>>
>>> Try something like this:
>>>
>>> Get-PsDrive C |select-object Free|ConvertTo-json
>>>
>>> You should then be able to use 'register' to capture the result and make
>>> use of it.
>>>
>>> By the way I found this which has a lot of good starting points for
>>> converting unix toolbox commands into powershell
>>> https://www.gitbook.com/book/devopscollective/a-unix-person-s-guide-to-powershell/details
>>>
>>> Hope this helps,
>>>
>>> Jon
>>>
>>>
>>>
>>> On Wednesday, July 13, 2016 at 12:06:44 PM UTC+1, ishan jain wrote:
>>>>
>>>> I am trying very hard to get a numerical value of free disk space on a
>>>> windows server 2012 R2 machine, but couldn't get it so far. All the
>>>> commands that are available for listing the disk space information - they
>>>> are not providing the output in proper format. Here are a few commands i
>>>> tried:
>>>>
>>>> *raw: Get-PSDrive C | Select-Object Free*
>>>>
>>>> *Output:*
>>>>
>>>> TASK [debug]
>>>> *******************************************************************
>>>> ok: [AC05] => {
>>>> "freeSpace": {
>>>> "changed": false,
>>>> "rc": 0,
>>>> "stderr": "",
>>>> "stdout": "\r\n
>>>> Free\r\n
>>>> ----\r\n
>>>> 53051551744\r\n\r\n\r\n",
>>>> "stdout_lines": [
>>>> "",
>>>> "
>>>> Free",
>>>> "
>>>> ----",
>>>> "
>>>> 53051551744",
>>>> "",
>>>> ""
>>>> ]
>>>> }
>>>> }
>>>>
>>>>
>>>> *raw: fsutil volume diskfree C: | find /i "avail free bytes"*
>>>>
>>>> *output:*
>>>>
>>>> TASK [debug]
>>>> *******************************************************************
>>>> ok: [AC05] => {
>>>> "drive": {
>>>> "changed": false,
>>>> "rc": 0,
>>>> "stderr": "",
>>>> "stdout": "Total # of avail free bytes : 53056536576\r\n",
>>>> "stdout_lines": [
>>>> "Total # of avail free bytes : 53056536576"
>>>> ]
>>>> }
>>>> }
>>>>
>>>> wmic logicaldisk get size
>>>>
>>>> output: (no way of getting info for single drive)
>>>>
>>>> Size
>>>>
>>>> 63899168768
>>>>
>>>> 53683941376
>>>>
>>>>
>>>> ------------------
>>>>
>>>>
>>>> As you can see, there really doesn't seems to be a way of knowing the
>>>> exact numerical value of free disk space in windows machine. Anybody doing
>>>> this without using any external utility ?
>>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/a310326b-a600-48e2-9898-8d0b913f4167%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.