How to obtain Azure subscription details via Ansible module? - azure

I am getting started with Ansible + Azure. While I see Ansible modules for quite a few tasks, I am kinda stuck at the basic task of obtaining the subscription_id and subscription_name
To work around this I am using the az account show --subscription <subscription_name> command instead. I pull the output of that command into ansible and extract subscription_id and subscription_name
Is there a way to perform this task via an Ansible module instead ?

Looks it is not possible to perform this task via the Ansible module, all the Ansible module on Azure here, there is no one can do this.

Related

Job Suspended Run Login-AzureRmAccount to login using Azure AutomationAccounts System Managed Identity

I am trying to shutdown the VM using Azure Automation Account System Managed identity option.
However I am ending up with below error.
As per the other articles it is mentioned to upgrade the module Update-ModulesInAutomationToLatestVersion but I could not update due to below error. I am not sure what is the issue in the script. Same script works with AzureRunAsConnection option without issues ( script ).I even checked with simple login with System Managed Identity it successfully login and fetches the resource group names.
I have tested the above shared script in my automation account. Below are
the couple of observations:
You need to use Connect-AzureRMAccount -Identity cmdlet instead of 'connect-AzAccount` to connect to your subscription because the rest of the script that you have written using Azure RM cmdlets.
If we use AzureRM cmdlets in your run book script the job is getting suspended stating that Azure RM is going to retired and suggesting us to use Az Module in your workflow.
You can refer to this documentation on how to migrate your PowerShell scripts automatically from AzureRM to AZ modules.
If you want to perform start/stop on your virtual Machines you can leverage the Azure Automation start/stop during the off hours feature.
According to the MICROSOFT DOCUMENTATION and looking at your script the Azure Rm module is not supported and it has been updated to the latest version of Az module.
For more information please refer the below links:-
MICROSOFT DOCUMENT|Using a system-assigned managed identity for an Azure Automation account & Troubleshoot runbook issue.

Azure VM creation automation using Terraform and Jenkins?

I have the Terraform script/template to provision a VM in Azure,it accepts the input and provision the VM along with the required resources.
I have created the Azure Storage Account and uploaded the script into the blob container.
We are using Jenkins as our CI/CD tool.
Now, I want to build the pipeline or automation using Jenkins which would take the necessary input and run Terraform script to provision the VM.
How do I build the Jenkins pipeline so that I can run the pipeline / automation multiple times and provision the individual VMs?
Any sample Jenkins pipeline would be really helpful.
if your sole purpose is to run the terraform through jenkins then there are many pipeline scripts available , I have pasted one below the link, which will help to build a jenkins pipeline and has guided documentation as well.
https://github.com/manheim/terraform-pipeline
There is a dedicated architecture reference and sample available on the Azure documentation on Immutable Infrastructure CI/CD using Jenkins and Terraform on Azure Virtual Architecture
And here is the template as well
New-AzResourceGroup -Name <resource-group-name> -Location <resource-group-location> #use this command when you need to create a new resource group for your deployment
New-AzResourceGroupDeployment -ResourceGroupName <resource-group-name> -TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/application-workloads/jenkins/jenkins-cicd-vmss/azuredeploy.json

How to run AzureRM script via azure devops pipeline

I am attempting to run an azureRM script via a devops pipeline.
I have tried all the possible powershell tasks, however I am always met with the following error:
New-AzureRmResource : The term 'New-AzureRmResource' is not recognized as a name of a cmdlet, function, script file, or executable program.
The script is designed to add a VNET to an app service.
I am aware that AzureRM is outdated, however there is no way to do cross-regional VNet integration via Az or the az cli.
How can I run an AzureRm script via a devops pipeline?
I assume you use Microsoft Hosted Agents
Multiple ways:
Invoke-RestMethod - All commands are accessible as API endpoints.
Install-Module - Just Install AzureRM.
Use AzurePowerShell#3 task version - Easiest solution - See docs, The newer #4 and #5 versions do not support AzureRM.
Because Az PowerShell modules now have all the capabilities of AzureRM PowerShell modules and more, we'll retire AzureRM PowerShell modules on 29 February 2024. So you can try to use Az.Resources module instead.
For the changes between AzureRm and Az, please view this document(Az.Resources (previously AzureRM.Resources)).
In addition, agree with Repcak. You can use AzurePowerShell#3 task or earlier, because these versions of task support AzureRm modules.

Can an ansible VM in Azure query its own VM tags?

I have a VM running in Azure which has ansible installed.
Is it possible to run ansible to find the tags associated with this VM (which ansible is running on)?
Furthermore Can it be done as part of a "lookup" expression?
E.g hosts: {{lookup(tag_name) }}
Elaborating Hassan Raza's suggestion:
This is Azure REST API reference and this is Tags REST API section and this REST API helps to get the entire set of tags on a resource. So you may just use this REST API to accomplish your requirement.
On the other hand, you can also leverage Az PowerShell to get the entire set of tags on a resource and this is that Az PowerShell cmdlet which is part of Az.Resources module so you need it this module imported as a pre-requisite. Again, you may just use this cmdlet from your local machine or from wherever you want to.
On the another hand, you may also create something called Azure Automation Runbook (in general words its a script that's created in Azure cloud under an Azure Automation account). So you may create an Azure Automation account and then a simple runbook to get the entire set of tags on a resource.
The solution we went with was to use the Ansible azure_rm_virtualmachine_info_module :
https://docs.ansible.com/ansible/latest/collections/azure/azcollection/azure_rm_virtualmachine_info_module.html
This returns azure tag info.

Pass parameters to --custom-data Cloud Init script using az cli

Is there a way to pass parameters to the cloud init script? Trying to achieve something like this:
az vm create ... --custom-data myscript.yml param1 param2
Obviously it is possible to use variables from an ARM template like here: https://github.com/Azure/iotedge-vm-deploy/blob/master/cloud-init.txt#L44
I'm afraid you made the mistake. It's not the parameter for the CLI command az vm create, it's a variable of the Azure Template. You can see the variable docs is defined in the template variables block, and of course, you can then use it in the template for the VM property customData.
Now the CLI command az vm create does not support the parameters for the --custom-data. You can get more details about the cloud-init for Azure VM here.

Resources