How can we run multiple CLI command in Azure Bash together? - azure

I have Multiple Resources to deploy in my Environment using CLI command all together. For Example: I need to create VM, Web App, Redis Cache, etc using a single script.
Instead of creating individual resources can we create all together.

One way is to put all your commands in a .sh shell script. Below is an example of how you can do this:
#!/bin/bash
az group create -n MyResourceGroup -l centralus
az vm create -n MyVm -g MyResourceGroup --image UbuntuLTS
az webapp create -g MyResourceGroup -p MyPlan -n MyUniqueAppName
# More Azure CLI commands to create resources
Then you can run your script inside your local environment bash shell or Azure Cloud Shell like this:
username#Azure:~$ ./script.sh
Another way is to use an ARM template to deploy your resources.

Related

Azure VM run-command fails with piped commands

I am trying to install Azure CLI on an ubuntu VM running in Azure.
I am using the run-command cli operation to execute the command
az vm run-command invoke --command-id RunShellScript --name trainingVM-1 --resource-group azure-privsec --scripts "curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash"
When running a non-piped command, the command works fine but when I run the above piped command to install azure-cli, it fails with the error:
Could not retrieve credential from local cache for service principal 8bf34d22-7230-47e7-907e-e0df201xxxxx. Please run 'az login' for this service principal.
One of the reasons could be that I am using an old version of Azure cli(2.29.2). For now, I dont have the leverage to upgrade the azure-cli version.
What can be the best way to install azure-cli on my VM using the run-command cli call.
Edit:
Splitting into two commands like this works for me but I am still trying to see if we can consolidate it into a single command:
az vm run-command invoke --command-id RunShellScript --name trainingVM-1 --resource-group azure-privsec --scripts "wget -O /tmp/file.sh https://aka.ms/InstallAzureCLIDeb"
az vm run-command invoke --command-id RunShellScript --name trainingVM-1 --resource-group azure-privsec --scripts "sudo bash /tmp/file.sh"
Tested in Azure PowerShell and Azure DevOps pipeline, your first Azure VM run-command run successfully.
Use Azure PowerShell, I can run this command directly without any issue.
Use Azure CLI task in the DevOps pipeline and use a Microsoft-hosted agent.
Please ensure that the service principal corresponding to the Azure Resource Manager connection(Service Connection) has sufficient permission for your Azure VM, such as owner or contributor permissions.
Then I can see the command run successfully.
There are some docs for your reference:
Manage service connections
Connect to Microsoft Azure

Read entire file from Azure VM, via Azure CLI

I'm trying to retrieve a docker log for an application, from an Azure Devops Pipeline. In a pipeline step, I can run a bash script on the VM that's hosting the application, using the Azure CLI like this. This reads the docker log from the VM hosting the application:
az vmss run-command invoke --command-id RunShellScript --instance-id 0 -n aks-myservicename-12345678-vmss -g my-resource-group --scripts "docker logs [container_id]" > testing.txt
The problem is that the output of az vmss run-command invoke is limited to a max 4096 bytes - see https://learn.microsoft.com/en-us/azure/virtual-machines/linux/run-command#restrictions
Is there any way I can read the entire docker log?
The following reference documents an --output-blob-uri parameter that can be used with az vm run-command create. I'm wondering if that gives a way to output the docker log to a file that I can then access by using another Azure CLI step. I haven't found any documentation though on how this works https://learn.microsoft.com/en-us/cli/azure/vmss/run-command?view=azure-cli-latest

Azure CLI: Unable to escape pipe character (|) in Windows PowerShell

Scenario
I try to create an Azure web app with Azure CLI on my windows machine. Unfortunately, I am not able to choose runtime for my webapp. When I try: az webapp create -n name -g grop -p plan -r "DOTNETCORE|3.1", I am getting an error:
'3.1' is not recognized as an internal or external command,
operable program or batch file.
I was trying to escape pipe with backslash \ but it does not help
You need to enclose within quotes, try this
az webapp create --name somename -g grop --plan plan --runtime='"DOTNETCORE|3.1"'
Also replacing pipe | with colon : helps:
az webapp create --name somename -g grop --plan plan --runtime="DOTNETCORE:3.1"

Custom Data with Azure Windows VM run PowerSell Script

I am trying to download and install an exe during the provisioning of a Windows VM in Azure cloud. I do not want to use Custom Script Extension but instead I want to use "Custom Data". I cannot find any solid examples on Azure documentation.
In AWS, I found enough resources and I could develop the below PowerShell script and add it to the User Data but that doesn't work on Azure, I tried different variations but with no luck. Has anyone done that before? Is there any clear documentation on that? I read Azure uses Cloud-init but again, no clear examples on how to do that with Cloud-init for a Windows machine, all examples are for Linux.
<powershell>
start-transcript
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest 'https://www.domain-name.com/filename.exe' -OutFile C:\filename.exe
C:\filename.exe --quiet
</powershell>
Any help would be appreciated.
You can inject data into a Windows virtual machine on Azure, but you can't execute it using custom data or cloud init unfrotunately. Execution of the custom data using cloud init is only supported in Ubuntu images.
Source: https://azure.microsoft.com/es-es/blog/custom-data-and-cloud-init-on-windows-azure/
To achieve an execution of a script post provisioning, it depends on how you're provisioning the VM.
In ARM templates you can use custom script extensions: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
Using Azure CLI you can execute a script using az vm run-command like this:
az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group \
--scripts 'param([string]$arg1)' \
'Write-Host Hello' \
--parameters 'arg1=kemety'
Sourced from here: https://learn.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest

az container exec when using Docker compose

Is it possible to execute command in a container which is running under Azure WebApp service by Docker Compose?
When I create single container by az container create ..., then it works.
But when I create set of containers by Docker compose script using az webapp create --multicontainer-config-type compose ..., then it does not work.
From logs I see that there is running container myWebApp_myContainer_1 so I try:
az container exec -g myResourceGroup -n myWebApp_myContainer_1 --exec-command "/bin/bash"
With this result:
The Resource 'Microsoft.ContainerInstance/containerGroups/myWebApp_myContainer_1'
under resource group 'myResourceGroup' was not found.
Then I try:
az container exec -g myResourceGroup -n myWebApp --container-name myWebApp_myContainer_1 --exec-command "/bin/bash"
With this result:
The Resource 'Microsoft.ContainerInstance/containerGroups/myWebApp' under resource group 'myResourceGroup' was not found.
Note that it is normally possible to execute commands in containers started by Docker compose script on local Docker (out of Azure).
Update I don't like to install SSH server into Docker images. It is a bad approach. I'm looking for a way of direct exec like az container exec does.
Thank you for any hint.
For your issue, when the web app created from a Docker image, it's just a web app, not a container. So you cannot use the command az container exec to connect to it.
If you really want to connect to the web app that created from a Docker image, there are two ways as I know to achieve it.
The one is that I describe in the comment, you should install the OpenSSH server in the Docker image. Then ssh into the web app from the port exposed to the Internet.
The other one as you wish is using the command az webapp remote-connection. For more details, you can read Open SSH session from remote shell.

Resources