我正在致力于通过 Azure 的 GH 操作开发部署工作流程。我已经制定了输出:如何捕获新部署的虚拟机的 IP,以及后续作业的作业策略。
outputs:
ip-addresses: ${{ steps.get-ips.outputs.ips }}
run: |
$ips = @()
foreach ($i in ${{ github.event.inputs.vmcount }}) {
$ip=(Get-AzNetworkInterface -ResourceGroupName my-rg).IpConfigurations.PrivateIpAddress
$ips += $ip
}
$json = $ips | ConvertTo-Json -AsArray -Compress
Write-Output $json
echo "ips=$json" >> $env:GITHUB_OUTPUT
- name: Output values
run: |
echo ${{ steps.get-ips.outputs.ips }}
这效果很好,但我正在捕获指定资源组中的所有 IP。如果除了新部署的虚拟机之外还有其他虚拟机,这些 IP 也会被捕获。我只想要新部署的虚拟机的 IP(如果碰巧有其他虚拟机)。
各位有什么建议吗?谢谢!
这效果很好,但我正在捕获指定资源组中的所有 IP。如果除了新部署的虚拟机之外还有其他虚拟机,这些 IP 也会被捕获。我只想要新部署的虚拟机的 IP(如果碰巧有其他虚拟机)。
我建议您部署具有特定标签的虚拟机或在网络接口中设置标签,如
reason:repro
。
现在,使用上述标签,您可以捕获特定虚拟机的 IP。
$VMs = get-azvm
$ips = @()
foreach ($VM in $VMs)
{
[Hashtable]$VMTag = (Get-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name).Tags
foreach ($h in $VMTag.GetEnumerator()) {
if (($h.Name -eq "Reason") -and ($h.value -eq "Repro"))
{
Write-host "VM with tags Resource:test are" $VM.Name
$ip=(Get-AzNetworkInterface | Where {$_.Id -eq $VM.NetworkProfile.NetworkInterfaces[0].Id}).IpConfigurations.PrivateIpAddress
$ips += $ip
}
$json = $ips | ConvertTo-Json -AsArray -Compress
Write-Output $json
}
}
上述脚本将获取部署有标签的特定 Azure 虚拟机,并从中捕获 IP 地址。
输出:
VM with tags Resource:test are AG-VM
["192.168.1.9","10.5.0.6","10.5.0.5","172.16.10.4","172.17.11.4","10.0.1.4","10.0.1.5"]
["192.168.1.9","10.5.0.6","10.5.0.5","172.16.10.4","172.17.11.4","10.0.1.4","10.0.1.5"]
["192.168.1.9","10.5.0.6","10.5.0.5","172.16.10.4","172.17.11.4","10.0.1.4","10.0.1.5"]
["192.168.1.9","10.5.0.6","10.5.0.5","172.16.10.4","172.17.11.4","10.0.1.4","10.0.1.5"]
["192.168.1.9","10.5.0.6","10.5.0.5","172.16.10.4","172.17.11.4","10.0.1.4","10.0.1.5"]
["192.168.1.9","10.5.0.6","10.5.0.5","172.16.10.4","172.17.11.4","10.0.1.4","10.0.1.5"]
参考:
通过在 PowerShell 中使用标签进行过滤来获取 Azure VM 详细信息 - Stack Overflow,作者:Aatif Akhter