I need to capture the result of MSSQL Queries in Ansible output however, there are no supported Ansible modules which help me to achieve this. As a workaround, i have written a powershell script which runs the SQL query and returns the output in a weird format with lots of unnecessary data which i would like to remove.
PowerShell Script
$ServerInstance = 'serverhostname'
$Database = 'master'
$Query = "DECLARE @sqlVers numeric(2,0)
DECLARE @compatlevel numeric(3,0)
SET @sqlVers = left(cast(serverproperty('productversion') as varchar), 2)
SET @compatlevel=@sqlVers*10
SELECT name,compatibility_level
FROM sys.databases WHERE compatibility_level<>@compatlevel"
$SqlcmdOptions = @"
-S"$ServerInstance" -d "$Database" -Q "$Query"
"@
Start-Process -FilePath "SQLCMD.EXE" -ArgumentList @"
$SqlcmdOptions
"@ -Wait -NoNewWindow -RedirectStandardOutput C:\temp\SQLCOMPLVL.txt -PassThru
Get-Content C:\temp\SQLCOMPLVL.txt
Powershell script output:
How can format the above either through Powershell script or in ansible in such a way that i get only the data in the following way: name comptibility_level data value
or in short, i could get data between name and (x rows affected) and could convert it into JSON format so that Ansible could collect powershell script output in JSON format.