Ansible - 标记Azure运行的VM

问题描述 投票:0回答:1

今天,我一直在寻找从Ansible自动标记在Azure中运行虚拟机的最佳方法。

第一种方法是使用azure_rm_virtualmachine模块,但在部署新VM时它可以正常工作。当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中解决但仍未使用自定义图像。

那么问题是如何运行VM?如何更换旧标签并添加新标签?

azure ansible
1个回答
0
投票

问题是使用azure_rm_deploymentazure_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'))]"
                    }]
                }
            }
        }
    ]
}

基本上这是第一种方法,命名变量,应用方式等将以更优化的方式改变。我会在改进它时更新它。

© www.soinside.com 2019 - 2024. All rights reserved.