{"id":5948,"date":"2019-02-20T09:25:40","date_gmt":"2019-02-20T09:25:40","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5948"},"modified":"2021-11-16T11:45:51","modified_gmt":"2021-11-16T11:45:51","slug":"working-with-azure-using-ansible","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/working-with-azure-using-ansible\/","title":{"rendered":"Working with Azure using Ansible"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"215\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/03\/Working-with-Azure.png\" alt=\"\" class=\"wp-image-6005\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/03\/Working-with-Azure.png 400w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2019\/03\/Working-with-Azure-300x161.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>Ansible includes a suite of modules for interacting with &#8220;Azure Resource Manager&#8221;, giving you the tools to easily create and orchestrate infrastructure on the Microsoft Azure Cloud.<\/p>\n\n\n\n<p><strong>Step 1 &#8211; Requirements &#8211; Azure SDK in Ansible Control Server<\/strong><br> Using the Azure Resource Manager modules requires having specific Azure SDK modules installed on the host running Ansible.&#8217;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">$ pip install <span class=\"hljs-string\">'ansible&#91;azure]'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note &#8211; You can also directly run Ansible in Azure Cloud Shell, where Ansible is pre-installed.<\/p>\n\n\n\n<p><strong>Step 2 &#8211; Authenticating with Azure<\/strong><br> Using the Azure Resource Manager modules requires authenticating with the Azure API. You can choose from two authentication strategies:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Active Directory Username\/Password<\/li><li>Active Directory Username\/Password<\/li><\/ul>\n\n\n\n<p><strong>Step 3 &#8211; Setting up &#8220;Service Principal Credentials&#8221;<\/strong><\/p>\n\n\n\n<p>Please follow this steps &#8211; https:\/\/docs.microsoft.com\/en-gb\/azure\/active-directory\/develop\/howto-create-service-principal-portal<\/p>\n\n\n\n<p>After stepping through the tutorial you will have:<\/p>\n\n\n\n<p><strong>Client ID &#8211; <\/strong>Your Client ID, which is found in the \u201cclient id\u201d box in the \u201cConfigure\u201d page of your application in the Azure portal<\/p>\n\n\n\n<p><strong>Secret key &#8211; <\/strong>Your Secret key, generated when you created the application. You cannot show the key after creation. If you lost the key, you must create a new one in the \u201cConfigure\u201d page of your application.<\/p>\n\n\n\n<p><strong>tenant ID &#8211;<\/strong> And finally, a tenant ID. It\u2019s a UUID (e.g. ABCDEFGH-1234-ABCD-1234-ABCDEFGHIJKL) pointing to the AD containing your application. You will find it in the URL from within the Azure portal, or in the \u201cview endpoints\u201d of any given URL.<\/p>\n\n\n\n<p><strong>Azure Subscription Id &#8211;<\/strong><\/p>\n\n\n\n<p><strong>Step 4 &#8211;  Providing Credentials to Azure Modules<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Method &#8211; 1 &#8211;    <\/strong>Ansible Tower, you will most likely want to use environment variables. To pass service principal credentials via the environment, define the following variables:<br> AZURE_CLIENT_ID<br> AZURE_SECRET<br> AZURE_SUBSCRIPTION_ID<br> AZURE_TENANT<\/p>\n\n\n\n<p><strong>Method &#8211; 2 &#8211; <\/strong>A file within your home directory. The modules will look for credentials in $HOME\/.azure\/credentials [default]<br> subscription_id=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<br> client_id=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<br> secret=xxxxxxxxxxxxxxxxx<br> tenant=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/p>\n\n\n\n<p><strong>Method &#8211; 3 &#8211; <\/strong>Pass credentials as parameters to a task within a playbook. If you wish to pass credentials as parameters to a task, use the following parameters for service principal:<br> client_id<br> secret<br> subscription_id<br> tenant<\/p>\n\n\n\n<p><strong>Step 4 &#8211;  <\/strong>Creating a Virtual Machine with Default Options<br> If you simply want to create a virtual machine without specifying all the details, you can do that as well. The only caveat is that you will need a virtual network with one subnet already in your resource group. Assuming you have a virtual network already with an existing subnet, you can run the following to create a VM:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">azure_rm_virtualmachine:\n  resource_group: Testing\n  <span class=\"hljs-attr\">name<\/span>: testvm10\n  <span class=\"hljs-attr\">vm_size<\/span>: Standard_D1\n  <span class=\"hljs-attr\">admin_username<\/span>: chouseknecht\n  <span class=\"hljs-attr\">ssh_password_enabled<\/span>: <span class=\"hljs-literal\">false<\/span>\n  <span class=\"hljs-attr\">ssh_public_keys<\/span>: <span class=\"hljs-string\">\"{{ ssh_keys }}\"<\/span>\n  <span class=\"hljs-attr\">image<\/span>:\n    offer: CentOS\n    <span class=\"hljs-attr\">publisher<\/span>: OpenLogic\n    <span class=\"hljs-attr\">sku<\/span>: <span class=\"hljs-string\">'7.1'<\/span>\n    <span class=\"hljs-attr\">version<\/span>: latest<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Step 5 &#8211;  Creating Individual Components<\/strong><br> An Azure module is available to help you create a storage account, virtual network, subnet, network interface, security group and public IP. Here is a full example of creating each of these and passing the names to the azure_rm_virtualmachine module at the end:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">- name: Create storage account\n  azure_rm_storageaccount:\n    resource_group: Testing\n    name: testaccount001\n    account_type: Standard_LRS\n\n- name: Create virtual network\n  azure_rm_virtualnetwork:\n    resource_group: Testing\n    name: testvn001\n    address_prefixes: <span class=\"hljs-string\">\"10.10.0.0\/16\"<\/span>\n\n- name: Add subnet\n  azure_rm_subnet:\n    resource_group: Testing\n    name: subnet001\n    address_prefix: <span class=\"hljs-string\">\"10.10.0.0\/24\"<\/span>\n    virtual_network: testvn001\n\n- name: Create <span class=\"hljs-keyword\">public<\/span> ip\n  azure_rm_publicipaddress:\n    resource_group: Testing\n    allocation_method: <span class=\"hljs-keyword\">Static<\/span>\n    name: publicip001\n\n- name: Create security group that allows SSH\n  azure_rm_securitygroup:\n    resource_group: Testing\n    name: secgroup001\n    rules:\n      - name: SSH\n        protocol: Tcp\n        destination_port_range: <span class=\"hljs-number\">22<\/span>\n        access: Allow\n        priority: <span class=\"hljs-number\">101<\/span>\n        direction: Inbound\n\n- name: Create NIC\n  azure_rm_networkinterface:\n    resource_group: Testing\n    name: testnic001\n    virtual_network: testvn001\n    subnet: subnet001\n    public_ip_name: publicip001\n    security_group: secgroup001\n\n- name: Create virtual machine\n  azure_rm_virtualmachine:\n    resource_group: Testing\n    name: testvm001\n    vm_size: Standard_D1\n    storage_account: testaccount001\n    storage_container: testvm001\n    storage_blob: testvm001.vhd\n    admin_username: admin\n    admin_password: Password!\n    network_interfaces: testnic001\n    image:\n      offer: CentOS\n      publisher: OpenLogic\n      sku: <span class=\"hljs-string\">'7.1'<\/span>\n      version: latest<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_54295\"><iframe loading=\"lazy\"  id=\"_ytid_61407\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_54295\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Ansible includes a suite of modules for interacting with &#8220;Azure Resource Manager&#8221;, giving you the tools to easily create and orchestrate infrastructure on the Microsoft Azure Cloud. Step 1 &#8211;&#8230; <\/p>\n","protected":false},"author":1,"featured_media":6005,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5038],"tags":[639,5197],"class_list":["post-5948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","tag-ansible","tag-azure"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5948"}],"version-history":[{"count":6,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5948\/revisions"}],"predecessor-version":[{"id":25525,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5948\/revisions\/25525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/6005"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}