I would like to udpate a file config.yaml file by inserting some configuration parameters via bash.
The file to be updated looks like:
{
"log": [
{
"format": "plain",
"level": "info",
"output": "stderr"
}
],
"p2p": {
"topics_of_interest": {
"blocks": "normal",
"messages": "low"
},
"trusted_peers": [
{
"address": "/ip4/13.230.137.72/tcp/3000",
"id": "fe3332044877b2034c8632a08f08ee47f3fbea6c64165b3b"
}
]
},
"rest": {
"listen": "127.0.0.1:3100"
}
}
And it needs to look like:
{
"log": [
{
"format": "plain",
"level": "info",
"output": "stderr"
}
],
"storage": "./storage",
"p2p": {
"listen_address":"/ip4/0.0.0.0/tcp/3000",
"public_address":"/ip4/0.0.0.0/tcp/3000",
"topics_of_interest": {
"blocks": "normal",
"messages": "low"
},
"trusted_peers": [
{
"address": "/ip4/13.230.137.72/tcp/3000",
"id": "fe3332044877b2034c8632a08f08ee47f3fbea6c64165b3b"
}
]
},
"rest": {
"listen": "127.0.0.1:3100"
}
}
so adding
on the first level "storage": "./storage",
and on the second level in the p2p section "listen_address":"/ip4/0.0.0.0/tcp/3000", and "public_address":"/ip4/0.0.0.0/tcp/3000",
How do I do this with sed?
For YAML to JSON editor checkout---YAML to JSON editor
If you are certain that your YAML file is written in the JSON subset of YAML, you can use jq:
jq --arg a "/ip4/0.0.0.0/tcp/3000" \
'.storage = "./storage" |
.php += {listen_address: $a, public_address: $a}' config.yaml > tmp &&
mv tmp config.yaml
Related
using jq i'm trying to add data to a specific element in my json below :
{
"users": [
{
"username": "karim",
"queue": [
"default"
]
},
{
"username": "admin",
"queue": [
"apps",
"prod"
]
}
]
}
what i want to do is to add items in queue[] of user admin like this
{
"users": [
{
"username": "hive",
"queue": [
"default"
]
},
{
"username": "admin",
"queue": [
"apps",
"prod",
"dev"
]
}
]
}
This is the command i used
jq '.users[] | select(.username == "admin").queue += ["dev"]' file.json
But the result is not as expected
{
"username": "hive",
"queue": [
"default"
]
}
{
"username": "admin",
"queue": [
"apps",
"prod",
"dev"
]
}
Why users array doesn't appear ? I need to keep it in the result
With the pipe you are changing the context down to an array element, which is what you want for the selection. If you put parentheses around the pipe and the selection, you will keep the assignment and thus the filter's output on top-level:
jq '(.users[] | select(.username == "admin")).queue += ["dev"]'
{
"users": [
{
"username": "karim",
"queue": [
"default"
]
},
{
"username": "admin",
"queue": [
"apps",
"prod",
"dev"
]
}
]
}
Demo
I am calling API and getting below output but from the output and i want to find the key based on value input and my input value is "vpc-tz" how to achieve this in ansible using json_query?
{
"json": {
"allScopes": [
{
"
"clusters": {
"clusters": [
{
"cluster": {
"clientHandle": "",
"type": {
"name": "ClusterComputeResource"
},
"universalRevision": 0,
"vsmUuid": "423B1819-9495-4F10-A96A-6D8284E51B29"
}
}
]
},
"controlPlaneMode": "UNICAST_MODE",
"description": "",
"extendedAttributes": [
],
"id": "vdnscope-6",
"isTemporal": false,
"isUniversal": false,
"name": "vpc-tz",
"nodeId": "85e0073d-0e5a-4f04-889b-42df771aebf8",
"objectId": "vdnscope-6",
"objectTypeName": "VdnScope",
"revision": 0,
"type": {
"name": "VdnScope"
},
"universalRevision": 0,
"virtualWireCount": 0,
"vsmUuid": "423B1819-9495-4F10-A96A-6D8284E51B29"
},
]
}
}
Here is a query which works:
json.allScopes[?name=='vpc-tz'].name
I have json code and need to filter it by the value of the attribute DNSName. The filter must be case insensitive.
How can I do that? Is there a possibility to solve it with jq?
This is how I create the json code:
aws elbv2 describe-load-balancers --region=us-west-2 | jq
My unfiltered source json code looks like this:
{
"LoadBalancers": [
{
"IpAddressType": "ipv4",
"VpcId": "vpc-abcdabcd",
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:000000000000:loadbalancer/app/MY-LB1/a00000000000000a",
"State": {
"Code": "active"
},
"DNSName": "MY-LB1-123454321.us-west-2.elb.amazonaws.com",
"SecurityGroups": [
"sg-00100100",
"sg-01001000",
"sg-10010001"
],
"LoadBalancerName": "MY-LB1",
"CreatedTime": "2018-01-01T00:00:00.000Z",
"Scheme": "internet-facing",
"Type": "application",
"CanonicalHostedZoneId": "ZZZZZZZZZZZZZ",
"AvailabilityZones": [
{
"SubnetId": "subnet-17171717",
"ZoneName": "us-west-2a"
},
{
"SubnetId": "subnet-27272727",
"ZoneName": "us-west-2c"
},
{
"SubnetId": "subnet-37373737",
"ZoneName": "us-west-2b"
}
]
},
{
"IpAddressType": "ipv4",
"VpcId": "vpc-abcdabcd",
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:000000000000:loadbalancer/app/MY-LB2/b00000000000000b",
"State": {
"Code": "active"
},
"DNSName": "MY-LB2-9876556789.us-west-2.elb.amazonaws.com",
"SecurityGroups": [
"sg-88818881"
],
"LoadBalancerName": "MY-LB2",
"CreatedTime": "2018-01-01T00:00:00.000Z",
"Scheme": "internet-facing",
"Type": "application",
"CanonicalHostedZoneId": "ZZZZZZZZZZZZZ",
"AvailabilityZones": [
{
"SubnetId": "subnet-54545454",
"ZoneName": "us-west-2a"
},
{
"SubnetId": "subnet-64646464",
"ZoneName": "us-west-2c"
},
{
"SubnetId": "subnet-74747474",
"ZoneName": "us-west-2b"
}
]
}
]
}
I now want some bash code to filter this result for the record with the DNSName property value MY-LB2-9876556789.us-west-2.elb.amazonaws.com, and need the entire LoadBalancer object back as a result. This is how I wish my result to look like:
{
"IpAddressType": "ipv4",
"VpcId": "vpc-abcdabcd",
"LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:000000000000:loadbalancer/app/MY-LB2/b00000000000000b",
"State": {
"Code": "active"
},
"DNSName": "MY-LB2-9876556789.us-west-2.elb.amazonaws.com",
"SecurityGroups": [
"sg-88818881"
],
"LoadBalancerName": "MY-LB2",
"CreatedTime": "2018-01-01T00:00:00.000Z",
"Scheme": "internet-facing",
"Type": "application",
"CanonicalHostedZoneId": "ZZZZZZZZZZZZZ",
"AvailabilityZones": [
{
"SubnetId": "subnet-54545454",
"ZoneName": "us-west-2a"
},
{
"SubnetId": "subnet-64646464",
"ZoneName": "us-west-2c"
},
{
"SubnetId": "subnet-74747474",
"ZoneName": "us-west-2b"
}
]
}
Does anyone know how to do it?
Update:
This solution works, but is not case insensitive:
aws elbv2 describe-load-balancers --region=us-west-2 | jq -c '.LoadBalancers[] | select(.DNSName | contains("MY-LB2"))'
Update:
This solution seems to work even better:
aws elbv2 describe-load-balancers --region=us-west-2 | jq -c '.LoadBalancers[] | select(.DNSName | match("my-lb2";"i"))'
But I did not have the chance to test in detail yet.
You probably should be using test/2 rather than match/2, but in either case, since the problem description calls for
case-insensitive equality, you would use an anchored regex:
.LoadBalancers[]
| select(.DNSName | test("^my-lb2-9876556789.us-west-2.elb.amazonaws.com$";"i"))
With the caveat that ascii_upcase only translates ASCII characters, it might be more efficient to use it:
.LoadBalancers[]
| select(.DNSName | ascii_upcase == "MY-LB2-9876556789.US-WEST-2.ELB.AMAZONAWS.COM")
I am trying to mount a data disk to a Windows Vm on Azure through an ARM template which is creating the VM. This is my ARM resource
{
"name": "[parameters('virtualMachineName')]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2016-04-30-preview",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
],
"tags": {
"Busuness Group": "[parameters('busunessGroup')]",
"Role": "[parameters('role')]"
},
"properties": {
"osProfile": {
"computerName": "[parameters('virtualMachineName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"provisionVmAgent": "true"
}
},
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"imageReference": {
"publisher": "microsoft-ads",
"offer": "standard-data-science-vm",
"sku": "standard-data-science-vm",
"version": "latest"
},
"dataDisks": [
{
"lun": 0,
"createOption": "Empty",
"caching": "None",
"managedDisk": {
"storageAccountType": "Premium_LRS"
},
"diskSizeGB": "[parameters('dataDiskSizeGB')]",
}
]
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
}
]
}
},
"plan": {
"name": "standard-data-science-vm",
"publisher": "microsoft-ads",
"product": "standard-data-science-vm"
},
"resources": [
{
"type": "extensions",
"name": "CustomScriptExtension",
"apiVersion": "2015-06-15",
"location": "[resourceGroup().location]",
"dependsOn": [
"[parameters('virtualMachineName')]"
],
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": ["https://paste.fedoraproject.org/paste/FMoOq4E3sKoQzqB5Di0DcV5M1UNdIGYhyRLivL9gydE=/raw"]
}
}
}
]
}
It failed with following error
{
"code": "VMExtensionProvisioningError",
"message": "VM has reported a failure when processing extension 'CustomScriptExtension'. Error message: \"Invalid Configuration - CommandToExecute is not specified in the configuration; it must be specified in either the protected or public configuration section\"."
}
I also tried passing the command directly
"settings": {
"commandToExecute": "Get-Disk |Where partitionstyle -eq ‘raw’ | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel “Data” -Confirm:$false"
}
both didn't work. what I am doing wrong here ?
So, you need to explicitly call powershell to use powershell, just like in the examples:
"commandToExecute": "[concat('powershell -command ', variable('command'))]"
you can attempt to paste your command directly, but due to all the quotes it won't parse properly, so save your command as a variable and concat like that.
I'm following the tutorial "Extract Data from a Source File." I'm able to upload a file as described, but when I try to convert the file to SVF it fails with "TranslationWorker-InternalFailure." I get the same error with both a 3ds file created with Blender and an f3d created with Autodesk Fusion 360.
Here's my code (python):
r=requests.post(
'https://developer.api.autodesk.com/modelderivative/v2/designdata/job',
headers={
'authorization':'{0} {1}'.format(auth_type,auth_token)
},
json={
"input": {
"urn": urn_cube_base64,
},
"output": {
"formats": [
{
"type": "svf",
"views": [
"2d",
"3d"
]
}
]
}
}
)
print r.json()
while True:
r=requests.get('https://developer.api.autodesk.com/modelderivative/v2/designdata/{0}/manifest'.format(urn_cube_base64),
headers={
'authorization':'{0} {1}'.format(auth_type,auth_token)
},
)
print r.text
rj=r.json()
progress=rj['progress']
status=rj['status']
print rj['status'],progress
if status in ('success','failed','timeout'):
print json.dumps(rj,indent=2)
break
It produces the following output:
{
"hasThumbnail": "false",
"status": "failed",
"derivatives": [
{
"hasThumbnail": "false",
"status": "failed",
"name": "LMV Bubble",
"messages": [
{
"message": "Extractor error code -1073741819",
"code": "TranslationWorker-InternalFailure",
"type": "error"
}
],
"outputType": "svf",
"progress": "complete"
}
],
"region": "US",
"version": "1.0",
"progress": "complete",
"type": "manifest",
"urn": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6dG1wX2J1Y2tldDEvY3ViZS4zZHM"
}