Azure CLI: Troubleshooting Issues with Variables in Commands – Unexpected Behavior

86 Views Asked by At

I'm trying to extract information from a text file and store the content in a variable using the Azure CLI command below.

file_content=$(cat /home/diwakar/cli.txt)
echo $file_content

After running the above command, the following output was obtained (Same content I have cli.txt file):

Output:

Audience={"Users":["user1","user2","user4","user3"],"Groups":[],"DefaultRolloutPercentage":50}

When I run the actual command, the variable takes on a different value, leading to an error.

az appconfig feature filter update --connection-string $endpoint --feature $featurename --filter-name Microsoft.Targeting --filter-parameters $file_content --yes

Below is the error message:

Filter parameter value must be a JSON escaped string. "{"Users":["user1","user2","user4","user3"],"Groups":[],"DefaultRolloutPercentage":50}" is not a valid JSON object.

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

Filter parameter value must be a JSON escaped string. "{"Users":["user1","user2","user4","user3"],"Groups":[],"DefaultRolloutPercentage":50}" is not a valid JSON object.

The above error occurred due to the value of the $file_content variable is not a valid JSON object. This is because the value of the $file_content variable is being interpreted as a string literal, rather than a JSON object.

In my environment, I set the cli.txt as below:

{"Users":["user1","user2","user4","user3"],"Groups":[],"DefaultRolloutPercentage":50}

enter image description here

Using the below command I can able to update the az appconfig feature filter from the text file.

Command and output:

vexxxre:~$ file_content1=$(cat cli.txt)
venkxxxe:~$ echo "$file_content1"
{"Users":["user1","user2","user4","user3"],"Groups":[],"DefaultRolloutPercentage":50}
venkxxxx:~$ az appconfig feature filter update --connection-string <your-connection-string> --feature <Your-feature-name> --filter-name Microsoft.Targeting --filter-parameters Audience="$file_content1" --yes
This command is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
{
  "name": "Microsoft.Targeting",
  "parameters": {
    "Audience": {
      "DefaultRolloutPercentage": 50,
      "Groups": [],
      "Users": [
        "user1",
        "user2",
        "user4",
        "user3"
      ]
    }
  }
}

enter image description here

Portal:

enter image description here

Reference:

az appconfig feature filter | Microsoft Learn