Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible-标记Azure运行的虚拟机_Azure_Ansible - Fatal编程技术网

Ansible-标记Azure运行的虚拟机

Ansible-标记Azure运行的虚拟机,azure,ansible,Azure,Ansible,今天,我一直在寻找从Ansible自动标记Azure中运行的VM的最佳方法 第一种方法是使用azure\u rm\u virtualmachine模块,但在部署新的虚拟机时,它可以正常工作。当VM启动并运行时,这是另一个历史记录,主要是在使用自定义映像完成部署时 - name: Tag my VM azure_rm_virtualmachine: resource_group: myresourcegroup name: myvm admin_us

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

第一种方法是使用
azure\u rm\u 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"
请参阅:已在2.7中解决,但仍无法使用自定义图像


所以问题是如何在运行虚拟机的情况下做到这一点?如何更改旧标记并添加新标记?

问题是如何将
azure\u rm\u部署
azure\u rm\u虚拟机一起使用

通过
azure\u rm\u virtualmachine
我们注册事实并将其添加到变量中:

  - name: Azure Facts
    azure_rm_virtualmachine:
      name: myvm
      resource_group: myrsg
    register: myvm
然后,使用带有
azure\u rm\u deployment
的JSON模板部署VM,但保留VM的重要值:

注意:这些变量仅供参考,请正确使用它们以保持干净和易于管理:

密码不会更改,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'))]"
                    }]
                }
            }
        }
    ]
}
基本上,这是第一种方法,命名变量、应用方式等将以更优化的方式改变。我会在改进时更新它

{
    "$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'))]"
                    }]
                }
            }
        }
    ]
}