Recommended file format (YAML? JSON? Other?) - json
Before I start I should state that I'm new to both YAML and JSON so the rules of formatting are not that clear.
I'm trying to write a Perl script (Perl because I know it to exist on all of our servers.) which will update several network-related settings for various hosts. My preference is to have all of the settings in a single file and update the configurations based on which host the script is running on.
I looked at YAML, but I'm a bit put off by the fact that I can't do something like:
host:
hostname: first
interface: eth0
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
interface: eth1
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
host:
hostname: second
interface: eth0
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
interface: eth1
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
That is to say, I've plugged this into YAML validators and it has failed.
I have figured out that, for YAML, I can do the following:
host: "first"
interface1:
name: eth0
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
interface2:
name: eth1
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
This is less than desirable, though, as it makes having multiple hosts in one file impossible. I'm basing this on the fact that I keep getting errors from the online validators that I've used when I do attempt this.
I've looked at using JSON, but I don't know all of the rules for that either. I do know that the following does not work:
{
"host": "first",
"interface1": {
"newip": "2.3.4.5",
"oldip": "1.2.3.4",
"oldgw": "1.2.3.1",
"name": "eth0",
"newgw": "2.3.4.1"
},
"interface2": {
"newip": "2.3.4.5",
"oldip": "1.2.3.4",
"oldgw": "1.2.3.1",
"name": "eth1",
"newgw": "2.3.4.1"
}
}
{
"host": "second",
"interface1": {
"newip": "2.3.4.5",
"oldip": "1.2.3.4",
"oldgw": "1.2.3.1",
"name": "eth0",
"newgw": "2.3.4.1"
},
"interface2": {
"newip": "2.3.4.5",
"oldip": "1.2.3.4",
"oldgw": "1.2.3.1",
"name": "eth1",
"newgw": "2.3.4.1"
}
}
Is there a format I can use that will allow me to store all of the host and their information in a single file that can be parsed?
If either YAML or JSON are suitable, what am I doing wrong?
Your YAML problem with host is the same as what it was initially with interface: You're trying to put subkeys at the same level as the keys that contain them.
host:
name: first
interface1:
name: eth0
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
interface2:
name: eth1
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
should work, although that still doesn't address your need for multiple hosts. For that (and to better handle multiple interfaces), you should use lists:
host:
- name: first_host
interface:
- name: eth0
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
- name: eth1
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
- name: second_host
interface:
- ...
When read in by Perl, this will give you the structure:
{
"host": [
{
"interface": [
{
"newip": "2.3.4.5",
"oldip": "1.2.3.4",
"oldgw": "1.2.3.1",
"name": "eth0",
"newgw": "2.3.4.1"
},
{
"newip": "2.3.4.5",
"oldip": "1.2.3.4",
"oldgw": "1.2.3.1",
"name": "eth1",
"newgw": "2.3.4.1"
}
],
"name": "first_host"
}
]
}
As far as JSON, that's a subset of YAML. Personally, I prefer to have the full YAML spec available to me, but JSON provides more interoperability with non-Perl languages.
I'd prefer JSON over YAML. I recently built a system whose "user interface" (ha) was basically one giant config file; the user needed to edit that config file to control the system; and I used YAML for that file. It turns out that YAML has a few really annoying gotchas that make it unsuitable for humans -- it's very picky about whitespace, for example.
Also, it's less familiar in general: I'd guess that anyone with programming experience has run into JSON, and understands it. But YAML is more niche.
If you're not using the advanced features of YAML -- such as the ability to define variables and then reference them later -- I'd recommend that you go with JSON instead.
Do not care about the format. Populate a data structure and let JSON or YAML do the dirty work for you. If you are going to produce and parse the files yourself anyway, there is almost no advantage in using JSON or YAML.
The exact format is unimportant: both YAML and JSON are fine. Actually, I would recommend to keep this specific part pluggable.
The issue with your YAML is that the data structure has to make some sense, e.g.:
- host:
hostname: first
interfaces:
eth0:
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
eth1:
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
- host:
hostname: second
interfaces:
...
Or if the interfaces have to be ordered:
- host:
hostname: first
interfaces:
-
name: eth0
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
-
name: eth1
oldip: 1.2.3.4
newip: 2.3.4.5
oldgw: 1.2.3.1
newgw: 2.3.4.1
If writing YAML manually is to tedious for you, just write a small script that generates it for you.
Note that lists have to be somehow introduces by a marker like -. Intendation is not sufficient for this.
Related
export results from jq to csv
I'm having the following dataset which I'd like to export into a CSV: Dataset: { "data": { "activeFindings": { "findings": [ { "findingId": "risk#80703", "accountId": "00000000-000000-0000000-000000", "products": [ "GWSERVER01" ], "findingDisplayName": "risk#80703", "severity": "CRITICAL", "findingDescription": "PSOD with re-formatting a valid dedup metadata block.", "findingImpact": "Potential ESXi host crash", "recommendations": [ "This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523)", "This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804)" ], "kbLinkURLs": [ "https://kb.vmware.com/s/article/80703" ], "recommendationsVCF": [ "This issue is resolved with VMware Cloud Foundation 4.1" ], "kbLinkURLsVCF": [ "https://kb.vmware.com/s/article/80703" ], "categoryName": "Storage", "findingTypes": [ "UPGRADE" ], "firstObserved": 1629806351877, "totalAffectedObjectsCount": 12, "affectedObjects": [ { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server01.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server02.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server03.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server04.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server05.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server06.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server07.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 } ] } ], "totalRecords": 1, "timeTaken": 56 } } } { "data": { "activeFindings": { "findings": [ { "findingId": "risk#80703", "accountId": "00000000-000000-0000000-000000", "products": [ "GWSERVER02.corp.contoso.org" ], "findingDisplayName": "risk#80703", "severity": "CRITICAL", "findingDescription": "PSOD with re-formatting a valid dedup metadata block.", "findingImpact": "Potential ESXi host crash", "recommendations": [ "This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523)", "This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804)" ], "kbLinkURLs": [ "https://kb.vmware.com/s/article/80703" ], "recommendationsVCF": [ "This issue is resolved with VMware Cloud Foundation 4.1" ], "kbLinkURLsVCF": [ "https://kb.vmware.com/s/article/80703" ], "categoryName": "Storage", "findingTypes": [ "UPGRADE" ], "firstObserved": 1635968448112, "totalAffectedObjectsCount": 2, "affectedObjects": [ { "sourceName": "GWSERVER02.corp.contoso.org", "objectName": "server10.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17167734", "solutionTags": [], "firstObserved": 1635968448112 }, { "sourceName": "GWSERVER02.corp.contoso.org", "objectName": "server11.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17167734", "solutionTags": [], "firstObserved": 1635968448112 } ] } ], "totalRecords": 1, "timeTaken": 51 } } } And header would be as follow: "Finding Id","Issue Description","Risk if no Action Taken","Severity","Recommendations","Source Name","Object Name","Object Type","Host Version","Build","First Observed","Reference" Header keys mapping as follow: Finding Id = findingId Issue Description = findingDescription Risk if no Action Taken = findingImpact Severity = severity Recommendations = recommendations Source Name = sourceName Object Name = objectName Object Type = objectType Host Version = version Build = buildNumber First Observed = firstObserved Reference = kbLinkURLs Unfortunately, we have to perform an API call per each finding & product (eg: we're not able to pull all the findings for all products at once - the API does not allow us to perform such query and thus, we have to make several calls to get all the findings with its associated objects.) With that said, what would be the preferred approach to export the data into a csv ? Would using jq's #CSV work though we would have to loop through several nodes ? Any help/guidance would be appreciated. Thanks! Note 1: A stripped version of the dataset as requested by chepner { "data": { "activeFindings": { "findings": [ { "findingId": "risk#80703", "accountId": "00000000-000000-0000000-000000", "products": [ "GWSERVER01" ], "findingDisplayName": "risk#80703", "severity": "CRITICAL", "findingDescription": "PSOD with re-formatting a valid dedup metadata block.", "findingImpact": "Potential ESXi host crash", "recommendations": [ "This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523)", "This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804)" ], "kbLinkURLs": [ "https://kb.vmware.com/s/article/80703" ], "recommendationsVCF": [ "This issue is resolved with VMware Cloud Foundation 4.1" ], "kbLinkURLsVCF": [ "https://kb.vmware.com/s/article/80703" ], "categoryName": "Storage", "findingTypes": [ "UPGRADE" ], "firstObserved": 1629806351877, "totalAffectedObjectsCount": 12, "affectedObjects": [ { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server01.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, { "sourceName": "GWSERVER01.corp.contoso.org", "objectName": "server02.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17499825", "solutionTags": [], "firstObserved": 1629806351877 }, ] } ], "totalRecords": 1, "timeTaken": 56 } } } { "data": { "activeFindings": { "findings": [ { "findingId": "risk#80703", "accountId": "00000000-000000-0000000-000000", "products": [ "GWSERVER02.corp.contoso.org" ], "findingDisplayName": "risk#80703", "severity": "CRITICAL", "findingDescription": "PSOD with re-formatting a valid dedup metadata block.", "findingImpact": "Potential ESXi host crash", "recommendations": [ "This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523)", "This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804)" ], "kbLinkURLs": [ "https://kb.vmware.com/s/article/80703" ], "recommendationsVCF": [ "This issue is resolved with VMware Cloud Foundation 4.1" ], "kbLinkURLsVCF": [ "https://kb.vmware.com/s/article/80703" ], "categoryName": "Storage", "findingTypes": [ "UPGRADE" ], "firstObserved": 1635968448112, "totalAffectedObjectsCount": 2, "affectedObjects": [ { "sourceName": "GWSERVER02.corp.contoso.org", "objectName": "server10.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17167734", "solutionTags": [], "firstObserved": 1635968448112 }, { "sourceName": "GWSERVER02.corp.contoso.org", "objectName": "server11.corp.contoso.org", "objectType": "ESX", "version": "6.7.0", "buildNumber": "17167734", "solutionTags": [], "firstObserved": 1635968448112 } ] } ], "totalRecords": 1, "timeTaken": 51 } } } And the resulted CSV file: "Finding Id","Issue Description","Risk if no Action Taken","Severity","Recommendations","Source Name","Object Name","Object Type","Host Version","Build","First Observed","Reference" "risk#80703","PSOD with re-formatting a valid dedup metadata block.","Potential ESXi host crash","CRITICAL","This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804);This issue is resolved with VMware Cloud Foundation 4.1","GWSERVER01.corp.contoso.org","server01.corp.contoso.org","HostSystem","6.7.0","17499825","1629806351877","https://kb.vmware.com/s/article/80703" "risk#80703","PSOD with re-formatting a valid dedup metadata block.","Potential ESXi host crash","CRITICAL","This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804);This issue is resolved with VMware Cloud Foundation 4.1","GWSERVER01.corp.contoso.org","server02.corp.contoso.org","HostSystem","6.7.0","17499825","1629806351877","https://kb.vmware.com/s/article/80703" "risk#80703","PSOD with re-formatting a valid dedup metadata block.","Potential ESXi host crash","CRITICAL","This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804);This issue is resolved with VMware Cloud Foundation 4.1","GWSERVER02.corp.contoso.org","server10.corp.contoso.org","HostSystem","6.7.0","17167734","1635968448112","https://kb.vmware.com/s/article/80703" "risk#80703","PSOD with re-formatting a valid dedup metadata block.","Potential ESXi host crash","CRITICAL","This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804);This issue is resolved with VMware Cloud Foundation 4.1","GWSERVER02.corp.contoso.org","server11.corp.contoso.org","HostSystem","6.7.0","17167734","1635968448112","https://kb.vmware.com/s/article/80703"
I would combine jq with spyql, here's how: jq -c '.data.activeFindings.findings[]' full_sample.json | spyql "SELECT json->findingId AS 'Finding Id', json->findingDescription AS 'Issue Description', json->findingImpact AS 'Risk if no Action Taken', json->severity AS Severity, ';'.join(json->recommendations) AS Recommendations, json->affectedObjects->sourceName AS 'Source Name', json->affectedObjects->objectName AS 'Object Name', json->affectedObjects->objectType AS 'Object Type', json->affectedObjects->version AS 'Host Version', json->affectedObjects->buildNumber AS Build, json->affectedObjects->firstObserved AS 'First Observed', ';'.join(json->kbLinkURLsVCF) AS Reference FROM json EXPLODE json->affectedObjects TO csv" Finding Id,Issue Description,Risk if no Action Taken,Severity,Recommendations,Source Name,Object Name,Object Type,Host Version,Build,First Observed,Reference risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server01.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server02.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server03.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server04.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server05.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server06.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server07.corp.contoso.org,ESX,6.7.0,17499825,1629806351877,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER02.corp.contoso.org,server10.corp.contoso.org,ESX,6.7.0,17167734,1635968448112,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER02.corp.contoso.org,server11.corp.contoso.org,ESX,6.7.0,17167734,1635968448112,https://kb.vmware.com/s/article/80703 I am using jq to extract the part of the JSON we need, while compressing the output to JSON lines (required by spyql). Then, spyql takes care of the rest, namely joining arrays (expressions are python with some optional syntax sugar), renaming columns and generating the CSV. If you want to convert the firstObserved timestamp to datetime you can do it like this (assuming UTC timestamp): $ jq -c '.data.activeFindings.findings[]' full_sample.json | spyql "SELECT json->findingId AS 'Finding Id', json->findingDescription AS 'Issue Description', json->findingImpact AS 'Risk if no Action Taken', json->severity AS Severity, ';'.join(json->recommendations) AS Recommendations, json->affectedObjects->sourceName AS 'Source Name', json->affectedObjects->objectName AS 'Object Name', json->affectedObjects->objectType AS 'Object Type', json->affectedObjects->version AS 'Host Version', json->affectedObjects->buildNumber AS Build, datetime.utcfromtimestamp(json->affectedObjects->firstObserved/1000) AS 'First Observed', ';'.join(json->kbLinkURLsVCF) AS Reference FROM json EXPLODE json->affectedObjects TO csv" Finding Id,Issue Description,Risk if no Action Taken,Severity,Recommendations,Source Name,Object Name,Object Type,Host Version,Build,First Observed,Reference risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server01.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server02.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server03.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server04.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server05.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server06.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER01.corp.contoso.org,server07.corp.contoso.org,ESX,6.7.0,17499825,2021-08-24 11:59:11.877000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER02.corp.contoso.org,server10.corp.contoso.org,ESX,6.7.0,17167734,2021-11-03 19:40:48.112000,https://kb.vmware.com/s/article/80703 risk#80703,PSOD with re-formatting a valid dedup metadata block.,Potential ESXi host crash,CRITICAL,This issue is resolved in VMware ESXi 6.7 upgrade to Patch 05 (17700523);This issue is resolved in VMware ESXi 7.0 upgrade to Update 1 (16850804),GWSERVER02.corp.contoso.org,server11.corp.contoso.org,ESX,6.7.0,17167734,2021-11-03 19:40:48.112000,https://kb.vmware.com/s/article/80703 If you don't need millisecond precision in your datetime you can use integer division (i.e. datetime.utcfromtimestamp(json->affectedObjects->firstObserved//1000)). Disclaimer: I am the author of spyql
You can apply #csv at the last part after removing the yielded null values through use of delpaths([path(.[] | select(.==null))]) in order to prevent generating successive redundant commas such as jq -r '.data.activeFindings.findings[] | [.findingId , .findingDescription, .findingImpact, .severity, (.recommendations | join(",")) , .affectedObjects[].sourceName, .affectedObjects[].objectName, .affectedObjects[].objectType, .affectedObjects[].version, .affectedObjects[].buildNumber, .firstObserved, (.kbLinkURLs | join(",")) ] | delpaths([path(.[] | select(.==null))]) | #csv' Demo
Ansible: json filtering
ill have a role playbook which get json from gitlab --- - name: Make an API call uri: method: GET url: "https://gitlab.example.com/api/v4/projects/***/repository/files/Dev/raw?ref=master" headers: PRIVATE-TOKEN: ************** register: json_var - name: show json debug: msg: "{{json_var}}" - name: test debug: var: json_var.json.plannedrelease register: release - name: debug debug: msg: "{{ release }}" but cant get json value to variable, i need only version "1.0" in variable release (from "plannedrelease" : "1.0"), how can i filter it? Playbook output is: PLAY [127.0.0.1] *************************************************************** TASK [Gathering Facts] ********************************************************* ok: [127.0.0.1] TASK [get_contour_version : Make an API call] ********************************** ok: [127.0.0.1] TASK [get_contour_version : show json] ***************************************** ok: [127.0.0.1] => { "msg": { "cache_control": "max-age=0, private, must-revalidate, no-store, no-cache", "changed": false, "connection": "close", "content_disposition": "inline; filename=\"Dev\"; filename*=UTF-8''Dev", "content_length": "402", "content_type": "text/plain; charset=utf-8", "cookies": {}, "cookies_string": "", "date": "Tue, 19 Jan 2021 19:42:33 GMT", "elapsed": 0, "expires": "Fri, 01 Jan 1990 00:00:00 GMT", "failed": false, "json": { "Created": "12/06/2020 10:11", "Key": "123", "Updated": "01/12/2020 11:51", "contour": "Dev", "plannedrelease": "1.0", "… TASK [get_contour_version : test] ********************************************** ok: [127.0.0.1] => { "json_var.json.plannedrelease": "1.0" } TASK [get_contour_version : debug] ********************************************* ok: [127.0.0.1] => { "msg": { "changed": false, "failed": false, "json_var.json.plannedrelease": "1.0" } } PLAY RECAP ********************************************************************* 127.0.0.1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 probably i used wrong method for filtering
Have you tried https://docs.ansible.com/ansible/latest/collections/ansible/builtin/set_fact_module.html ? Reading the documentation the following might work: - name: Save release var set_fact: release: "{{ json_var.json.plannedrelease }}"
Error while trying to install OKD 3.11 in air-gapped or disconnected environment (RHEL 7.4)
As part of a OKD 3.11 setup on on-premise virtual servers running on RHEL-7.4, facing below error there. Necessary rpm packages were installed as part of installation (Ex: Ansible Playbooks). The OKD 3.11 installation files were retrieved from Github repository here at https://github.com/openshift/openshift-ansible/tree/release-3.11 fatal: [dstoicpwkl01v]: FAILED! => { "changed": false, "invocation": { "module_args": { "allow_downgrade": false, "autoremove": false, "bugfix": false, "conf_file": null, "disable_excludes": null, "disable_gpg_check": false, "disable_plugin": [], "disablerepo": [], "download_dir": null, "download_only": false, "enable_plugin": [], "enablerepo": [], "exclude": [], "install_repoquery": true, "install_weak_deps": true, "installroot": "/", "list": null, "lock_timeout": 30, "name": [ "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm" ], "releasever": null, "security": false, "skip_broken": false, "state": "present", "update_cache": false, "update_only": false, "use_backend": "auto", "validate_certs": true } }, "msg": "Failed to connect to dl.fedoraproject.org at port 443: [Errno -2] Name or service not known", "status": -1, "url": "https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm" } PLAY RECAP ************************************************************************************************************************************************************************ dstoicpmal02v : ok=19 changed=0 unreachable=0 failed=1 skipped=40 rescued=0 ignored=0 dstoicpwkl01v : ok=14 changed=0 unreachable=0 failed=1 skipped=32 rescued=0 ignored=0 localhost : ok=11 changed=0 unreachable=0 failed=0 skipped=5 rescued=0 ignored=0 Any inputs to overcome this dependency, appreciate it. Thanks.
You should install epel before deploying OKD 3.11: sudo yum -y install epel-release May be you are behind a firewall, and are not able to reach the site https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
json_query in Ansible filtering output from nvme command returns nothing?
I'm having a hell of a time trying to use json_query to filter the JSON output of some external command (nvme list -o json). The nvme output is captured in Ansible with this: - name: Run nvme command command: cmd: nvme list -o json register: nvme_output become: yes Contains: - name: nvme_ouput stdout debug: var: nvme_output.stdout TASK [general : nvme_ouput stdout] ********************************************* ok: [dev-0600099bfee3c0f60.internal] => { "nvme_output.stdout": { "Devices": [ { "DevicePath": "/dev/nvme0n1", "Firmware": "1.0", "Index": 0, "MaximumLBA": 33554432, "ModelNumber": "Amazon Elastic Block Store", "PhysicalSize": 17179869184, "ProductName": "Non-Volatile memory controller: Vendor 0x1d0f Device 0x8061", "SectorSize": 512, "SerialNumber": "vol0e6af89fd1991cwtf", "UsedBytes": 0 }, { "DevicePath": "/dev/nvme1n1", "Firmware": "1.0", "Index": 1, "MaximumLBA": 104857600, "ModelNumber": "Amazon Elastic Block Store", "PhysicalSize": 53687091200, "ProductName": "Non-Volatile memory controller: Vendor 0x1d0f Device 0x8061", "SectorSize": 512, "SerialNumber": "vol0885625da4a60awtf", "UsedBytes": 0 } ] } } I now need all the DevicePaths. Using the online tester on eg. https://jmespath.org/, I came up with this rather simple query: Devices[*].DevicePath Trying to use it in Ansible: - name: nvme_ouput json_query var debug: msg: "{{ nvme_output.stdout | json_query(jq) }}" vars: jq: >- Devices[*].DevicePath But… TASK [general : nvme_ouput json_query var] ********************* ok: [dev-0600099bfee3c0f60.internal] => { "msg": "" } Why is the output of json_query() empty? I'm running this on AWS EC2 on a Debian 10 system, if it matters, with: ansible-playbook 2.7.7 config file = /home/alex/sysops-ansible/ansible.cfg configured module search path = ['/home/alex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible-playbook python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]
Parsing JSON from Ansible
I'm looking on the web but I'm unable to find a solution, the scenario in quite complex. I have one json array with values like these: [ { "IP1": "1.2.3.4" "IP1_VLAN": "900 - CLOUD-DEV" "IP1_Role": "Management" "IP2": "4.5.6.7" "IP2_VLAN": "901 - CLOUD-DEV" "IP2_Role": "Production" "IP2": "8.9.10.11" "IP2_VLAN": "902 - CLOUD-DEV" "IP2_Role": "Backup" } ] My goal is: "Select the IP with the role of Management and tell me the address", it should select IP*_Role == Management and tell me in this case 1.2.3.4 Do you have any idea how to deal with this?
Following my comment, my 2 cent answer: searching through your actual data structure is going to be hard and ugly. If you have the option to change it a bit, things can get much easier. Here is an example using a list declared in yaml in the following demo playbook. Data is extracted using json_query with the relevent jmespath expression (i.e. the query variable in the debug task) --- - hosts: localhost gather_facts: False vars: my_ips: - ip: "1.2.3.4" vlan: "900 - CLOUD-DEV" role: "Management" - ip: "4.5.6.7" vlan: "901 - CLOUD-DEV" role: "Production" - ip: "8.9.10.11" vlan: "902 - CLOUD-DEV" role: "Backup" tasks: - name: Get ips per role vars: query: >- [?role=='{{ item }}'] debug: msg: "List of ips for role {{ item }}: {{ my_ips | json_query(query) }}" loop: - Management - Production - Backup Which gives PLAY [localhost] ******************************************************************************************************************************************************************************************************************************************************** TASK [Get ips per role] ************************************************************************************************************************************************************************************************************************************************* Wednesday 23 October 2019 16:14:23 +0200 (0:00:00.043) 0:00:00.043 ***** ok: [localhost] => (item=Management) => { "msg": "List of ips for role Management: [{'ip': '1.2.3.4', 'vlan': '900 - CLOUD-DEV', 'role': 'Management'}]" } ok: [localhost] => (item=Production) => { "msg": "List of ips for role Production: [{'ip': '4.5.6.7', 'vlan': '901 - CLOUD-DEV', 'role': 'Production'}]" } ok: [localhost] => (item=Backup) => { "msg": "List of ips for role Backup: [{'ip': '8.9.10.11', 'vlan': '902 - CLOUD-DEV', 'role': 'Backup'}]" } PLAY RECAP ************************************************************************************************************************************************************************************************************************************************************** localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 The equivalent variable declaration in plain json would be: [ { "ip": "1.2.3.4", "vlan": "900 - CLOUD-DEV", "role": "Management" }, { "ip": "4.5.6.7", "vlan": "901 - CLOUD-DEV", "role": "Production" }, { "ip": "8.9.10.11", "vlan": "902 - CLOUD-DEV", "role": "Backup" } ] If you have to load this from an external string (e.g. loading a file...), you can use the from_json filter in your playbook