使用Ansible with Azure从Azure VM获取事实

时间:2018-04-16 14:15:52

标签: azure ansible ansible-2.x ansible-facts

我正在尝试使用一个可在Azure中创建虚拟机的游戏手册,并在创建后在其上运行一个剧本。

我可以创建虚拟机,但由于公共IP在创建之前不存在,因此无法用于清单。这是我目前的名字:

---
- hosts: localhost
  tasks:
- name: Create virtual network
  azure_rm_virtualnetwork:
    resource_group: az-test
    name: az-test-vnet
    address_prefixes: "10.43.0.0/16"
- name: Add subnet
  azure_rm_subnet:
    resource_group: az-test
    name: az-test-subnet
    address_prefix: "10.43.0.0/24"
    virtual_network: az-test-vnet
- name: Create VM
  azure_rm_virtualmachine:
    resource_group: az-test
    name: az-test-vm
    vm_size: Standard_DS1_v2
    admin_username: az-test
    ssh_password_enabled: false
    ssh_public_keys: 
      - path: /home/az-test/.ssh/authorized_keys
        key_data: "ssh-rsa xxxx "
    image:
      offer: CentOS
      publisher: OpenLogic
      sku: '7.4'
      version: latest
#- name: Get facts for all Public IPs within a resource groups
#  azure_rm_publicip_facts:
#    resource_group: az-test

最后一点是Ansible Azure模块,该模块应该返回资源组中的所有公共IP但是失败。

即使我可以动态地检索公共IP地址,也可以在其上运行剧本,因为ansible-playbook最初只在“localhost”上运行,而公共IP不在库存中?

使用最新的一切:

  • ansible 2.4.3.0
  • python 2.7
  • 天青-CLI-芯2.0.31

为凌乱的YAML格式化道歉......

编辑: 可以看到Ansible正在收集我可以使用但不确定如何引用它们的事实?

"ansible_facts": {
"azure_vm": {
    "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Compute/virtualMachines/az-test",
    "location": "eastus",
    "name": "az-test",
    "powerstate": "running",
    "properties": {
        "hardwareProfile": {
            "vmSize": "Standard_DS1_v2"
        },
        "instanceView": {
            "disks": [
                {
                    "name": "az-test.vhd",
                    "statuses": [
                        {
                            "code": "ProvisioningState/succeeded",
                            "displayStatus": "Provisioning succeeded",
                            "level": "Info",
                            "time": "2018-04-16Tx"
                        }
                    ]
                }
            ],
            "extensions": [
                {
                    "name": "OmsAgentForLinux",
                    "statuses": [
                        {
                            "code": "ProvisioningState/succeeded",
                            "displayStatus": "Provisioning succeeded",
                            "level": "Info",
                            "message": "Enable succeeded"
                        }
                    ],
                    "type": "Microsoft.EnterpriseCloud.Monitoring.OmsAgentForLinux",
                    "typeHandlerVersion": "1.4.60.2"
                }
            ],
            "statuses": [
                {
                    "code": "ProvisioningState/succeeded",
                    "displayStatus": "Provisioning succeeded",
                    "level": "Info",
                    "time": "2018-04-16Tx"
                },
                {
                    "code": "PowerState/running",
                    "displayStatus": "VM running",
                    "level": "Info"
                }
            ],
            "vmAgent": {
                "extensionHandlers": [
                    {
                        "status": {
                            "code": "ProvisioningState/succeeded",
                            "displayStatus": "Ready",
                            "level": "Info",
                            "message": "Plugin enabled"
                        },
                        "type": "Microsoft.EnterpriseCloud.Monitoring.OmsAgentForLinux",
                        "typeHandlerVersion": "1.4.60.2"
                    }
                ],
                "statuses": [
                    {
                        "code": "ProvisioningState/succeeded",
                        "displayStatus": "Ready",
                        "level": "Info",
                        "message": "Guest Agent is running",
                        "time": "2018-04-16Tx"
                    }
                ],
                "vmAgentVersion": "2.2.25"
            }
        },
        "networkProfile": {
            "networkInterfaces": [
                {
                    "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Network/networkInterfaces/az-test01",
                    "name": "az-test01",
                    "properties": {
                        "dnsSettings": {
                            "appliedDnsServers": [],
                            "dnsServers": [],
                            "internalDomainNameSuffix": "x.bx.internal.cloudapp.net"
                        },
                        "enableAcceleratedNetworking": false,
                        "enableIPForwarding": false,
                        "ipConfigurations": [
                            {
                                "etag": "W/\"x\"",
                                "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Network/networkInterfaces/az-test01/ipConfigurations/default",
                                "name": "default",
                                "properties": {
                                    "primary": true,
                                    "privateIPAddress": "10.43.0.5",
                                    "privateIPAddressVersion": "IPv4",
                                    "privateIPAllocationMethod": "Dynamic",
                                    "provisioningState": "Succeeded",
                                    "publicIPAddress": {
                                        "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Network/publicIPAddresses/az-test01",
                                        "name": "az-test01",
                                        "properties": {
                                            "idleTimeoutInMinutes": 4,
                                            "ipAddress": "52.x.x.x",
                                            "ipConfiguration": {
                                                "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Network/networkInterfaces/az-test01/ipConfigurations/default"
                                            },
                                            "provisioningState": "Succeeded",
                                            "publicIPAddressVersion": "IPv4",
                                            "publicIPAllocationMethod": "Static",
                                            "resourceGuid": "x"
                                        }
                                    },
                                    "subnet": {
                                        "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Network/virtualNetworks/az-test-vnet/subnets/az-test-subnet"
                                    }
                                }
                            }
                        ],
                        "macAddress": "00-0D-3A-1D-E1-8A",
                        "networkSecurityGroup": {
                            "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Network/networkSecurityGroups/az-test01"
                        },
                        "primary": true,
                        "provisioningState": "Succeeded",
                        "resourceGuid": "x",
                        "virtualMachine": {
                            "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Compute/virtualMachines/az-test"
                        }
                    }
                }
            ]
        },
        "osProfile": {
            "adminUsername": "az-test",
            "computerName": "az-test",
            "linuxConfiguration": {
                "disablePasswordAuthentication": true,
                "ssh": {
                    "publicKeys": [
                        {
                            "keyData": "ssh-rsa AAx ",
                            "path": "/home/az-test/.ssh/authorized_keys"
                        }
                    ]
                }
            },
            "secrets": []
        },
        "provisioningState": "Succeeded",
        "storageProfile": {
            "dataDisks": [],
            "imageReference": {
                "offer": "CentOS",
                "publisher": "OpenLogic",
                "sku": "7.4",
                "version": "7.4.20180118"
            },
            "osDisk": {
                "caching": "ReadOnly",
                "createOption": "fromImage",
                "diskSizeGB": 30,
                "name": "az-test.vhd",
                "osType": "Linux",
                "vhd": {
                    "uri": "https://x.blob.core.windows.net/vhds/az-test.vhd"
                }
            }
        },
        "vmId": "x"
    },
    "resources": [
        {
            "id": "/subscriptions/x/resourceGroups/az-test/providers/Microsoft.Compute/virtualMachines/az-test/extensions/OmsAgentForLinux",
            "location": "eastus",
            "name": "OmsAgentForLinux",
            "properties": {
                "autoUpgradeMinorVersion": true,
                "provisioningState": "Succeeded",
                "publisher": "Microsoft.EnterpriseCloud.Monitoring",
                "settings": {
                    "azureResourceId": "/subscriptions/x/resourcegroups/az-test/providers/microsoft.compute/virtualmachines/az-test",
                    "stopOnMultipleConnections": true,
                    "workspaceId": "x"
                },
                "type": "OmsAgentForLinux",
                "typeHandlerVersion": "1.0"
            },
            "type": "Microsoft.Compute/virtualMachines/extensions"
        }
    ],
    "tags": {},
    "type": "Microsoft.Compute/virtualMachines"
}

}

1 个答案:

答案 0 :(得分:0)

模块名称应为azure_rm_publicipaddress_facts,而不是azure_rm_publicip_facts