Use the Ansible CLI to create a VM in AZURE Platform
Manage Linux virtual machines in Azure using Ansible
--- # Get facts for the user
- name: Create a Virtual Machine on Azure Using Ansible
hosts: localhost
vars:
vm_name: "Test-Ansible"
vm_size: "Standard_B1ls"
vm_image: "RedHat:RHEL:8-LVM:latest"
vm_username: "testansible"
vm_password: "my-password@1234"
rg_name: "test-ansible"
vnet_name: "test-ansible"
subnet_name: "test-ansible"
location: "centralindia"
subscription_id: <YOUR SUBSCRIPTION ID>
tenant: <YOUR TENANT ID>
client_id: <YOUR CLIENT ID>
secret: <YOUR SECRET>
tasks:
- name: Create a Resource Group
azure.azcollection.azure_rm_resourcegroup:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
name: "{{ rg_name }}"
location: "{{ location }}"
register: rg
- name: Create a Virtual Network
azure.azcollection.azure_rm_virtualnetwork:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
resource_group: "{{ rg_name }}"
name: "{{ vnet_name }}"
address_prefixes: "10.0.0.0/16"
register: vnet
- name: Create a subnet
azure.azcollection.azure_rm_subnet:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
resource_group: "{{ rg_name }}"
virtual_network_name: "{{ vnet_name }}"
name: "{{ subnet_name }}"
address_prefix: "10.0.0.0/24"
register: subnet
- name: Create a public IP address
azure.azcollection.azure_rm_publicipaddress:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
resource_group: "{{ rg_name }}"
allocation_method: static
name: "{{ vm_name }}-public-ip"
register: public_ip
- name: Create a network security group and configure the security group
azure.azcollection.azure_rm_securitygroup:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
resource_group: "{{ rg_name }}"
name: "{{ vm_name }}-nsg"
rules:
- name: "AllowSSH"
protocol: Tcp
direction: Inbound
priority: 1000
access: Allow
source_address_prefix: "*"
source_port_range: "*"
destination_port_range: "22"
destination_address_prefix: "*"
register: nsg
- name: Create a Virtual Network Interface Card
azure.azcollection.azure_rm_networkinterface:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
resource_group: "{{ rg_name }}"
name: "{{ vm_name }}-nic"
virtual_network: "{{ vnet_name }}"
subnet_name: "{{ subnet_name }}"
public_ip_name: "{{ vm_name }}-public-ip"
security_group: "{{ vm_name }}-nsg"
- name: Create a vm_image
azure.azcollection.azure_rm_virtualmachine:
subscription_id: "{{ subscription_id }}"
tenant: "{{ tenant }}"
client_id: "{{ client_id }}"
secret: "{{ secret }}"
resource_group: "{{ rg_name }}"
name: "{{ vm_name }}"
vm_size: "{{ vm_size }}"
admin_username: "{{ vm_username }}"
admin_password: "{{ vm_password }}"
image:
offer: "CentOS"
publisher: "OpenLogic"
sku: "7.5"
version: "latest"
os_disk_caching: ReadWrite
os_disk_name: "{{ vm_name }}-os-disk"
network_interface_names:
- "{{ vm_name }}-nic"
network_interfaces:
- name: "{{ vm_name }}-nic"
properties:
primary: True
availability_set: null
ssh_public_keys: []
ssh_password_enabled: true
Comments
Post a Comment