Created
November 27, 2017 22:14
-
-
Save mohclips/1605e09b264a7370159d84dcd1378e04 to your computer and use it in GitHub Desktop.
Ansible script to build a network, subnet and VM on Azure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: Create Azure VM | |
hosts: localhost | |
connection: local | |
vars: | |
# { | |
# "offer": "UbuntuServer", | |
# "publisher": "Canonical", | |
# "sku": "16.04-LTS", | |
# "urn": "Canonical:UbuntuServer:16.04-LTS:latest", | |
# "urnAlias": "UbuntuLTS", | |
# "version": "latest" | |
# }, | |
vm_offer: "UbuntuServer" | |
vm_pub: "Canonical" | |
vm_sku: "16.04-LTS" | |
vm_size: "Standard_A0" | |
az: "ukwest" | |
vm_net: "myVNet" | |
vm_subnet: "mySubnet" | |
vm_publicIP: "myPublicIP" | |
vm_NSG: "myNSG" | |
vm_NIC: "myNIC" | |
vm_Name: "Test01" | |
resource_group: "test01-resgrp" | |
os_user: "azuser" | |
tasks: | |
- name: Create a resource group | |
azure_rm_resourcegroup: | |
name: "{{ resource_group }}" | |
location: "{{ az }}" | |
tags: | |
testing: testing | |
delete: never | |
- name: Create virtual network | |
azure_rm_virtualnetwork: | |
resource_group: "{{ resource_group }}" | |
name: "{{ vm_net }}" | |
address_prefixes: "10.0.0.0/16" | |
- name: Add subnet | |
azure_rm_subnet: | |
resource_group: "{{ resource_group }}" | |
name: "{{ vm_subnet }}" | |
address_prefix: "10.0.1.0/24" | |
virtual_network: "{{ vm_net }}" | |
- name: Create public IP address | |
azure_rm_publicipaddress: | |
resource_group: "{{ resource_group }}" | |
allocation_method: Static | |
name: "{{ vm_publicIP }}" | |
register: reg_publicIP | |
- debug: var=reg_publicIP | |
- name: Create Network Security Group that allows SSH | |
azure_rm_securitygroup: | |
resource_group: "{{ resource_group }}" | |
name: "{{ vm_NSG }}" | |
rules: | |
- name: SSH | |
protocol: Tcp | |
destination_port_range: 22 | |
access: Allow | |
priority: 1001 | |
direction: Inbound | |
- name: Create virtual network interface card | |
azure_rm_networkinterface: | |
resource_group: "{{ resource_group }}" | |
name: "{{ vm_NIC }}" | |
virtual_network: "{{ vm_net }}" | |
subnet: "{{ vm_subnet }}" | |
public_ip_name: "{{ vm_publicIP }}" | |
security_group: "{{ vm_NSG }}" | |
- name: Create VM | |
azure_rm_virtualmachine: | |
resource_group: "{{ resource_group }}" | |
name: "{{ vm_Name }}" | |
vm_size: "{{ vm_size }}" | |
admin_username: "{{ os_user }}" | |
ssh_password_enabled: false | |
ssh_public_keys: | |
- path: "/home/{{ os_user }}/.ssh/authorized_keys" | |
key_data: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABwlu57 mykey" | |
network_interfaces: "{{ vm_NIC }}" | |
image: | |
offer: "{{ vm_offer }}" | |
publisher: "{{ vm_pub }}" | |
sku: "{{ vm_sku }}" | |
version: latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ansible-playbook build-1vm.yml | |
[WARNING]: Could not match supplied host pattern, ignoring: all | |
[WARNING]: provided hosts list is empty, only localhost is available | |
PLAY [Create Azure VM] ************************************************************************************************************ | |
TASK [Gathering Facts] ************************************************************************************************************ | |
ok: [localhost] | |
TASK [Create a resource group] **************************************************************************************************** | |
changed: [localhost] | |
TASK [Create virtual network] ***************************************************************************************************** | |
changed: [localhost] | |
TASK [Add subnet] ***************************************************************************************************************** | |
changed: [localhost] | |
TASK [Create public IP address] *************************************************************************************************** | |
changed: [localhost] | |
TASK [debug] ********************************************************************************************************************** | |
ok: [localhost] => { | |
"reg_publicIP": { | |
"changed": true, | |
"failed": false, | |
"state": { | |
"dns_settings": {}, | |
"etag": "W/\"7d7f8fb2-87fb-4328-a96e-3d54fdd7c22b\"", | |
"idle_timeout_in_minutes": 4, | |
"ip_address": "51.141.26.148", | |
"location": "ukwest", | |
"name": "myPublicIP", | |
"provisioning_state": "Succeeded", | |
"public_ip_allocation_method": "Static", | |
"tags": null, | |
"type": "Microsoft.Network/publicIPAddresses" | |
} | |
} | |
} | |
TASK [Create Network Security Group that allows SSH] ****************************************************************************** | |
changed: [localhost] | |
TASK [Create virtual network interface card] ************************************************************************************** | |
changed: [localhost] | |
TASK [Create VM] ****************************************************************************************************************** | |
changed: [localhost] | |
PLAY RECAP ************************************************************************************************************************ | |
localhost : ok=9 changed=7 unreachable=0 failed=0 | |
Create VM ------------------------------------------------------------- 152.52s | |
Create virtual network interface card ---------------------------------- 35.31s | |
Create Network Security Group that allows SSH -------------------------- 14.84s | |
Create virtual network -------------------------------------------------- 8.18s | |
Create public IP address ------------------------------------------------ 8.06s | |
Add subnet -------------------------------------------------------------- 7.01s | |
Create a resource group ------------------------------------------------- 2.46s | |
Gathering Facts --------------------------------------------------------- 0.68s | |
------------------------------------------------------------------------ 0.03s | |
Playbook finished: Mon Nov 27 22:08:08 2017, 9 total tasks. 0:03:49 elapsed. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment