Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to automate VM creation on Azure with Ansible CLI

June 15, 2023
Deepankar Jain
Related topics:
Automation and management
Related products:
Red Hat Ansible Automation PlatformRed Hat Ansible Automation Platform on Microsoft Azure

    In this article, we will demonstrate how to use the Red Hat Ansible Automation Platform command-line interface (CLI) to create a virtual machine on Microsoft Azure. We will walk you through the steps required to get started with Ansible Automation Platform and Azure, including setting up the necessary resources and creating a VM using the Azure module and Ansible Automation Platform.

    This series covers the end-to-end process of creating a Virtual Machine(VM) on Azure using Ansible Automation Platform. This 3-part series includes:

    • Part 1: How to automate VM creation on Azure with Ansible CLI
    • Part 2: How to use Ansible to create a VM on Azure
    • Part 3: How to use Ansible to create a VM on Azure via workflow

    By the end of this article, you'll have a better understanding of how to use Ansible Automation Platform CLI to manage VMs and how this streamlines your infrastructure management workflows.

    Prerequisites

    Before you begin this tutorial, complete the following:

    • Make sure Ansible Automation Platform is installed on your system.
    • Create a Microsoft Azure account.
    • Install Ansible content collection for Azure on your system.

    How to use the Ansible CLI to create a VM

    Follow these steps to create a virtual machine using Ansible Automation Platform CLI:

    • Create a service principal and give permissions in Azure.
    • Generate the client secret for service principal.
    • You should now have a subscriptionid, tenantid, clientid and client secret that you can use to access your Azure Account and launch a VM.
    • Open any text editor on your local machine and copy the following yml into it:
    --- # 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
    
    
    • Save and close the file.
    • Open the terminal in the directory where the file is located on your local machine.
    • Run the following command:
      ansible-playbook <filename>.yml

      This is the output:

      ansible-playbook -i inventory azure_cli.yml 
      
      PLAY [Create a Virtual Machine on Azure Using Ansible] ************************************************************************************************************************************************************
      
      TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
      ok: [localhost]
      
      TASK [Create a Resource Group] ************************************************************************************************************************************************************************************
      changed: [localhost]
      
      TASK [Create a Virtual Network] ***********************************************************************************************************************************************************************************
      changed: [localhost]
      
      TASK [Create a subnet] ********************************************************************************************************************************************************************************************
      changed: [localhost]
      
      TASK [Create a public IP address] *********************************************************************************************************************************************************************************
      changed: [localhost]
      
      TASK [Create a network security group and configure the security group] *******************************************************************************************************************************************
      changed: [localhost]
      
      TASK [Create a Virtual Network Interface Card] ********************************************************************************************************************************************************************
      [DEPRECATION WARNING]: Setting ip_configuration flatten is deprecated and will be removed. Using ip_configurations list to define the ip configuration. This feature will be removed in version [2, 9]. 
      Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
      changed: [localhost]
      
      TASK [Create a vm_image] ******************************************************************************************************************************************************************************************
      [WARNING]: Both option network_interface_names and its alias network_interfaces are set.
      changed: [localhost]
      
      PLAY RECAP ********************************************************************************************************************************************************************************************************
      localhost                  : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
      
      Figure 1 shows the the Microsoft Azure VM.
      A screenshot of the Microsoft Azure virtual machine.
      Figure 1: The Microsoft Azure virtual machine.

    What’s next?

    In this article, we demonstrated how to create a VM using Ansible Automation Platform. If you followed this step-by-step guide, you should now have a good understanding of how to use Ansible Automation Platform to automate the creation of a VM.

    In our next article in this series, we will explore how Ansible Automation Platform further eases the process of creating VMs by defining infrastructure as code, tracking infrastructure changes, and enforcing compliance policies.

    Get started with Ansible Automation Platform by exploring interactive hands-on labs. Download Ansible Automation Platform at no cost and begin your automation journey. You can refer to An IT executive's guide to automation e-book for a better understanding of the Ansible Automation Platform.

    Last updated: January 11, 2024

    Related Posts

    • Deallocate an Azure VM Using the Azure CLI on RHEL

    • Create an Azure Red Hat OpenShift cluster in less than 5 minutes

    • How to create an EC2 instance in AWS using Ansible automation

    • 5 examples of security automation with Ansible

    Recent Posts

    • MCP servers vs. skills: Choosing the right context for your AI

    • How to route external and local LLMs with Models-as-a-Service

    • Protect data offloaded to GPU-accelerated environments with OpenShift sandboxed containers

    • Case study: Measuring energy efficiency on the x64 platform

    • How to prevent AI inference stack silent failures

    What’s up next?

    Why automate? Which tool do I use? This e-book dives into various automation options, how they work, what to automate, and the benefits of each tool.

    Download the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.