Ansible - 标记 Azure 运行的虚拟机
Posted
技术标签:
【中文标题】Ansible - 标记 Azure 运行的虚拟机【英文标题】:Ansible - Tagging Azure running VMs 【发布时间】:2019-02-06 19:49:54 【问题描述】:今天,我一直在努力寻找从 Ansible 自动标记 Azure 中正在运行的 VM 的最佳方法。
第一种方法是使用azure_rm_virtualmachine
模块,但在部署新虚拟机时它可以正常工作。当 VM 启动并运行时,这是另一个历史记录,主要是在使用自定义映像完成部署时。
- name: Tag my VM
azure_rm_virtualmachine:
resource_group: myresourcegroup
name: myvm
admin_username: ansible
admin_password: mypassword
virtual_network_name: myvnet
virtual_network_resource_group: myvnetrsg
vm_size: Standard_D2_v2
state: present
started: no
append_tags: True
image:
name: mycustomimage
resource_group: myimagesrsg
tags:
env: "dev"
请参阅:https://github.com/ansible/ansible/issues/35235 在 2.7 中已解决,但仍无法使用自定义图像。
那么问题是如何处理正在运行的虚拟机?如何更换旧标签并添加新标签?
【问题讨论】:
【参考方案1】:问题是使用azure_rm_deployment
和azure_rm_virtualmachine
。
使用azure_rm_virtualmachine
,我们注册事实并将它们添加到变量中:
- name: Azure Facts
azure_rm_virtualmachine:
name: myvm
resource_group: myrsg
register: myvm
然后,使用带有azure_rm_deployment
的 JSON 模板部署 VM,但保留 VM 的重要值:
注意:变量仅供参考,正确使用它们以保持清洁和可管理:
- name: Create Azure VM from ARM template with public IP
azure_rm_deployment:
state: present
deployment_name: mydeployment
location: mylocation
resource_group_name: myresorcegroup
wait_for_deployment_completion: yes
template: " lookup('template', 'azure.json') "
parameters:
tags:
value: " vmtags "
adminUsername:
value: " myvm.ansible_facts.azure_vm.properties.osProfile.adminUsername "
adminPassword:
value: mypassword
imageName:
value: " myvm.ansible_facts.azure_vm.properties.storageProfile.imageReference.id | basename "
imageResourceGroup:
value: myimagesrsg
dnsLabelPrefix:
value: " myvm.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.publicIPAddress.properties.dnsSettings.domainNameLabel "
vmName:
value: myvm
ComputerName:
value: " myvm.ansible_facts.azure_vm.properties.osProfile.computerName "
vmResourceGroup:
value: myrsg
nicName:
value: " myvm.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].name "
virtualNetworkName:
value: "myvm.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.subnet.id.split('/')[-3] "
publicIPAddressName:
value: " myvm.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.publicIPAddress.name "
subnetName:
value: " myvm.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.subnet.id | basename "
vmSize:
value: " myvm.ansible_facts.azure_vm.properties.hardwareProfile.vmSize "
storageAccountType:
value: " myvm.ansible_facts.azure_vm.properties.storageProfile.osDisk.managedDisk.storageAccountType "
密码不会改变,VM 名称和资源组已经知道,标签是这样的字典:
vars:
vmtags:
MyFirstDay: "Saturday"
Env: "dev"
JSON 模板呢?
JSON 是标准 Azure 模板,但将标签添加为 对象:
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters":
"tags":
"type": "object"
,
"adminUsername":
"type": "string"
,
"adminPassword":
"type": "securestring"
,
"vmName":
"type": "string"
,
"ComputerName":
"type": "string"
,
"imageName":
"type": "string"
,
"imageResourceGroup":
"type": "string"
,
"vmSize":
"type": "string"
,
"vmResourceGroup":
"type": "string"
,
"virtualNetworkName":
"type": "string"
,
"nicName":
"type": "string"
,
"subnetName":
"type": "string"
,
"dnsLabelPrefix":
"type": "string"
,
"publicIPAddressName":
"type": "string"
,
"storageAccountType":
"type": "string"
,
"variables":
"apiVersion": "2015-06-15",
"publicIPAddressType": "Dynamic",
"privateIPAddressType": "Dynamic",
"addressPrefix": "10.0.0.0/16",
"subnetPrefix": "10.0.0.0/24",
"vnetID": "[resourceId('Microsoft.Network/virtualNetworks',parameters('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetID'),'/subnets/',parameters('subnetName'))]",
"sshKeyPath": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]",
"hostDNSNameScriptArgument": "[concat('*.',resourceGroup().location,'.cloudapp.azure.com')]"
,
"resources": [
"apiVersion": "[variables('apiVersion')]",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[parameters('publicIPAddressName')]",
"location": "[resourceGroup().location]",
"properties":
"publicIPAllocationMethod": "[variables('publicIPAddressType')]",
"dnsSettings":
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
,
"apiVersion": "[variables('apiVersion')]",
"type": "Microsoft.Network/virtualNetworks",
"name": "[parameters('virtualNetworkName')]",
"location": "[resourceGroup().location]",
"properties":
"addressSpace":
"addressPrefixes": [
"[variables('addressPrefix')]"
]
,
"subnets": [
"name": "[parameters('subnetName')]",
"properties":
"addressPrefix": "[variables('subnetPrefix')]"
]
,
"apiVersion": "[variables('apiVersion')]",
"type": "Microsoft.Network/networkInterfaces",
"name": "[parameters('nicName')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Network/publicIPAddresses/', parameters('publicIPAddressName'))]",
"[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]"
],
"properties":
"ipConfigurations": [
"name": "ipconfig1",
"properties":
"privateIPAllocationMethod": "[variables('privateIPAddressType')]",
"publicIPAddress":
"id": "[resourceId('Microsoft.Network/publicIPAddresses',parameters('publicIPAddressName'))]"
,
"subnet":
"id": "[variables('subnetRef')]"
]
,
"name": "[parameters('vmName')]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2016-04-30-preview",
"location": "[resourceGroup().location]",
"tags": "[parameters('tags')]",
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('nicName'))]"
],
"properties":
"osProfile":
"computerName": "[parameters('ComputerName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
,
"hardwareProfile":
"vmSize": "[parameters('vmSize')]"
,
"storageProfile":
"imageReference":
"id": "[resourceId(parameters('imageResourceGroup'),'Microsoft.Compute/images', parameters('imageName'))]"
,
"osDisk":
"name": "[concat(parameters('vmName'),'_OsDisk')]",
"createOption": "FromImage",
"managedDisk":
"storageAccountType": "[parameters('storageAccountType')]"
,
"networkProfile":
"networkInterfaces": [
"id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('nicName'))]"
]
]
基本上这是第一种方法,命名变量、应用方式等将以更优化的方式发生变化。我会在改进时更新它。
【讨论】:
以上是关于Ansible - 标记 Azure 运行的虚拟机的主要内容,如果未能解决你的问题,请参考以下文章
用于连接到虚拟机并运行 azcopy 的 Azure 逻辑应用