Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Created February 25, 2022 09:54
Show Gist options
  • Select an option

  • Save dmccuk/9705c86f688d78594281bdf16fee8388 to your computer and use it in GitHub Desktop.

Select an option

Save dmccuk/9705c86f688d78594281bdf16fee8388 to your computer and use it in GitHub Desktop.

Build Azure Instances using Ansible

Subscribe To Me On YouTube: https://bit.ly/lon_sub

Links:

  1. https://azure.microsoft.com/en-gb/free/?ref=microsoft.com
  2. https://docs.microsoft.com/en-us/azure/developer/ansible/install-on-linux-vm
  3. https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt
  4. https://docs.microsoft.com/en-us/azure/developer/ansible/create-ansible-service-principal
  5. https://docs.ansible.com/ansible/latest/scenario_guides/guide_azure.html

Actual commands:

sudo apt-get update
sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg -y

curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg REDIRECT /dev/null
** Replace REDIRECT with chevron for greater than (YT doesn't like chevrons!) ** 

AZ_REPO=$(lsb_release -cs)
echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list
sudo apt-get update
sudo apt-get install azure-cli
sudo apt install ansible
sudo apt install python3-pip
pip3 install ansible[azure] --user
az login
az ad sp create-for-rbac --name ansible
az role assignment create --assignee appID --role Contributor
az account show --query '{tenantId:tenantId,subscriptionid:id}';
az ad sp list --display-name ansible --query '{clientId:[0].appId}'
vi ~/.azure/credentials
ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth"
ansible localhost -m azure_rm_resourcegroup -a "name=test1234 location=uksouth state=absent"
mkdir azure
cd azure
vi azure_vm.yml
ansible localhost -m azure_rm_resourcegroup -a "name=Testing location=uksouth"
ansible-playbook azure_vm.yml
cat azure_vm.yml
ssh youtubedemo@IP_ADDRESS

The Playbook:

- name: create resources to build a VM
  hosts: localhost
  tasks:

  - name: Create storage account
    azure_rm_storageaccount:
      resource_group: Testing
      name: 123ansibledemo123
      account_type: Standard_LRS

  - name: Create virtual network
    azure_rm_virtualnetwork:
      resource_group: Testing
      name: testvn001
      address_prefixes: "10.10.0.0/16"

  - name: Add subnet
    azure_rm_subnet:
      resource_group: Testing
      name: subnet001
      address_prefix: "10.10.0.0/24"
      virtual_network: testvn001

  - name: Create public ip
    azure_rm_publicipaddress:
      resource_group: Testing
      allocation_method: Static
      name: publicip001

  - name: Create security group that allows SSH
    azure_rm_securitygroup:
      resource_group: Testing
      name: secgroup001
      rules:
        - name: SSH
          protocol: Tcp
          destination_port_range: 22
          access: Allow
          priority: 101
          direction: Inbound

  - name: Create NIC
    azure_rm_networkinterface:
      resource_group: Testing
      name: testnic001
      virtual_network: testvn001
      subnet: subnet001
      public_ip_name: publicip001
      security_group: secgroup001

  - name: Create virtual machine
    azure_rm_virtualmachine:
      resource_group: Testing
      name: testvm001
      vm_size: Standard_B1s
      storage_account: 123ansibledemo123
      storage_container: testvm001
      storage_blob: testvm001.vhd
      admin_username: youtubedemo
      admin_password: Password123123
      network_interfaces: testnic001
      image:
        offer: CentOS
        publisher: OpenLogic
        sku: '7.1'
        version: latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment