currently I want to create simple automation that will tag my Azure resources based on content of json file. To parse json into tags that can be used inside az tag update I'm using jq with command jq -rj '. | to_entries[] |" \"\(.key)\"=\"\(.value)\""'.
This is returning tags in a format that is consumed by AZ CLI i.e:
"primary_owner"="Owner Name" "primary_email"="owner#owner.com" "deplyment_version"="1.0.0" "project_epic"="" "project_feature"="" "project_name"="DIF" "project_billing_name"="DIF" "cost_center"="" "owner_email"="" "incident_management_application_id"="ServiceNow" "environment"="PROD NEW" "opco"="PROD NEW" "department_name"="DATA and ANALYTICS"
Problem that I'm facing right now is that when I copy-paste those tags into a command
az tag update --operation Replace --resource-id /subscriptions/$SUBSCRIPTON/resourceGroups/$RESOURCE --tags "primary_owner"="Owner Name" "primary_email"="owner#owner.com" "deplyment_version"="1.0.0" "project_epic"="" "project_feature"="" "project_name"="DIF" "project_billing_name"="DIF" "cost_center"="" "owner_email"="" "incident_management_application_id"="ServiceNow" "environment"="PROD NEW" "opco"="PROD NEW" "department_name"="DATA and ANALYTICS"
everything is working fine with output looking like
"properties": {
"tags": {
"cost_center": "",
"department_name": "DATA and ANALYTICS",
"deplyment_version": "1.0.0",
"environment": "PROD NEW",
"incident_management_application_id": "ServiceNow",
"opco": "PROD NEW",
"owner_email": "",
"primary_email": "owner#owner.com",
"primary_owner": "Owner Name",
"project_billing_name": "DIF",
"project_epic": "",
"project_feature": "",
"project_name": "DIF"
}
However whenever I try to get them using any command i.e
az tag update --operation Replace --resource-id /subscriptions/$SUBSCRIPTON/resourceGroups/$RESOURCE --tags $(jq -rj '. | to_entries[] |" \"\(.key)\"=\"\(.value)\""'`)
or by running
jq -rj '. | to_entries[] |" \"\(.key)\"=\"\(.value)\""'` > file
az tag update --operation Replace --resource-id /subscriptions/$SUBSCRIPTON/resourceGroups/$RESOURCE --tags $(less file)
my command is completely messed up and shows every escaped " character and output looks like
"properties": {
"tags": {
"\"cost_center\"": "\"\"",
"\"department_name\"": "\"DATA",
"\"deplyment_version\"": "\"1.0.0\"",
"\"environment\"": "\"PROD",
"\"incident_management_application_id\"": "\"ServiceNow\"",
"\"opco\"": "\"PROD",
"\"owner_email\"": "\"\"",
"\"primary_email\"": "\"owner#owner.com\"",
"\"primary_owner\"": "\"Owner",
"\"project_billing_name\"": "\"DIF\"",
"\"project_epic\"": "\"\"",
"\"project_feature\"": "\"\"",
"\"project_name\"": "\"DIF\"",
"ANALYTICS\"": "",
"NEW\"": "",
"Name\"": "",
"and": ""
}
Any idea what am I doing wrong?
tag my Azure resources based on content of json file.
I have reproduced in my environment and got expected results as below and I followed Microsoft-Document:
$res= Get-Content "Path"
$answer=az tag update --operation Replace --resource-id "XX" --tags $res
$answer | ft
Path- Path of the file or files Location
XX- Resource Id
y.json:
"primary_email"= "owner#owner.com"
"primary_owner"= "Owner-Name"
"project_billing_name"= "DIF"
"project_epic"= " "
"project_feature"= " "
"project_name"= "DIF"
Output:
In Azure Portal(Just to check) Output:
Related
I trying to run below PowerShell script to add azure data factory data sets. But im getting motioned error.
Json File
{
"name": "DSNAME",
"properties": {
"linkedServiceName": {
"referenceName": "REGNAME1",
"type": "LinkedServiceReference"
},
"annotations": [],
"type": "AzureDataExplorerTable",
"schema": [],
"typeProperties": {
"table": "TABLE_TEST"
}
},
"type": "Microsoft.DataFactory/factories/datasets"
}
Powershell
az config set extension.use_dynamic_install=yes_without_prompt
Get-ChildItem "ADF_DATASETS/" -Filter *.json |
Foreach-Object {
$content = Get-Content $_.FullName
az datafactory dataset create --properties $content --name "DATASETNAME" --factory-name "ADFNAME" --resource-group "RG_TEST"
}
Error:
Error detail: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Please provide a valid JSON file path or JSON string.
The provided JSON string may have been parsed by the shell. See https://docs.microsoft.com/cli/azure/use-cli-effectively#use-quotation-marks-in-arguments
ERROR: Failed to parse string as JSON:
What kind of text --properties expect?
It is expecting JSON string in double quotation marks. So you have to put the value of $content inside the double quotation mark.
az datafactory dataset create --properties "{\"type\":\"AzureBlob\",\"linkedServiceName\":{\"type\":\"LinkedServiceReference\",\"referenceName\":\"exampleLinkedService\"},\"parameters\":{\"MyFileName\":{\"type\":\"String\"},\"MyFolderPath\":{\"type\":\"String\"}},\"typeProperties\":{\"format\":{\"type\":\"TextFormat\"},\"fileName\":{\"type\":\"Expression\",\"value\":\"#dataset().MyFileName\"},\"folderPath\":{\"type\":\"Expression\",\"value\":\"#dataset().MyFolderPath\"}}}" --name "exampleDataset" --factory-name "exampleFactoryName" --resource-group "exampleResourceGroup"
I've got an old azure subscription with an ampersand (&) in the name.
I can't switch to it with az account set -s "Salt & Pepper" and have been passing the subscription ID instead of the name. The usual escape methods produce the same result as the original name:
$ az account list
[
{
"id": "foo-000-0000",
"isDefault": true,
"name": "Salt & Pepper",
"state": "Enabled",
...
}
]
$ az account set -s "Salt & Pepper"
CommandNotFoundError: 'Salt & Pepper' is misspelled or not recognized by the system.
Try this: 'az account list'
How can I form the -s argument such that it can recognize my cluster name?
I'm trying to create a VM via the CLI and add an artifact from the public repo in a dev test lab.
Here is my script:
az lab vm create --resource-group TestRG --lab-name TestLab --name 'ScriptVM' --image "Windows 10 Pro, Version 1809" --image-type gallery --size 'Standard_B2s' --admin-username 'user' --admin-password 'pw' --artifacts '{ "/artifactsources/publicrepo/artifacts/linux-java" }'
I don't know the format of the artifacts switch though as it just says a JSON encoded array. How does this work? I just get the error
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
So the answer was I didn't have all the required elements in the JSON. I also fell foul of escaping quotation marks in the PS script. The final switch ended up looking like this:--artifacts '[{"""artifactId""": """artifacts/windows-notepadplusplus"""}]'
The following example shows the sections that make up the basic structure of a definition file:
{
"$schema": "https://raw.githubusercontent.com/Azure/azure-devtestlab/master/schemas/2016-11-28/dtlArtifacts.json",
"title": "",
"description": "",
"iconUri": "",
"targetOsType": "",
"parameters": {
"<parameterName>": {
"type": "",
"displayName": "",
"description": ""
}
},
"runCommand": {
"commandToExecute": ""
}
}
It resides in your Git repo and is referenced in the AZ command. A video of the entire process can be found in our documentation.
https://learn.microsoft.com/en-us/azure/lab-services/devtest-lab-artifact-author
I'm looking to store my connection string in the following variable:
connectionString= az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv
When I execute the above, I get the full connection string in the response. However, when I input:
echo $connectionString
...I get a blank response. The variable is not being stored. Any recommendations on what else to try?
You could use command substitution in order to capture the output in a variable:
connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)
If you need to preserve output across multiple lines, i.E. when the Azure CLI returns values in JSON format, you may want to use a slightly different format for the output to stdout.
Consider this example:
varResourceGroup=$(az group show -n $resourceGroup)
Using the same command as in your example for the output to stdout would result in a single line:
echo $varResourceGroup
{ "id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>", "location": "westeurope", "managedBy": null, "name": "<resourceGroup_name>", "properties": { "provisioningState": "Succeeded" }, "tags": null }
If you use a sightly different format, the line breaks are preserved:
echo "$varResourceGroup"
{
"id": "/subscriptions/<subscription_id>/resourceGroups/<resourceGroup_name>",
"location": "westeurope",
"managedBy": null,
"name": "<resourceGroup_name>",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null
}
As Holger said, we can use this script to define the variable:
connectionString=$(az storage account show-connection-string -n $storageAccount -g $resourceGroup --query connectionString -o tsv)
Also, we can use this way to define this variable, like this:
[root#jasoncli#jasonye ~]# connectionstring=`az storage account show-connection-string -n jasondisk3 -g jasonauto --query connectionString -o tsv`
[root#jasoncli#jasonye ~]# echo $connectionstring
DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;AccountName=jasondisk3;AccountKey=m+kQwLuQZiI3LMoMTyAI8KxxxxD+ZaT9HUL3Agxxxxqul4s8fAIHGPMTD/AG2j+TPHBpttq5hXRmTaQ==
Hope this helps.
I'm new but trying to get a new script running but I need it to call on todays date as a variable within the configuration file so the program can be run.
I'm sure sure the best way to implement it so far this line will replace the correct part of the configuration file I need but I can't figure out how to get it to use the "todays date" e.g. date +%F command.
sed -i 's/"to_date":.*/"to_date":"date +%F"/' /config/settings
config following:
{
"username":"admin",
"password":"redhat",
"assumeyes":true,
"to_date": "2011-10-01",
"skip_depsolve":false,
"skip_errata_depsolve":false,
"security_only":false,
"use_update_date":false,
"no_errata_sync":false,
"dry_run":false,
"errata": ["RHSA-2014:0043", "RHBA-2014:0085"],
"blacklist": {
},
"removelist": {
},
"channels":[
{
"rhel-x86_64-server-5": {
"label": "my-rhel5-x86_64-clone",
"existing-parent-do-not-modify": true
},
"rhn-tools-rhel-x86_64-server-5": {
"label": "my-tools-5-x86_64-clone",
"name": "My Clone's Name",
"summary": "This is my channel's summary",
"description": "This is my channel's description"
}
},
{
"rhel-i386-server-5": "my-rhel5-i386-clone"
}
]
}
Using a proper JSON parser jq with the --arg field to pass the current date,
jq --arg inputDate $(date +%F) '.to_date = $inputDate' /config/settings
{
"username": "admin",
"password": "redhat",
"assumeyes": true,
"to_date": "2017-01-27",
"skip_depsolve": false,
"skip_errata_depsolve": false,
"security_only": false,
"use_update_date": false,
"no_errata_sync": false,
"dry_run": false,
"errata": [
"RHSA-2014:0043",
"RHBA-2014:0085"
],
"blacklist": {},
"removelist": {},
"channels": [
{
"rhel-x86_64-server-5": {
"label": "my-rhel5-x86_64-clone",
"existing-parent-do-not-modify": true
},
"rhn-tools-rhel-x86_64-server-5": {
"label": "my-tools-5-x86_64-clone",
"name": "My Clone's Name",
"summary": "This is my channel's summary",
"description": "This is my channel's description"
}
},
{
"rhel-i386-server-5": "my-rhel5-i386-clone"
}
]
}
The jq download and usage instructions are pretty straight forward. Recommend using it for manipulating JSON, instead of depending upon regex.
jq does not edit the file in-place, save it to a temporary file and rename it back, using GNU mktemp
jsonTemp=$(mktemp)
jq --arg inputDate $(date +%F) '.to_date = $inputDate' /config/settings > "$jsonTemp"
mv "$jsonTemp" /config/settings
To include the output of a command inside some quoted text, you have to use a subshell and use double-quotes so the text will get expanded :
sed -i "s/\"to_date\":.*/\"to_date\":\"$(date +%F)\"/" /config/settings
Also I second Inian's comment : you should be using jq to manipulate JSON data.
For example, the following command should do the modification you need :
jq ".toDate = $(date +%F)" /config/settings