Running Azure runbook from Powershell script - azure

I have an app running on a local machine that I want to be able to run a powershell script to turn on my Azure Vm's. I have a runbook in my subscription to turn them on and have the functionality to run the runbook. from the Powershell script. My only problem right now is authenticating to my Azure subscription. I have a Automation account and the connection and certificate that comes with it but I'm a bit confused as to whether or not I can use this to perform this functionality.

you could use service principal auth, or certificate auth (well, azure ad user auth as well, but somehow microsoft thinks its not safe enough).
Basically go through this link: https://blogs.msdn.microsoft.com/benjaminperkins/2017/01/20/execute-an-azure-powershell-arm-script-without-prompting-for-credentials
ps. one fine option for locally running script: Enable-AzureRmContextAutosave, which would allow you to auth once and after that just reuse that auth on you local powershell

Related

Possible to connect local PowerShell to Azure Runbooks?

I'm looking for a way to use my machine-local PowerShell ISE or VSCode to connect to Azure Runbooks. Usually, one has to scaffold locally and then test in the cloud in an Azure runbook. Has anybody ever done this?
One way I'm looking into it is this idea but I was wondering if you knew of something more convenient.
To connect from local (VScode) to Azure Runbooks:
Thanking #Kaido Järvemets for detailed steps. I've tried by considering few steps from there and was able to connect successfully.
Check for Azure Automation extension in Visual Studio code.
To work with Azure automation from local, we need to set up a few settings by searching "Preferences: User settings" (ctrl+shift+p).
Register a new application under Azure Active Directory and add the respective "client_id, client_secret, automation_account_name, subscription_id,tenant_id" in extension settings of Azure Automation configuration.
Give the required permissions in Automation Account -> Access Control (IAM) as shown here:
Created a PowerShell runbook script to run connect-AzAccount under Azure Automation Account through portal.
Opened the same runbook in Azure Automation and was able to connect from local (vscode)to Azure.
Note: Search for Automation explorer (ctrl+shift+p) and create a new runbook if needed. you can run the same script in Azure portal also & it will work as described above.

How to log in to Azure using az cli from a Gitlab CI runner?

I have generated a zip file of a Node.JS-based web app in Gitlab, and I am trying to deploy it as an Azure "web app" using az webapp deploy. This works fine on my local machine where I am logged in, but I can't for the life of me figure out how I can log in to Azure from the Gitlab runner, so that I can run that same command. I've tried:
Using the Publish Profile (already need to be logged in for that!)
Creating a managed identity with roles on the app (but I don't have access to AD)
Creating the managed system identity in the app's "Identity" pane (can't find any associated password?!)
Generating a JWT token to store in Gitlab as described in this question (I don't have access to the App Registrations functionality)
I don't want to use Azure to rebuild the application using the webhook system, I already have a known-working ZIP package that I want to deploy. My only hangup is logging in.
How can I log in to Azure -- i.e. what incantations do I have to provide to az login -- from a Gitlab CI runner, in order to deploy my website from a zipped Gitlab artifact to the App Service?
(note: I am a teacher and trying to figure this out for my students; it is possible that I am working with a somehow-limited Azure but my local IT doesn't support us for this and of course neither will Microsoft.)
If you cannot access the app registrations as discussed in a similar question and have no federation configured, your only options are to use a username and password (e.g. a user's username and password to authenticate to AAD), use a device code flow, or self-host your GitLab runner on Azure with a managed identity.
Using username and password
To use username is password is straightforward:
az login --tenant $YOUR_TENANT_ID -u $YOUR_USERNAME -p $YOUR_PASSWORD
However, this may not be possible if you normally do not login to Azure using a username and password (for example, you use OAuth or other federated login for the Azure portal and users have no passwords set). In which case, you will need to use the device code flow.
Using device code
To use device code flow, you will need to monitor the job output, copy the URL shown, and login from your browser every time your job runs. In your job, add the following:
az login --tenant $YOUR_TENANT_ID --use-device-code
In the job output you will see a message similar to the following:
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code YOUR-CODE-WILL-APPEAR-HERE to authenticate
Copy the code from the message, open your browser to the device login page and enter the code to allow your job to proceed.
Note: It is possible for organizations to disable this login method, in which case you will see an error when trying to login this way.
Self-hosting GitLab runner on Azure with a managed identity
Lastly, if you're not able to use any of the above methods, you can deploy the GitLab runner to Azure itself as an application that uses a managed identity (for example on AKS, ACI, or on a VM with a managed identity).
For example, you can configure a shell runner on an Azure VM. Azure VMs with a managed identity will not require az login to perform az cli commands.
Creating the managed system identity in the app's "Identity" pane (can't find any associated password?!)
The reason you can't find any associated password is because managed identities can only be leveraged from Azure services -- for example, Azure VMs using a managed identity are able to use az cli without logging in.

Azure Automation: How to start Azure Runbook from Hybrid Runbook - without another authentication?

I have a working Azure Automation Hybrid Runbook Worker that is successfully executing runbooks. From one such Hybrid runbook I need to pass a result on to another runbook that must run on Azure, not on the Hybrid Worker.
After doing some research it seems that I have to use the Start-AzAutomationRunbook cmdlet to start the Azure runbook - which requires a Connect-AzAccount before, which means additional authentication:
To use Start-AzAutomationRunbook, the script must authenticate to your
Azure subscription.
But this seems overly complicated. Is there a less complex way to chain Azure and Hybrid runbooks? Without having to explicitly authenticate again? Overall, the runbooks are located in the same Automation account right next to each other.

Azure VM with cli

Is there a way to authenticate to Azure without any login on an Azure VM? Same feature like amazon instance profile so I can run azure commands without authentication
I don't think it is possible to communicate with Azure cloud without authentication like AWS using the instance profile. In Azure you have to use service principle with respective role(Reader, contributor or owner) assigned. Once service principle is created you can use it for authentication with Azure SDK or REST API. You can automate once you have service principle details.
You can use Azure CLI.
The authentication can be done using the Publish Settings file.
This is useful if you wish to use Azure CLI commands in a script etc.
You can download the file by using
azure account download
Make sure you keep this file safe as it provides direct access to your azure account.
Then authentication is a simple process of importing the file using
azure account import /path/to/.publishsettings_file
Now deployment commands can be run on the command line without logging in.
Scroll to the section public settings file in the link for more info on how to use the publish settings file.

How do I authenticate Jenkins to run Azure xplat cli commands?

I have a Jenkins running on Ubuntu.
I want to create jobs that will kick-off Azure xplat-cli commands.
How do I authenticate Jenkins?
We achieved the same using this article: https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/#authenticate-service-principal-with-password---azure-cli
Briefly you have to create a new application record within Azure Active Directory that will have its own login and password. Then you should grant required permissions to that account. After that you can perform a fully non-interactive login command that works for ARM model.

Resources