{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "virtualMachineName": {
      "type": "string"
    },
    "siteLocation": {
      "type": "string"
    },
    "virtualMachineSize": {
      "type": "string",
      "defaultValue": "Standard_B1ms",
      "allowedValues": [
        "Standard_B1ms",
        "Standard_B1s",
        "Standard_B2s",
        "Standard_DS1_v2"
      ]
    },
    "adminUsername": {
      "type": "string"
    },
    "adminPublicKey": {
      "type": "string"
    }
  },
  "variables": {
    "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]",
    "vnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', variables('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetId'), '/subnets/', variables('subnetName'))]",
    "networkInterfaceName": "ansibleVMnic",
    "networkSecurityGroupName": "ansible-nsg",
    "subnetName": "default",
    "virtualNetworkName": "ansible-vnet",
    "publicIpAddressName": "ansible-ip",
    "publicIpAddressType": "Dynamic",
    "publicIpAddressSku": "Basic",
    "osDiskType": "Premium_LRS"
  },
  "resources": [
    {
      "name": "[variables('networkInterfaceName')]",
      "type": "Microsoft.Network/networkInterfaces",
      "apiVersion": "2018-04-01",
      "location": "[parameters('siteLocation')]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkSecurityGroups/', variables('networkSecurityGroupName'))]",
        "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[concat('Microsoft.Network/publicIpAddresses/', variables('publicIpAddressName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "privateIPAllocationMethod": "Dynamic",
              "publicIpAddress": {
                "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', variables('publicIpAddressName'))]"
              }
            }
          }
        ],
        "networkSecurityGroup": {
          "id": "[variables('nsgId')]"
        }
      },
      "tags": {}
    },
    {
      "name": "[variables('networkSecurityGroupName')]",
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2018-08-01",
      "location": "[parameters('siteLocation')]",
      "properties": {
        "securityRules": [
          {
            "name": "SSH",
            "properties": {
              "priority": 300,
              "protocol": "TCP",
              "access": "Allow",
              "direction": "Inbound",
              "sourceAddressPrefix": "*",
              "sourcePortRange": "*",
              "destinationAddressPrefix": "*",
              "destinationPortRange": "22"
            }
          }
        ]
      },
      "tags": {}
    },
    {
      "name": "[variables('virtualNetworkName')]",
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2018-08-01",
      "location": "[parameters('siteLocation')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [ "10.0.0.0/24" ]
        },
        "subnets": [
          {
            "name": "default",
            "properties": {
              "addressPrefix": "10.0.0.0/24"
            }
          }
        ]
      },
      "tags": {}
    },
    {
      "name": "[variables('publicIpAddressName')]",
      "type": "Microsoft.Network/publicIpAddresses",
      "apiVersion": "2018-08-01",
      "location": "[parameters('siteLocation')]",
      "properties": {
        "publicIpAllocationMethod": "[variables('publicIpAddressType')]"
      },
      "sku": {
        "name": "[variables('publicIpAddressSku')]"
      },
      "tags": {}
    },
    {
      "name": "[parameters('virtualMachineName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2018-06-01",
      "location": "[parameters('siteLocation')]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', variables('networkInterfaceName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('virtualMachineSize')]"
        },
        "storageProfile": {
          "osDisk": {
            "createOption": "fromImage",
            "managedDisk": {
              "storageAccountType": "[variables('osDiskType')]"
            }
          },
          "imageReference": {
            "publisher": "OpenLogic",
            "offer": "CentOS",
            "sku": "7.5",
            "version": "latest"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            }
          ]
        },
        "osProfile": {
          "computerName": "[parameters('virtualMachineName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "linuxConfiguration": {
              "disablePasswordAuthentication": true,
              "ssh": {
                  "publicKeys": [
                      {
                          "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                          "keyData": "[parameters('adminPublicKey')]"
                      }
                  ]
              }
            }
          }
      },
      "tags": {}
    },
    {
      "name": "[concat(parameters('virtualMachineName'),'/CustomScriptForLinux')]",
      "type": "Microsoft.Compute/virtualMachines/extensions",
      "location": "[parameters('siteLocation')]",
      "apiVersion": "2015-06-15",
      "properties": {
        "publisher": "Microsoft.OSTCExtensions",
        "type": "CustomScriptForLinux",
        "typeHandlerVersion": "1.4",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "fileUris": [ "https://raw.githubusercontent.com/russmckendrick/azure-ansible-centos/master/install_ansible.sh" ],
          "commandToExecute": "sh install_ansible.sh"
        }
      },
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
      ]
    }
  ],
  "outputs": {
    "adminUsername": {
      "type": "string",
      "value": "[parameters('adminUsername')]"
    }
  }
}
