Ask Bicep Parameters Conditionally - azure

I am creating a an Azure ARM template/Bicep for creating a virtual machine. The template will create both Windows and Linux virtual machines. In the parameters, I want to conditionally ask about the operating system SKU as shown below:
#description('Select the OS type to deploy:')
#allowed([
'Windows'
'Linux'
])
param operatingSystem string
#description('The Ubuntu version (SKU):')
#allowed([
'ubuntu2004'
'ubuntu2004gen2'
])
param ubuntuSKU string
#description('The Desktop Windows version (SKU):')
#allowed([
'win10'
'win11'
])
param windowsSKU string
Currently, the script asks the user to enter both ubuntuSKU and windowsSKU. I would like to show only the parameter which corresponds to the operatingSystem parameter.
For example, if user enters Windows for operatingSystem, the Bicep only asks parameter windowsSKU from user. If user selects Linux for operatingSystem, the Bicep only shows parameter ubuntuSKU.
One workaround is to combine ubuntuSKU and windowsSKU and refactor the rest of my code. However, I prefer to see if there is a better and smarter solution.

There's not dependency/branching in params in the bicep file, there are 2 approaches you can consider though:
combine the options into a single param and use a dictionary lookup in the template, e.g. https://github.com/bmoore-msft/AzureRM-Samples/blob/master/leap-2020/modules/module.ultimate-vm.json#L165-L176 - this works but the few combinations the better.
the other option is to provide the UI for the template and in that ui definition you can set conditions/branching on params. this is pretty involved but works well provided the template will be deployed via the portal... IDK know of an example that does this exact scenario, but this shows some of the capabilities of the overall UI/experience that may help: https://techcommunity.microsoft.com/t5/azure-governance-and-management/using-azure-templatespecs-with-a-custom-ui/ba-p/3586173

Related

Reference group variable in Azure pipeline

I tried to find online, but all the options I tried are not working for me.
I have an Azure pipeline that runs a Python script, and inside of it I need to fetch a group variable value, this variable contains the banckend Url. I tried multiple ways, but still not working. Could anyone shed a light here?
This is the way I am trying to add the value with $(ENVIRONMENT), but I also tried $(env:ENVIRONMENT), $(Environment), $(env:Environment).
Thank you in advace.
On this Azure Pipelines page, you'll see a TAB called Variables. Click in this tab, select Variables Group and then Link Variable Group. In this way, you can select what variable group you'll use.
To use a variable from a variable group, you need to reference a key from the variable group, for example:
Example of variable group Development with the key/values:
- name: URL
value: http://localhost
- name: ENVIRONMENT
value: DEV
In your pipeline:
echo $(URL)
echo $(ENVIRONMENT)
Using the Python task:
import os
URL = os.environ.get('URL')
print (URL)
I recommend you change this pipeline from the Classic mode to Yaml mode; the pipeline will be more modular and reusable.
Microsoft doc: https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops-2022&tabs=yaml

Azure Devops: Using dynamic variable names in customCondition

I have created a pipeline variable dynamically using powershell script with variable name
$ReleaseVariableName = "[$(Release.EnvironmentName)]_should_run_regression"
Now in next Job, I want to add a customCondition to check if value of above variable is "True".
I have tried couple of steps like using join,
eq(join('_', #(variables['Release.EnvironmentName'], 'should_run_regression')), 'True')
but that doesn't work since I cannot declare array using # in custom condition. So is there a way to use dynamic variable names in azure custom conditions?
PS:
I am working with Classic UI so YAML solutions don't work.
The step where I am using this custom condition is agentless job, so I can't use powershell scripts to read dynamic variable and set a Job level variable
I have an alternative of creating different variables for each stage but that looks awful

Terrafom import on windows

i was following this tutorial https://learn.hashicorp.com/tutorials/terraform/state-cli?in=terraform/cli and i came across a problem.
I'm using cmd.exe
i copied&pasted command terraform import aws_security_group.sg_8080 $(terraform output -raw security_group) from article Remove resource from the state, and i got error visible below, What should i do?
The import command expects two arguments.
Usage: terraform [global options] import [options] ADDR ID
Import existing infrastructure into your Terraform state.
This will find and import the specified resource into your Terraform
state, allowing existing infrastructure to come under Terraform
management without having to be initially created by Terraform.
The ADDR specified is the address to import the resource to. Please
see the documentation online for resource addresses. The ID is a
resource-specific ID to identify that resource being imported. Please
reference the documentation for the resource type you're importing to
determine the ID syntax to use. It typically matches directly to the ID
The tutorial you referred to is giving an example command line which relies on some expansion capabilities of typical Unix shells.
One way to achieve a similar result in the Windows Command Prompt (cmd.exe) is to manually run the nested command and then copy-paste its result into the argument of the second command.
For example, first run the command inside the $( ... ) sequence:
C:\example> terraform output -raw security_group
sg-0096a764b1e76f7fd
Here I've assumed that the output would be the same as the ID shown in the example in the tutorial, but of course in your case you will have a different ID with an sg- prefix reflecting the actual security object in your AWS account.
You can then place that security group ID into the outer command line instead of the $( ... ) sequence:
C:\example> terraform import aws_security_group.sg_8080 sg-0096a764b1e76f7fd
This two-step process should reproduce the same effect that a Unix-style shell would've achieved with the command line shown in the tutorial.
I'm not familiar enough with cmd.exe to suggest a direct single-step command similar to the one in the tutorial, and I don't have a Windows system to test on, but there is a question on the SuperUser StackExchange which is asking the same thing you asked in more general terms, agnostic of Terraform; perhaps the answers there will be helpful.

How I can see the azure arm template output? or any functions like concat or resourceid output for debugging purpose

Wondering how to see the result of any ARM function.
for example, concat function used on dependson and it makes the template invalid
It would be great to be able to see how JSON translated from arm functions and parameter before sending to azure
"[resourceId(concat('Microsoft.Compute/virtualMachineScaleSets/', parameters('vmssNamePrefix')),'/extensions/AzureDiskEncryption')]
your snippet should be like this:
"[resourceId('Microsoft.Compute/virtualMachineScaleSets/extensions', parameters('vmssNamePrefix'), 'AzureDiskEncryption')]
there is no debugging mode, best you can do is use outputs section and test your functions there to see what is being generated
Reference: https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#resourceid

Does Get-AzureRmHdInsightJobOutput return an object?

I am currently working on a pre-existing kettle job that calls a powershell script that sends Azure HdInsight a pig job. Get-AzureHdInsightJobOutput is part of this script and is now deprecated. So I am replacing it with Get-AzureRmHdInsightJobOutput. However the new cmdlet has two parameter sets, one for display and one for download. I need an object to be returned in order to avoid making changes to the kettle job.
I'm hoping to find out if the display parameters will return an object or if they just print out the results.
Yes, Get-AzureRmHdInsightJobOutput cmdlet will return string object when in use with the Display parameter set, regardless of the DisplayOutputType.
You can refer to the source code of this cmdlet below in the Azure PowerShell github link:
GetAzureHDInsightJobOutputCommand.cs
Hope this helps!

Resources