0
votes

I'm trying to have a single playbook that will create a VM in Azure and also run a playbook on it once it is created.

I can get the VM to be created but as the Public IP doesn't exist before it is created I can't use for the inventory. This is my current yaml:

---
- 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

The last bit there's an Ansible Azure module that should return all public IPs in the resource group but that fails.

Even if I could dynamically retrive the Public IP address, could the playbook even be run on it as ansible-playbook was initially only run on 'localhost' and the Public IP is not in the inventory?

Using latest of everything:

  • ansible 2.4.3.0
  • python 2.7
  • azure-cli-core-2.0.31

Apologies for the messy YAML formatting..

EDIT: Can see Ansible is gathering facts that I can use but not exactly sure how to reference them?

"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
Take a look at the example of ec2 module, section Launch instances, runs some tasks.Konstantin Suvorov
Thanks, add_host looks like what I want! I'll try it out.Hexdump
Hmm took a look and I'm not sure how to reference the facts generated by azure?Hexdump

1 Answers

0
votes

The module name should be azure_rm_publicipaddress_facts instead of azure_rm_publicip_facts