Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security & DevOps Insights

  • Home
  • About
  • Advertise With US
  • Reviews
  • Contact
  • Toggle search form
Home » Configuration Management Tool » Install and configure Ansible on Azure Virtual Machine
  • LAPS PAssword Not Showing Up
    LAPS password not showing up in GUI Windows
  • Delete AWS EBS Volume
    How to delete an Elastic Block Store Volume on AWS AWS/Azure/OpenShift
  • 111615smarthomeNG
    Unspecified GSS failure: How to fix Clock skew too great Web Server
  • filezilla 1
    Connect to a FileZilla Server: How to install and configure a FileZilla Server on Windows Windows Server
  • finalelastic
    How to Install and Configure Elasticsearch on Linux  Linux
  • Synology 1
    Synology NAS brute-force attack: Employ Synology Best Practice to remediate StealthWorker Botnet attack Security | Vulnerability Scans and Assessment
  • Download and apply Windows ADK Patch
    How to download and install the Windows ADK Patches Windows
  • featuredpkg
    How to solve /var/lib/dpkg/lock Error in Ubuntu Linux Linux

Install and configure Ansible on Azure Virtual Machine

Posted on 02/09/202115/09/2023 Christian By Christian No Comments on Install and configure Ansible on Azure Virtual Machine
ansiblebanner

Ansible is an open-source product that automates cloud provisioning, configuration management, and application deployments. When using Ansible you can easily provision virtual machines, containers, and networks and complete cloud infrastructures. Also, Ansible it allows you to automate the deployment and configuration of resources in your environment. In this article, you will be learning what is an Ansible understanding the step by step on how to install and configure Ansible on Azure Virtual Machine. Kindly refer to some of the related guides: How to install Ansible on Windows with Cygwin, how to install and configure Ansible on Ubuntu, how to configure a remote server (windows) to Support Ansible, and how to install Kerberos packages in Windows via Cygwin.

Things needed to achieve the success of configuring Ansible in Azure VM. Please see How To Check the Kernel Version in Linux / Ubuntu / CentOS.

  • We will be creating a resource group. Create a CentOS virtual machine.
  • Install Ansible on the virtual machine.
  • Connect to the virtual machine via SSH
  • Configure Ansible on the virtual machine.
  • Also you will need to create an Azure service principal, where you will be making note of the following values: appId, displayName, password, and tenant.

Create an Azure Resource Group

Kindly log on to your Microsoft Portal account following this link https://portal.azure.com to create a resource group. At the top left-hand corner, click on the “Icon” and select “Create Resource Group”

portal
portal
azure-portal
azure portal

Then type in the following details:

  • Resource group name
  • Select region
  • Then click Review + Create
resource

After creating your resource group, click to open your created resource group, you should have a similar interface below

Webp.net-resizeimage

Also see how to Automate Infrastructure Deployments in the Cloud with Ansible and Azure Pipelines, What to note when settings up Ansible to work with Kerberos, and how to fix Ansible error: Server unreachable, ssl: auth method ssl requires a password.

Create a CentOS Azure Virtual Machine for Ansible

In this case, we will be using the Azure CLI interface from the screenshot and below also using the following commands below with Bash Scripting

az vm create \
--resource-group startAnsible \
--name startAnsible-vm \
--image OpenLogic:CentOS:7.7:latest \
--admin-username azureuser \
--admin-password password@12345
bash

Here we have successfully created the virtual machine for Ansible, using the Bash command, and we now have our Public IP address and Private IP address, which we will be used in connecting to ssh

Connect to the virtual machine via SSH

Using the SSH command, let connect our virtual machine’s public IP address.

  • ssh [email protected]
  • Press enter key
  • Type in your create password
  • Press enter key
  • Then you will be logged in.
bash2

Configure Ansible On The Virtual Machine

Run the following commands to configure Ansible 2.9 on Centos using bash shell in your Azure portal.

sudo yum update -y
sudo yum install -y python3-pip
sudo pip3 install --upgrade pip
pip3 install "ansible==2.9.17"
pip3 install ansible[azure]

Let Configure Ansible on CertOs

Let’s run the following commands to configure Ansible on Centos using the bash shell in your Azure portal.

sudo yum update -y
sudo yum install -y python3-pip
sudo pip3 install --upgrade pip
ansible-galaxy collection install azure.azcollection
wget https://raw.githubusercontent.com/ansible-collections/azure/dev/requirements-azure.txt
sudo pip3 install -r requirements-azure.txt

Note: The ansible control node requires Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed. Ansible 4.0.0 and ansible-core 2.11 have a soft dependency on Python 3.8, but function with lower versions. However, Ansible 5.0.0 and ansible-core 2.12 will require 3.8 and newer.

Let Create Azure credentials

In other to create Azure Ansible credentials, you need the following information:

  • Azure subscription ID and tenant ID
  • The service principal applicationID, and secret.

Create and Configure Ansible Credentials File

Let configure and create local credentials to the Ansible. This is for security reasons, credential files should only be used in development environments, to achieve this you need to be connected to the host virtual machine, create and open a file named Credentials by running the following commands in your azure bash environment.

mkdir ~/.azure
vi ~/.azure/credentials

Create Azure Service Principal

We will be creating a service principal using the bash in the Azure portal

  • Login to your Azure portal account
  • Click on the CLI terminal at the top of the screen
  • Select bash, and type the following command: az ad sp create-for-rbac –name ansible
  • Copy and save the data’s provided for reference use
bash3

Create A Role ID

Create a role, using the following commands in your bash environment in the Azure portal

az role assignment create --assignee <appId number> --role contributor
  • Press enter key
  • Copy and save the data’s provided for reference use
bash4

hence insert the following lines into the file by replacing the placeholders with the service principal values provided

[default]
subscription_id=<your-subscription_id>
client_id=<security-principal-appid> 
secret=<security-principal-password>  
tenant=<security-principal-tenant>
bash5

After replacing the placeholders with the service principal values provided in the vi editor via the bash environment, save and close your job following the below information

Press Esc to enter Command mode, and then type :wq to write and quit the file.

Usecase 1 – Create a resource in Azure using Ansible

Let create a resource in Azure by run testing our Ansible Installation within the new Ansible configuration that we just created. Check to confirm ansible version installation. ansible --version


isaac@Azure:~$ ansible --version
ansible 2.10.2
  config file = None
  configured module search path = ['/home/isaac/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible/lib/python3.7/site-packages/ansible
  executable location = /opt/ansible/bin/ansible
  python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]

Let create a resource in Azure using the Ansible installation

- name: Create Azure VM
  hosts: localhost
  connection: local
  vars:
    resource_group: "{{ resource_group_name }}"
    resource_group_secondary: createvmsubnetin2ndrg2
    vm_name: testvm
    location: eastus

From the cloud shell, run this command to deploy resources in Azure using Ansible-playbook

ansible-playbook ./create_virtualmachine_with_subnet_in_different_resource_group.yml -e "resource_group_name=ansible-test-rg01"
Webp.net-resizeimage-2-1

With Ansible playbook, we have been able to deploy resources in Azure such as:

  • Create a resource group
  • Create a Virtual machine
  • Deploy network interface
  • Deploy network security group
  • Public IP Address
  • Create a storage account

Usecase 2- Patch and Install Windows Updates With Ansible

Let patch and install windows updates using our Ansible Installation. Check to confirm that you have successfully install Ansible by checking the ansible version installation. ansible --version


isaac@Azure:~$ ansible --version
ansible 2.10.2
  config file = None
  configured module search path = ['/home/isaac/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/ansible/lib/python3.7/site-packages/ansible
  executable location = /opt/ansible/bin/ansible
  python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]

Here I’ll show you a playbook that installs Windows updates on a managed Windows machines without needing to use WSUS. The playbook installs Security, Definition, critical, and rollup updates and restarts the machine if needed.

Below you can see the code to patch and install windows updates with Ansible. Run the following code on your CLI

- name: "Windows Updates"
  hosts: windows
  tasks:
    - name: Install Windows updates
      win_updates:
        category_names:
         - SecurityUpdates
         - CriticalUpdates
         - UpdateRollups
         - DefinitionUpdates 
        reboot: yes

Note: To install all the update types use the following list.

 - SecurityUpdates
         - CriticalUpdates
         - UpdateRollups
         - DefinitionUpdates
         - updates
         - FeaturePacks
         - Servicepacks
         - Tools
         - Application

We have seen the step by steps on how to Install and configure Ansible on Azure Virtual Machine, create a resource group, deploy a CentOS virtual machine, install Ansible on the virtual machine, able to connect to the virtual machine via SSH. Configure Ansible on the virtual machine. Lastly able to create an Azure service principal with the following values: appId, displayName, password, and tenant.

Rate this post

Thank you for reading this post. Kindly share it with others.

  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Pocket (Opens in new window) Pocket
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Threads (Opens in new window) Threads
  • Share on Nextdoor (Opens in new window) Nextdoor
Configuration Management Tool Tags:Azure

Post navigation

Previous Post: Service Principal Name: How to add or reset and delete SPNs
Next Post: Create an App Service Plan with Continuous Deployment to deploy a .NET Application from GitHub

Related Posts

  • Screenshot 2020 05 14 at 19.43.13
    Repo: How to setup Chef Repository Configuration Management Tool
  • Installing Jenkins
    How to Install and Configure Jenkins on Windows 11 Configuration Management Tool
  • 1 ifg3ir3l 8ejus3pueqt0a
    Fix cannot find KDC for realm while getting initial credentials and kinit configuration file does not specify default realm Configuration Management Tool
  • images 7
    The plugin filter file/etc/ansible/plugin_filters(yml) does not exist – Skipping Configuration Management Tool
  • ansible vault
    Concept of Ansible on Windows using Cygwin Configuration Management Tool
  • Create S3 Bucket with Terraform
    Create an S3 Bucket with Terraform AWS/Azure/OpenShift

More Related Articles

Screenshot 2020 05 14 at 19.43.13 Repo: How to setup Chef Repository Configuration Management Tool
Installing Jenkins How to Install and Configure Jenkins on Windows 11 Configuration Management Tool
1 ifg3ir3l 8ejus3pueqt0a Fix cannot find KDC for realm while getting initial credentials and kinit configuration file does not specify default realm Configuration Management Tool
images 7 The plugin filter file/etc/ansible/plugin_filters(yml) does not exist – Skipping Configuration Management Tool
ansible vault Concept of Ansible on Windows using Cygwin Configuration Management Tool
Create S3 Bucket with Terraform Create an S3 Bucket with Terraform AWS/Azure/OpenShift

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

VEEAMLEGEND

vexpert-badge-stars-5

Virtual Background

GoogleNews

Categories

veeaam100

sysadmin top30a

  • LAPS PAssword Not Showing Up
    LAPS password not showing up in GUI Windows
  • Delete AWS EBS Volume
    How to delete an Elastic Block Store Volume on AWS AWS/Azure/OpenShift
  • 111615smarthomeNG
    Unspecified GSS failure: How to fix Clock skew too great Web Server
  • filezilla 1
    Connect to a FileZilla Server: How to install and configure a FileZilla Server on Windows Windows Server
  • finalelastic
    How to Install and Configure Elasticsearch on Linux  Linux
  • Synology 1
    Synology NAS brute-force attack: Employ Synology Best Practice to remediate StealthWorker Botnet attack Security | Vulnerability Scans and Assessment
  • Download and apply Windows ADK Patch
    How to download and install the Windows ADK Patches Windows
  • featuredpkg
    How to solve /var/lib/dpkg/lock Error in Ubuntu Linux Linux

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,836 other subscribers
  • RSS - Posts
  • RSS - Comments
  • About
  • Authors
  • Write for us
  • Advertise with us
  • General Terms and Conditions
  • Privacy policy
  • Feedly
  • Telegram
  • Youtube
  • Facebook
  • Instagram
  • LinkedIn
  • Tumblr
  • Pinterest
  • Twitter
  • mastodon

Tags

AWS Azure Bitlocker Microsoft Windows PowerShell WDS Windows 10 Windows 11 Windows Deployment Services Windows Server 2016

Copyright © 2025 TechDirectArchive

 

Loading Comments...
 

You must be logged in to post a comment.