Ansible-标记运行Azure的VM

时间:2018-08-31 18:46:49

标签: azure ansible

今天,我一直在寻找从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中解决,但仍无法使用“自定义图片”。

所以问题是如何在运行的VM上进行操作?如何更改旧标签并添加新标签?

1 个答案:

答案 0 :(得分:0)

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

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