Reputation: 2378
I'm trying to automate some processes in our project which includes some steps like create the VM, connect to the newly created VM and run some commands remotely.
Now previously what I used to do is run this commands in sequence manually.
1.Create a VM
New-AzureRmResourceGroupDeployment -Name VmDeployment -ResourceGroupName XYZ`
-TemplateFile "C:\Templates\template.json" `
-TemplateParameterFile "C:\Templates\parameters.json"
2.Connect to the VM.
Set-Item WSMan:\localhost\Client\TrustedHosts -Value 100.9.4.12
$UserName = "100.9.4.12\admin"
$Password = ConvertTo-SecureString "admin@123" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$s = New-PSSession -ComputerName 100.9.4.12 -Credential $psCred
Invoke-Command -Session $s -ScriptBlock {Get-Service 'ServiceName'}
In this the IP address is used to add that in the trusted hosts on the client.I used to check the generated IP address on Azure Portal, replace that IP in the command and run them manually. But now , since I'm automating, there will be no manual intervention in the process.
So how should I retrieve the IP address of the newly created VM?
Upvotes: 1
Views: 6808
Reputation: 31384
You can use the Powershell command to get all the private IP of the VMs. And the command will like this:
Get-AzureRmNetworkInterface -ResourceGroupName resourceGroupName | Select-Object {$_.IpConfigurations.PrivateIpAddress}
Update
Also the command to set a variable like this:
$vm = Get-AzureRmNetworkInterface -ResourceGroupName charles | Select-Object -Property @{Name="Ip"; Expression = {$_.IpConfigurations.PrivateIpAddress}}
$vm[0].Ip
Then you will only get the IP address.
Upvotes: 0
Reputation: 1039
Not directly related to your question, but have you thought about using Terraform to automate the creation of your resources? http://terraform.io
Terraform is very similar to ARM (only much nicer) here's an example of a VM creation and the public IP export:
resource "azurerm_resource_group" "main" {
name = "test-resources"
location = "West US 2"
}
resource "azurerm_virtual_network" "main" {
name = "test-network"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = "${azurerm_resource_group.main.name}"
virtual_network_name = "${azurerm_virtual_network.main.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_public_ip" "test" {
name = "test-public-ip"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
public_ip_address_allocation = "dynamic"
tags {
environment = "production"
}
}
resource "azurerm_network_interface" "main" {
name = "test-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "dynamic"
public_ip_address_id = "${azurerm_public_ip.test.id}"
}
}
resource "azurerm_virtual_machine" "main" {
name = "test-vm"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS1_v2"
# Uncomment this line to delete the OS disk automatically when deleting the VM
# delete_os_disk_on_termination = true
# Uncomment this line to delete the data disks automatically when deleting the VM
# delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "production"
}
}
output "vm-ip" {
value = "${azurerm_public_ip.test.ip_address}"
}
Upvotes: 4
Reputation: 72151
why do you care about the ip address? just use dns name? you always know that one (since you define it when you create vm, at least you can do that). Another option is to output the ip address as part of the arm template.
or query the ip address in powershell:
Get-AzureRmVM -ResourceGroupName ‘HSG-ResourceGroup’ -Name ‘HSG-LinuxVM’ | Get-AzureRmPublicIpAddress
Upvotes: 2