{"id":3267,"date":"2022-05-14T14:01:35","date_gmt":"2022-05-14T14:01:35","guid":{"rendered":"https:\/\/exceedthecloud.com\/?p=3267"},"modified":"2022-05-14T15:19:50","modified_gmt":"2022-05-14T15:19:50","slug":"ansible-with-microsoft-azure","status":"publish","type":"post","link":"https:\/\/exceedthecloud.com\/?p=3267","title":{"rendered":"Ansible with Microsoft Azure"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this lab we will deploy, configure, and manage Azure resources by using Ansible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ansible is declarative configuration management software. It relies on a description of the intended configuration applicable to managed computers in the form of playbooks. Ansible automatically applies that configuration and maintains it going forward, addressing any potential discrepancies. Playbooks are formatted by using YAML.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike the majority of other configuration management tools, such as Puppet or Chef, Ansible is agentless, which means that it does not require the installation of any software in the managed machines. Ansible uses SSH to manage Linux servers and Powershell Remoting to manage Windows servers and clients.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to interact with resources other than operating systems (such as, for example, Azure resources accessible via Azure Resource Manager), Ansible supports extensions called modules. Ansible is written in Python so, effectively, the modules are implemented as Python libraries. In order to manage Azure resources, Ansible relies on&nbsp;<a href=\"https:\/\/github.com\/ansible-collections\/azure\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub-hosted modules<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ansible requires that the managed resources are specified in a designated host inventory. Ansible supports dynamic inventories for some systems, including Azure, so that the host inventory is dynamically generated at runtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The lab will consist of the following high-level steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Installing and configuring Ansible on the Azure VM<\/li><li>Downloading Ansible configuration and sample playbook files<\/li><li>Creating and configuring a managed identity in Azure AD<\/li><li>Configuring Azure AD credentials and SSH for use with Ansible<\/li><li>Deploying an Azure VM by using an Ansible playbook<\/li><li>Configuring an Azure VM by using an Ansible playbook<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">After you complete this lab, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Install and configure Ansible on Azure VM<\/li><li>Download Ansible configuration and sample playbook files<\/li><li>Create and configure Azure Active Directory managed identity<\/li><li>Configure Azure AD credentials and SSH for use with Ansible<\/li><li>Deploy an Azure VM by using an Ansible playbook<\/li><li>Configure an Azure VM by using an Ansible playbook<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Review applications required for this lab<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Identify the applications that you\u2019ll use in this lab:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microsoft Edge<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Prepare an Azure subscription<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Identify an existing Azure subscription or create a new one.<\/li><li>Verify that you have a Microsoft account or an Azure AD account with the Owner role in the Azure subscription and the Global Administrator role in the Azure AD tenant associated with the Azure subscription. For details, refer to&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/role-based-access-control\/role-assignments-list-portal\" target=\"_blank\" rel=\"noreferrer noopener\">List Azure role assignments using the Azure portal<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/active-directory\/roles\/manage-roles-portal#view-my-roles\" target=\"_blank\" rel=\"noreferrer noopener\">View and assign administrator roles in Azure Active Directory<\/a>.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Lab1: Deploy, configure, and manage Azure VMs by using Ansible<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this Lab, you will deploy, configure, and manage Azure VMs by using Ansible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 1: Provision an Azure VM serving as the Ansible control node<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will deploy an Azure VM by using Azure CLI and configure it as an Ansible control node that manages your Ansible environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: You will use the Azure VM configured as an Ansible control node to perform Ansible management tasks, including those you performed in the previous tasks of this lab.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Azure portal, in the toolbar, click the&nbsp;<strong>Cloud Shell<\/strong>&nbsp;icon located directly to the right of the search text box.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Alternatively, you can access Cloud Shell directly by navigating to&nbsp;<a href=\"https:\/\/shell.azure.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/shell.azure.com<\/a>.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\" start=\"2\"><li>If prompted to select either&nbsp;<strong>Bash<\/strong>&nbsp;or&nbsp;<strong>PowerShell<\/strong>, select&nbsp;<strong>Bash<\/strong>.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: If this is the first time you are starting&nbsp;<strong>Cloud Shell<\/strong>&nbsp;and you are presented with the&nbsp;<strong>You have no storage mounted<\/strong>&nbsp;message, select the subscription you are using in this lab, and select&nbsp;<strong>Create storage<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\" start=\"3\"><li>From the Bash session in the Cloud Shell pane, run the following to specify the name of the Azure region that will host the resources you deploy in this lab (replace the&nbsp;&lt;Azure_region&gt;&nbsp;placeholder with the name of the Azure region where you intend to deploy resources. Make sure that the name does not contain any spaces, e.g.&nbsp;westeurope):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LOCATION=eastus<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>From the Bash session in the Cloud Shell pane, run the following to create resource groups that will host the Azure VMs you deploy in this lab:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RG1NAME=exceedm14l03rg\naz group create --name $RG1NAME --location $LOCATION\nRG2NAME=exceedm14l03arg\naz group create --name $RG2NAME --location $LOCATION\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"490\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture1.png\" alt=\"\" class=\"wp-image-3268\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture1.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture1-300x236.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to deploy an Azure VM running Ubuntu into the resource group you created in the previous step:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>VM1NAME=exceedm1403vm1\naz vm create \\\n--resource-group $RG1NAME \\\n--name $VM1NAME \\\n--image UbuntuLTS \\\n--authentication-type password \\\n--admin-username azureuser \\\n--admin-password Pa55w.rd1234\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Wait for the deployment to complete before you proceed to the next step. This might take about 2 minutes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Once the provisioning completes, in the JSON-based output, identify the value of the&nbsp;<strong>\u201cpublicIpAddress\u201d<\/strong>&nbsp;property included in the output.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"231\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture2.png\" alt=\"\" class=\"wp-image-3269\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture2.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture2-300x111.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to connect to the newly deployed Azure VM by using SSH:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PIP=$(az vm show --show-details --resource-group $RG1NAME --name $VM1NAME --query publicIps --output tsv)\nssh azureuser@$PIP\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When prompted for confirmation to proceed, type&nbsp;<strong>yes<\/strong>&nbsp;and press the&nbsp;<strong>Enter<\/strong>&nbsp;key and, when prompted to provide the password, type&nbsp;<strong>Pa55w.rd1234<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"478\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture3.png\" alt=\"\" class=\"wp-image-3270\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture3.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture3-300x230.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 2: Install and configure Ansible on an Azure VM<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will install and configure Ansible on the Azure VM you deployed in the previous task.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the newly deployed Azure VM, run the following to update the Advanced Packaging Tool (apt) package list to include the latest version and package details:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"476\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture4.png\" alt=\"\" class=\"wp-image-3271\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture4.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture4-300x229.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following commands to add and install VSCode (whenever you are prompted for confirmation, type&nbsp;<strong>y<\/strong>&nbsp;and press the&nbsp;<strong>Enter<\/strong>&nbsp;key):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install wget<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"265\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture5.png\" alt=\"\" class=\"wp-image-3272\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture5.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture5-300x127.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wget -qO- https:\/\/packages.microsoft.com\/keys\/microsoft.asc | gpg --dearmor &gt; packages.microsoft.gpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo install -o root -g root -m 644 packages.microsoft.gpg \/etc\/apt\/trusted.gpg.d\/\nsudo sh -c 'echo \"deb &#91;arch=amd64,arm64,armhf signed-by=\/etc\/apt\/trusted.gpg.d\/packages.microsoft.gpg] https:\/\/packages.microsoft.com\/repos\/code stable main\" &gt; \/etc\/apt\/sources.list.d\/vscode.list'\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"68\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture6.png\" alt=\"\" class=\"wp-image-3273\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture6.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture6-300x33.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -f packages.microsoft.gpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install apt-transport-https\nsudo apt install code\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"476\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture7.png\" alt=\"\" class=\"wp-image-3274\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture7.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture7-300x229.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to install Ansible and the required Azure modules (<strong>make sure that you run the commands individually, line by line<\/strong>, and, whenever you are prompted for confirmation, type&nbsp;<strong>y<\/strong>&nbsp;and press the&nbsp;<strong>Enter<\/strong>&nbsp;key):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install python3-pip\nsudo -H pip3 install --upgrade pip\nsudo -H pip3 install ansible&#91;azure]\nsudo apt-add-repository --yes --update ppa:ansible\/ansible\nsudo apt install ansible\nsudo ansible-galaxy collection install azure.azcollection\ncurl -O https:\/\/raw.githubusercontent.com\/ansible-collections\/azure\/dev\/requirements-azure.txt\nsudo pip3 install -r requirements-azure.txt\nrm requirements-azure.txt\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Disregard any warnings. If you encounter any errors, rerun the commands.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"378\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture8.png\" alt=\"\" class=\"wp-image-3275\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture8.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture8-300x182.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to install the dnspython package to allow the Ansible playbooks to verify DNS names before deployment:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo -H pip3 install dnspython<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"150\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture9.png\" alt=\"\" class=\"wp-image-3276\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture9.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture9-300x72.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the newly deployed Azure VM, run the following to install the&nbsp;<strong>jq<\/strong>&nbsp;JSON parsing tool (when prompted, type&nbsp;<strong>y<\/strong>&nbsp;and press the&nbsp;<strong>Enter<\/strong>&nbsp;key):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install jq<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"445\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture10.png\" alt=\"\" class=\"wp-image-3277\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture10.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture10-300x214.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to install Azure CLI:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -sL https:\/\/aka.ms\/InstallAzureCLIDeb | sudo bash<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"534\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture11.png\" alt=\"\" class=\"wp-image-3278\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture11.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture11-300x257.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 3: Download Ansible configuration and sample playbook files<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will download from GitHub the Ansible configuration repository along with the sample lab files.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the newly deployed Azure VM, run the following to ensure that&nbsp;<strong>git<\/strong>&nbsp;is installed:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install git<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"315\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture12.png\" alt=\"\" class=\"wp-image-3279\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture12.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture12-300x151.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to clone the PartsUnlimitedMRP repo from GitHub:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/Microsoft\/PartsUnlimitedMRP.git<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: This repository contains playbooks for creating a wide range of resources, some of which we will use in the lab.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"169\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture13.png\" alt=\"\" class=\"wp-image-3280\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture13.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture13-300x81.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 4: Create and configure Azure Active Directory managed identity<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will generate an Azure AD managed identity in order to facilitate non-interactive authentication of Ansible, which is necessary to access Azure resources. You will also assign to the managed identity the Contributor role on the resource group you created in the previous task.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the newly deployed Azure VM, run the following to sign in to the Azure AD tenant associated with your Azure subscription:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>az login<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: If the command fails, rerun the installation of Azure CLI.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Note the code displayed in the output of the previous command and switch to your lab computer. From your lab computer, open another tab in the browser window displaying the Azure portal, navigate to&nbsp;<a href=\"https:\/\/microsoft.com\/devicelogin\" target=\"_blank\" rel=\"noreferrer noopener\">the Microsoft Device Login page<\/a>&nbsp;and, when prompted, enter the code and select&nbsp;<strong>Next<\/strong>.<\/li><li>When prompted, sign in with credentials you are using in this lab and close the browser tab.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"230\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture14.png\" alt=\"\" class=\"wp-image-3281\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture14.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture14-300x111.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Switch back to the Bash session in the Cloud Shell pane. Within the SSH session to the Azure VM configured as the Ansible control node, run the following to generate a system assigned managed identity:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RG1NAME=exceedm14l03rg\nVM1NAME=exceedm1403vm1\naz vm identity assign --resource-group $RG1NAME --name $VM1NAME\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"187\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture15.png\" alt=\"\" class=\"wp-image-3282\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture15.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture15-300x90.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to identify the value of your subscription by running:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SUBSCRIPTIONID=$(az account show --query id --output tsv)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to retrieve the value of the&nbsp;<strong>ID<\/strong>&nbsp;property of the built-in Azure Role Based Access Control Contributor role:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CONTRIBUTORID=$(az role definition list --name \"Contributor\" --query \"&#91;].id\" --output tsv)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to assign the Contributor role on the resource group you created earlier in this lab:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MIID=$(az resource list --name $VM1NAME --query &#91;*].identity.principalId --out tsv)\n\nRG2NAME=exceedm14l03arg\naz role assignment create --assignee \"$MIID\" \\\n--role \"$CONTRIBUTORID\" \\\n--scope \/subscriptions\/$SUBSCRIPTIONID\/resourceGroups\/$RG2NAME\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"296\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture16.png\" alt=\"\" class=\"wp-image-3283\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture16.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture16-300x142.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 5: Configure SSH for use with Ansible<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will configure SSH for use with Ansible.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the newly deployed Azure VM, run the following to generate the key pair (when prompted, press the&nbsp;<strong>Enter<\/strong>&nbsp;key three times to accept the default values of the locations of the files and not to set the passphrase):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t rsa<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"449\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture17.png\" alt=\"\" class=\"wp-image-3284\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture17.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture17-300x216.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to grant read, write, and execute permissions on the&nbsp;<strong>.ssh<\/strong>&nbsp;folder hosting the private key:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 755 ~\/.ssh<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to create as well as set read and write permissions on the&nbsp;<strong>authorized_keys<\/strong>&nbsp;file.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>touch ~\/.ssh\/authorized_keys\nchmod 644 ~\/.ssh\/authorized_keys\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: By providing keys included in this file, you are allowed access without having to provide a password.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to add the password to the&nbsp;<strong>authorized_keys<\/strong>&nbsp;file:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id azureuser@127.0.0.1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When prompted, type&nbsp;<strong>yes<\/strong>&nbsp;and enter the password&nbsp;<strong>Pa55w.rd1234<\/strong>&nbsp;for the&nbsp;<strong>azureuser<\/strong>&nbsp;user account you specified when deploying the third Azure VM earlier in this lab.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"230\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture18.png\" alt=\"\" class=\"wp-image-3285\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture18.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture18-300x111.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to verify that you are not prompted for password:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh 127.0.0.1<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"613\" height=\"649\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture20.png\" alt=\"\" class=\"wp-image-3286\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture20.png 613w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture20-283x300.png 283w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Type&nbsp;<strong>exit<\/strong>&nbsp;and press the&nbsp;<strong>Enter<\/strong>&nbsp;key to terminate the loopback connection you just established.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Establishing passwordless SSH authentication is a critical step for setting up your Ansible environment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 6: Create a web server Azure VM by using an Ansible playbook<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will create an Azure VM hosting a web server by using an Ansible playbook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Now that we have Ansible up and running in the control Azure VM, we can deploy our first playbook in order to create and configure a managed Azure VM. Before deploying the sample playbook, you need to replace the public SSH key included in its content with the key you generated in the previous task.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the Azure VM configured as the Ansible control node, run the following to identify the locally stored public key which you generated in the previous task:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat ~\/.ssh\/id_rsa.pub<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Record the output, including the username at the end of the output string.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"76\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture21.png\" alt=\"\" class=\"wp-image-3287\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture21.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture21-300x37.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to open the&nbsp;<strong>new_vm_web.yml<\/strong>&nbsp;file in the Code text editor:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>code ~\/PartsUnlimitedMRP\/Labfiles\/AZ-400T05-ImplemntgAppInfra\/Labfiles\/ansible\/new_vm_web.yml<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Code editor, if needed, change the name of the region in the&nbsp;dnsname: &#8216;.westeurope.cloudapp.azure.com&#8217;&nbsp;entry to the name of the Azure region you are targeting for deployment.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Make sure that this region matches the Azure region where you created the&nbsp;<strong>exceedm14l03rg<\/strong>&nbsp;resource group.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\" start=\"2\"><li>In the Code editor, change the value of&nbsp;vm_size&nbsp;entry from&nbsp;Standard_A0&nbsp;to&nbsp;Standard_DS1_v2.<\/li><li>In the Code editor, locate the SSH string towards the end of the file, in the&nbsp;key_data&nbsp;entry, delete the existing key value and replace it with the key value that you recorded earlier in this task.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Make sure that the value of&nbsp;admin_username&nbsp;entry that is included in the file matches the user name you used to sign in to the Azure VM hosting the Ansible control system (<strong>azureuser<\/strong>). The same user name must be used in the&nbsp;path&nbsp;entry of&nbsp;ssh_public_keys&nbsp;section.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\" start=\"4\"><li>Within the Code editor interface, click the&nbsp;<strong>\u2026<\/strong>&nbsp;on the top right, and select&nbsp;<strong>Save<\/strong>.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Next, you will deploy an Azure VM into the resource group created at the beginning of the lab. Use the following values for the deployment:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><td>Setting<\/td><td>Value<\/td><\/tr><\/thead><tbody><tr><td>Resource group<\/td><td><strong>exceedm14l03arg<\/strong><\/td><\/tr><tr><td>Virtual network<\/td><td><strong>exceedm1403aVNET<\/strong><\/td><\/tr><tr><td>Subnet<\/td><td><strong>exceedm1403aSubnet<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: The variables can be defined inside of playbooks or can be entered at runtime when invoking the&nbsp;ansible-playbook&nbsp;command by including the&nbsp;&#8211;extra-vars&nbsp;option. As the VM name, use only up to 15 lower case letters and numbers (no hyphens, underscore signs or upper case letters) and ensure it is globally unique, since the same name is used to generate the storage account and the DNS name for the public IP address associated with the corresponding Azure VM.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\" start=\"5\"><li>Run the following to create the virtual network and its subnet into which you will deploy an Azure VM by using an ansible playbook:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RG1NAME=exceedm14l03arg\nLOCATION=$(az group show --resource-group $RG1NAME --query location --output tsv)\nRG2NAME=exceedm14l03arg\nVNETNAME=exceedm1403aVNET\nSUBNETNAME=exceedm1403aSubnet\naz network vnet create \\\n--name $VNETNAME \\\n--resource-group $RG2NAME \\\n--location $LOCATION \\\n--address-prefixes 192.168.0.0\/16 \\\n--subnet-name $SUBNETNAME \\\n--subnet-prefix 192.168.1.0\/24\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"322\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture23.png\" alt=\"\" class=\"wp-image-3288\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture23.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture23-300x155.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to deploy the sample ansible playbook that provisions an Azure VM (<strong>make sure to replace the&nbsp;&lt;VM_name&gt;&nbsp;with the unique VM name you chose<\/strong>):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ansible-playbook ~\/PartsUnlimitedMRP\/Labfiles\/AZ-400T05-ImplemntgAppInfra\/Labfiles\/ansible\/new_vm_web.yml --extra-vars \"vmname=exceedvmans130 resgrp=exceedm14l03arg vnet=exceedm1403aVNET subnet=exceedm1403aSubnet\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Disregard deprecation warning regarding the setting ip_configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: You might receive the following errors if you enter an existing or an invalid VM name:<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>fatal: [localhost]: FAILED! =&gt; {&#8220;changed&#8221;: false, &#8220;failed&#8221;: true, &#8220;msg&#8221;: &#8220;The storage account named storageaccountname is already taken. &#8211; Reason.already_exists&#8221;}. To resolve this, use another name for the Azure VM, since the one you used is not globally unique.<\/li><li>fatal: [localhost]: FAILED! =&gt; {&#8220;changed&#8221;: false, &#8220;failed&#8221;: true, &#8220;msg&#8221;: &#8220;Error creating or updating your-vm-name &#8211; Azure Error: InvalidDomainNameLabel\\nMessage: The domain name label for your VM is invalid. It must conform to the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$.\u201d}. To resolve this issue, use another name for the Azure VM following the required naming convention.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Wait for the deployment to complete. This might take about 3 minutes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"354\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture24.png\" alt=\"\" class=\"wp-image-3289\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture24.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture24-300x170.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to create a new file named&nbsp;<strong>myazure_rm.yml<\/strong>&nbsp;and open it in the Code text editor:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>code .\/myazure_rm.yml<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Within the Code editor interface, paste the following content:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>plugin: azure_rm\ninclude_vm_resource_groups:\n- exceedm14l03arg\nauth_source: msi\n\nkeyed_groups:\n- prefix: tag\n  key: tags\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Within the Code editor interface, click the&nbsp;<strong>\u2026<\/strong>&nbsp;on the top right, and select&nbsp;<strong>Save<\/strong>.<\/li><li>Back in the Bash session in the Cloud Shell pane, within the SSH session to the Azure VM configured as the Ansible control node, run the following to perform a ping test, verifying that the dynamic inventory file includes the newly deployed Azure VM:<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ansible --user azureuser --private-key=\/home\/azureuser\/.ssh\/id_rsa all -m ping -i .\/myazure_rm.yml<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>When prompted whether you want to continue connecting, type&nbsp;<strong>yes<\/strong>&nbsp;and press the&nbsp;<strong>Enter<\/strong>&nbsp;key.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: The output should resemble the following:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exceedm1403vm2_5444 | SUCCESS =&gt; {\n    \"ansible_facts\": {\n        \"discovered_interpreter_python\": \"\/usr\/bin\/python\"\n    },\n    \"changed\": false,\n    \"ping\": \"pong\"\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: The first time you run the command you will have to acknowledge the authenticity of the target VMs, by typing&nbsp;<strong>yes<\/strong>&nbsp;and pressing the&nbsp;<strong>Enter<\/strong>&nbsp;key.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"261\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture25.png\" alt=\"\" class=\"wp-image-3290\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture25.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture25-300x125.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Task 7: Configure an Azure VM by using an Ansible playbook<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this task, you will run another Ansible playbook, this time to configure the newly deployed Azure VM. You will use a playbook that installs a software package httpd and downloads an HTML page from a GitHub repository. Once this is completed, you will have a fully functional Web server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: We will use the sample playbook&nbsp;<strong>~\/PartsUnlimitedMRP\/Labfiles\/AZ-400T05-ImplemntgAppInfra\/Labfiles\/ansible\/httpd.yml<\/strong>. We will use the variable&nbsp;<strong>vmname<\/strong>&nbsp;in order to modify the hosts parameter of the playbook that defines which host (out of the ones returned by the dynamic inventory script) the playbook will target.<\/p>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>In the Bash session in the Cloud Shell pane, within the SSH session to the Azure VM configured as the Ansible control node, run the following to identify the public IP address of the newly deployed Azure VM (<strong>make sure to replace the&nbsp;&lt;VM_name&gt;&nbsp;placeholder with the name you assigned to the newly provisioned Azure VM<\/strong>):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RGNAME='exceedm14l03arg'\nVMNAME='exceedvmans130'\nPIP=$(az vm show --show-details --resource-group $RGNAME --name $VMNAME --query publicIps --output tsv)\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"89\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture26.png\" alt=\"\" class=\"wp-image-3291\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture26.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture26-300x43.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to verify that the newly deployed Azure VM is currently not running any web service (where the&nbsp;&lt;IP_address&gt;&nbsp;placeholder represents the public IP address assigned to the network adapter of the Azure VM you provisioned in the previous task):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl http:\/\/$PIP<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Verify that the response is in the format&nbsp;curl: (7) Failed to connect to 52.186.157.26 port 80: Connection refused.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"110\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture27.png\" alt=\"\" class=\"wp-image-3292\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture27.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture27-300x53.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Run the following to install the HTTP service by using the Ansible playbook (where the&nbsp;&lt;VM_name&gt;&nbsp;placeholder represents the name of the VM you provisioned in the previous task):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ansible-playbook --user azureuser --private-key=\/home\/azureuser\/.ssh\/id_rsa -i .\/myazure_rm.yml ~\/PartsUnlimitedMRP\/Labfiles\/AZ-400T05-ImplemntgAppInfra\/Labfiles\/ansible\/httpd.yml --extra-vars \"vmname=exceedvmans130*\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Make sure to include the trailing asterisk (<strong>*<\/strong>) following the Azure VM name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: Wait for the installation to complete. This should take less than a minute.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"251\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture28.png\" alt=\"\" class=\"wp-image-3293\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture28.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture28-300x121.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\" type=\"1\"><li>Once the installation completes, run the following to verify that the newly deployed Azure VM is now running a web service (where the&nbsp;&lt;IP_address&gt;&nbsp;placeholder represents the public IP address assigned to the network adapter of the Azure VM you provisioned in the previous task):<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl http:\/\/$PIP<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: The output should have the following content:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CodeCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n &lt;html lang=\"en\"&gt;\n     &lt;head&gt;\n         &lt;meta charset=\"utf-8\"&gt;\n         &lt;title&gt;Hello World&lt;\/title&gt;\n     &lt;\/head&gt;\n     &lt;body&gt;\n         &lt;h1&gt;Hello World&lt;\/h1&gt;\n         &lt;p&gt;\n             &lt;br&gt;This is a test page\n             &lt;br&gt;This is a test page\n             &lt;br&gt;This is a test page\n         &lt;\/p&gt;\n     &lt;\/body&gt;\n &lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"387\" src=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture29.png\" alt=\"\" class=\"wp-image-3294\" srcset=\"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture29.png 624w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture29-300x186.png 300w, https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/Picture29-80x50.png 80w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Review<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this lab, you learned how to deploy, configure, and manage Azure resources by using Ansible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark class=\"kt-highlight\">Reminder: Don\u2019t forget to delete or shutdown all unused Azure resources after your labs for cost saving<\/mark><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lab we will deploy, configure, and manage Azure resources by using Ansible. Ansible is declarative configuration management software. It relies on a description of the intended configuration applicable to managed computers in the form of playbooks. Ansible automatically &hellip; <a href=\"https:\/\/exceedthecloud.com\/?p=3267\">Continued<\/a><\/p>\n","protected":false},"author":1,"featured_media":3296,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"kt_blocks_editor_width":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[16,98,4],"tags":[113,8,114,89,105,110],"class_list":["post-3267","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-powershell","category-practical-labs-series","tag-ansible","tag-azure","tag-bash","tag-cloud-shell","tag-devops","tag-github"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/exceedthecloud.com\/wp-content\/uploads\/2022\/05\/images.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/3267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3267"}],"version-history":[{"count":2,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/3267\/revisions"}],"predecessor-version":[{"id":3328,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/posts\/3267\/revisions\/3328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=\/wp\/v2\/media\/3296"}],"wp:attachment":[{"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/exceedthecloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}