Skip to content

Instantly share code, notes, and snippets.

@rohit20001221
Created April 11, 2025 13:47
Show Gist options
  • Select an option

  • Save rohit20001221/a79d3c8aaa8ccc4acc4a2686a0f271ea to your computer and use it in GitHub Desktop.

Select an option

Save rohit20001221/a79d3c8aaa8ccc4acc4a2686a0f271ea to your computer and use it in GitHub Desktop.
ansible-galaxy collection install azure.azcollection
pip install -r https://raw.githubusercontent.com/ansible-collections/azure.azcollection/main/requirements-azure.txt
---
- name: Create an Azure VM in East US with specific configurations
hosts: localhost
connection: local
gather_facts: no
vars:
resource_group: myResourceGroup
location: eastus
vm_name: myUbuntuVM
admin_username: azureuser
ssh_key_path: ~/.ssh/azure_key
vm_size: Standard_B2ms
image_offer: UbuntuServer
image_publisher: Canonical
image_sku: 20_04-lts-gen2
image_version: latest
tasks:
- name: Create resource group
azure.azcollection.azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
- name: Generate SSH key pair if not present
ansible.builtin.openssh_keypair:
path: "{{ ssh_key_path }}"
type: rsa
size: 2048
register: ssh_keypair
when: not lookup('file', ssh_key_path + '.pub', errors='ignore')
- name: Create virtual network
azure.azcollection.azure_rm_virtualnetwork:
resource_group: "{{ resource_group }}"
name: myVNet
address_prefixes: "10.0.0.0/16"
location: "{{ location }}"
- name: Create subnet
azure.azcollection.azure_rm_subnet:
resource_group: "{{ resource_group }}"
name: mySubnet
address_prefix: "10.0.1.0/24"
virtual_network: myVNet
- name: Create network security group with rules
azure.azcollection.azure_rm_securitygroup:
resource_group: "{{ resource_group }}"
name: myNSG
location: "{{ location }}"
rules:
- name: AllowSSH
protocol: Tcp
destination_port_range: 22
access: Allow
priority: 1001
direction: Inbound
- name: AllowHTTP
protocol: Tcp
destination_port_range: 80
access: Allow
priority: 1002
direction: Inbound
- name: AllowHTTPS
protocol: Tcp
destination_port_range: 443
access: Allow
priority: 1003
direction: Inbound
- name: AllowAllOutbound
protocol: "*"
direction: Outbound
access: Allow
priority: 1000
destination_port_range: "*"
- name: Create public IP
azure.azcollection.azure_rm_publicipaddress:
resource_group: "{{ resource_group }}"
allocation_method: Dynamic
name: myPublicIP
location: "{{ location }}"
- name: Create network interface
azure.azcollection.azure_rm_networkinterface:
resource_group: "{{ resource_group }}"
name: myNIC
location: "{{ location }}"
virtual_network: myVNet
subnet: mySubnet
public_ip_name: myPublicIP
security_group: myNSG
- name: Create virtual machine
azure.azcollection.azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name }}"
vm_size: "{{ vm_size }}"
admin_username: "{{ admin_username }}"
ssh_password_enabled: false
ssh_public_keys:
- path: "/home/{{ admin_username }}/.ssh/authorized_keys"
key_data: "{{ lookup('file', ssh_key_path + '.pub') }}"
network_interfaces: myNIC
image:
offer: "{{ image_offer }}"
publisher: "{{ image_publisher }}"
sku: "{{ image_sku }}"
version: "{{ image_version }}"
os_disk:
name: myOSDisk
caching: ReadWrite
create_option: FromImage
managed_disk_type: StandardHDD
location: "{{ location }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment