%3CLINGO-SUB%20id%3D%22lingo-sub-1062449%22%20slang%3D%22en-US%22%3EConfigure%20Ansible%20to%20use%20a%20Managed%20Identity%20with%20Azure%20Dynamic%20Inventory%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1062449%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20again%2C%20Chris%20Wallen%20here%20and%20in%20this%20post%2C%20I%E2%80%99m%20going%20to%20show%20you%20how%20to%20use%20a%20managed%20identity%20to%20manage%20resources%20using%20Azure%20dynamic%20inventory%20with%20Ansible.%20If%20you%E2%80%99re%20not%20familiar%20with%20managed%20identities%2C%20see%20this%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eoverview%20of%20managed%20identities%20for%20Azure%20resources%3C%2FA%3E.%20Basically%2C%20they%20just%20provide%20a%20method%20of%20authenticating%20to%20Azure%20without%20the%20need%20to%20store%20credentials%20in%20code%20or%20on%20any%20local%20resource.%20Now%2C%20before%20we%20get%20into%20the%20technical%20details%2C%20let%E2%80%99s%20go%20over%20the%20different%20sources%20of%20Azure%20credentials%20that%20Ansible%20can%20use%20to%20authenticate%20when%20using%20dynamic%20inventory%3A%3C%2FP%3E%0A%3CP%3EWhen%20using%20dynamic%20inventory%2C%20you%20can%20use%20any%20of%20the%20following%20methods%20to%20provide%20your%20Azure%20credentials%20to%20Ansible%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EStoring%20them%20in%20a%20plain-text%20credential%20file%3C%2FLI%3E%0A%3CLI%3ESetting%20Environment%20variables%20that%20store%20the%20credentials%3C%2FLI%3E%0A%3CLI%3EUsing%20the%20currently%20logged%20in%20Azure%20CLI%20credentials%3C%2FLI%3E%0A%3CLI%3EUsing%20a%20managed%20identity%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EObviously%2C%20the%20first%20option%20is%20the%20least%20secure%20as%20your%20credentials%20are%20stored%20in%20a%20flat-file%20that%20could%20be%20read%20by%20anyone%20with%20access.%20The%20second%20option%20is%20more%20secure%3B%20however%2C%20it%20requires%20you%20to%20either%20create%20more%20automation%20to%20set%20the%20environment%20variables%20for%20the%20ansible%20user%2C%20permanently%20set%20them%20in%20the%20user%20profile%2C%20which%20requires%20you%20again%20to%20store%20them%20in%20plain%20text%2C%20or%20set%20the%20variables%20manually.%20The%20third%20option%20is%20also%20secure%2C%20but%20does%20not%20lend%20itself%20to%20automation%20as%20you%20have%20to%20manually%20sign%20in%20to%20the%20CLI%20each%20time%20you%20want%20to%20use%20dynamic%20inventory.%20This%20just%20leaves%20us%20with%20option%204.%20This%20is%20the%20most%20secure%20as%20you%E2%80%99re%20not%20required%20to%20store%20credentials%20anywhere%20in%20code%20or%20on%20the%20local%20machine%20nor%20do%20you%20have%20to%20take%20any%20manual%20steps%20to%20authenticate.%20The%20only%20requirement%20is%20that%20your%20Ansible%20control%20server%20must%20be%20running%20in%20Azure.%20Once%20the%20managed%20identity%20is%20assigned%2C%20you%20can%20easily%20control%20the%20level%20of%20access%20to%20resources%20by%20using%20role-based%20access.%20Ok%2C%20now%20that%20we%20have%20that%20out%20of%20the%20way%2C%20let%E2%80%99s%20talk%20about%20the%20prerequisites.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-380606098%22%20id%3D%22toc-hId-380606098%22%3E%26nbsp%3B%3C%2FH1%3E%0A%3CH1%20id%3D%22toc-hId--1426848365%22%20id%3D%22toc-hId--1426848365%22%3EPrerequisites%3C%2FH1%3E%0A%3COL%3E%0A%3CLI%3EThe%20commands%20in%20this%20guide%20assume%20the%20use%20of%20Azure%20CLI%20in%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fcloud-shell%2Foverview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Cloud%20Shell%3C%2FA%3E.%20You%20can%20also%20use%20a%20local%20CLI%3B%20however%2C%20if%20you%E2%80%99re%20not%20running%20on%20Linux%2C%20you%E2%80%99ll%20need%20to%20convert%20the%20commands%20for%20Powershell%20or%20command%20prompt.%3C%2FLI%3E%0A%3CLI%3EAn%20Ansible%20control%20server%20(v2.8%20at%20minimum)%20deployed%20in%20Azure.%20For%20installation%20steps%2C%20see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.ansible.com%2Fansible%2Flatest%2Fscenario_guides%2Fguide_azure.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAnsible%E2%80%99s%20Microsoft%20Azure%20Guide%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EAt%20least%20one%20VM%20in%20Azure%20(Linux%20or%20Windows).%20You%E2%80%99ll%20also%20need%20to%20have%20it%20configured%20for%20communication%20with%20Ansible%20(ssh%20or%20winrm%20depending%20on%20OS)%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3ENow%2C%20on%20to%20the%20dynamic%20inventory%20configuration.%20First%2C%20we%E2%80%99ll%20need%20a%20managed%20identity%20to%20assign%20to%20our%20Ansible%20control%20server.%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId-1060664468%22%20id%3D%22toc-hId-1060664468%22%3E%26nbsp%3B%3C%2FH1%3E%0A%3CH1%20id%3D%22toc-hId--746789995%22%20id%3D%22toc-hId--746789995%22%3ECreate%20a%20managed%20identity%3C%2FH1%3E%0A%3CP%3EYou%20can%20use%20either%20a%20system-assigned%20or%20user-assigned%20identity.%20To%20decide%20which%20type%20is%20best%20for%20you%2C%20see%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fmanaged-identities-azure-resources%2Foverview%23how-does-the-managed-identities-for-azure-resources-work%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edifferences%20between%20a%20system-assigned%20and%20user-assigned%20managed%20identity%3C%2FA%3E.%20Regardless%20of%20which%20type%20you%20choose%3B%20we%E2%80%99ll%20need%20to%20first%20create%20the%20identity%20using%20Azure%20CLI%20in%20Azure%20Cloud%20Shell.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%20If%20you%20already%20have%20an%20identity%20created%2C%20skip%20to%20the%20section%20titled%20%3CEM%3E%E2%80%9C%3C%2FEM%3E%3C%2FSTRONG%3E%3CA%20href%3D%22%23_Assign_RBAC_rights%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3E%3CEM%3EAssign%20RBAC%20rights%20to%20the%20managed%20identity%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FA%3E%3CSTRONG%3E%3CEM%3E%E2%80%9D%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--56228521%22%20id%3D%22toc-hId--56228521%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1863682984%22%20id%3D%22toc-hId--1863682984%22%3EUser-Assigned%20Identity%3C%2FH2%3E%0A%3CP%3ETo%20create%20a%20new%20user-assigned%20identity%2C%20run%20the%20below%20command%2C%20replacing%20RG-NAME%20with%20the%20name%20of%20the%20resource%20group%20for%20the%20identity%2C%20and%20IDENTITY-NAME%20with%20the%20name%20of%20the%20identity.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20identity%20create%20-g%20RG-NAME%20-n%20IDENTITY-NAME%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-623829849%22%20id%3D%22toc-hId-623829849%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1183624614%22%20id%3D%22toc-hId--1183624614%22%3ESystem-Assigned%20Identity%3C%2FH2%3E%0A%3CP%3EFor%20a%20system-assigned%20identity%2C%20run%20the%20following%20command%2C%20where%20RG-NAME%20is%20the%20name%20of%20the%20resource%20group%20and%20VMNAME%20is%20the%20name%20of%20the%20Ansible%20control%20server%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20vm%20identity%20assign%20-g%20RG%20-n%20VMNAME%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--1194127718%22%20id%3D%22toc-hId--1194127718%22%3E%26nbsp%3B%3C%2FH1%3E%0A%3CH1%20id%3D%22toc-hId-1293385115%22%20id%3D%22toc-hId-1293385115%22%3EAssign%20RBAC%20rights%20to%20the%20managed%20identity%3C%2FH1%3E%0A%3CP%3ENow%20that%20we%20have%20the%20identity%20created%2C%20we%20need%20to%20assign%20it%20rights%20to%20Azure%20resources.%20I%20chose%20to%20give%20mine%20Reader%20rights%20on%20the%20resource%20group%20that%20I%E2%80%99ll%20be%20using%20for%20dynamic%20inventory.%20If%20you%20want%20to%20use%20multiple%20resource%20groups%20for%20your%20inventory%2C%20you%E2%80%99ll%20need%20to%20follow%20this%20procedure%20for%20each%20one.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--637250968%22%20id%3D%22toc-hId--637250968%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1850261865%22%20id%3D%22toc-hId-1850261865%22%3EUser-Assigned%20Identity%3C%2FH2%3E%0A%3CP%3ETo%20assign%20Reader%20for%20a%20resource%20group%20to%20a%20user-assigned%20identity%2C%20run%20the%20below%20commands%20in%20the%20Cloud%20Shell.%20To%20run%20the%20commands%2C%20you%E2%80%99ll%20need%20to%20replace%20IDENTITY-NAME%20with%20the%20name%20of%20the%20managed%20identity%20you%20created%20in%20the%20previous%20step%20and%20replace%20the%20scope%20with%20the%20resource%20id%20of%20the%20resource%20group%20to%20assign%20the%20rights%20to%20(found%20under%20properties%20blade%20of%20the%20resource%20group)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EspID%3D%24(az%20identity%20show%20-n%20IDENTITY-NAME%20-g%20RG%20--query%20'%5BprincipalId%5D'%20-o%20tsv)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20role%20assignment%20create%20--assignee%20%24spID%20--role%20%E2%80%98Reader%E2%80%99%20--scope%20%2Fsubscriptions%2FSUBID%2FresourceGroups%2FRG-NAME%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EExample%3A%20%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20role%20assignment%20create%20--assignee%20%24spID%20--role%20%E2%80%98Reader%E2%80%99%20--scope%20%2Fsubscriptions%2F%3C%2FSTRONG%3E%20%3CSTRONG%3Efddda745-b9e7-43e4-9a2a-45fa890210ed%20%2FresourceGroups%2FmyDemoRG%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-42807402%22%20id%3D%22toc-hId-42807402%22%3ESystem-Assigned%20Identity%3C%2FH2%3E%0A%3CP%3ETo%20assign%20Reader%20rights%20for%20a%20resource%20group%20to%20a%20system-assigned%20identity%2C%20run%20the%20following%20commands%20in%20the%20Cloud%20Shell.%20To%20run%20the%20commands%2C%20replace%20ANSIBLE-VM%20with%20the%20name%20of%20the%20Ansible%20VM%20as%20it%20appears%20in%20the%20portal%20and%20replace%20the%20scope%20with%20the%20resource%20id%20of%20the%20resource%20group%20to%20assign%20the%20rights%20to%20(found%20under%20properties%20blade%20of%20the%20resource%20group%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EspID%3D%24(az%20resource%20list%20-n%20VM-NAME%20--query%20%5B*%5D.identity.principalId%20--out%20tsv)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20role%20assignment%20create%20--assignee%20%24spID%20--role%20%E2%80%98Reader%E2%80%99%20--scope%20%2Fsubscriptions%2FSUBID%2FresourceGroups%2FRG-NAME%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CU%3EExample%3A%3C%2FU%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20role%20assignment%20create%20--assignee%20%24spID%20--role%20%E2%80%98Reader%E2%80%99%20--scope%20%2Fsubscriptions%2F%3C%2FSTRONG%3E%20%3CSTRONG%3Efddda745-b9e7-43e4-9a2a-45fa890210ed%20%2FresourceGroups%2FmyDemoRG%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22toc-hId--1893729780%22%20id%3D%22toc-hId--1893729780%22%3EConfigure%20dynamic%20inventory%3C%2FH1%3E%0A%3CP%3EWith%20Ansible%202.8%20and%20later%2C%20dynamic%20inventory%20is%20handled%20through%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fansible%2Fansible%2Fblob%2Fdevel%2Flib%2Fansible%2Fplugins%2Finventory%2Fazure_rm.py%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eazure_rm%20plugin%3C%2FA%3E.%20To%20configure%20the%20VMs%20for%20dynamic%20inventory%2C%20I%20used%20this%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fansible%2Fansible-manage-azure-dynamic-inventories%23ansible-version--28%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etutorial%3C%2FA%3E.%20I%E2%80%99ll%20summarize%20the%20steps%20below%3A%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-722865772%22%20id%3D%22toc-hId-722865772%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1084588691%22%20id%3D%22toc-hId--1084588691%22%3EConfigure%20Azure%20Resources%3C%2FH2%3E%0A%3CP%3EAdd%20a%20tag%20to%20the%20virtual%20machines%20you%20want%20to%20inventory.%20To%20create%20the%20tag%2C%20run%20the%20below%20command.%20Make%20sure%20to%20replace%20RG-NAME%20and%20VM-NAME%20with%20values%20appropriate%20for%20your%20environment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eaz%20vm%20update%20%5C%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20--resource-group%20RG-NAME%20%5C%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20--name%20VM-NAME%20%5C%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20--set%20tags.mytag%3Ddemo%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1402924142%22%20id%3D%22toc-hId-1402924142%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--404530321%22%20id%3D%22toc-hId--404530321%22%3ECreate%20the%20dynamic%20inventory%20file%3C%2FH2%3E%0A%3CP%3EUse%20the%20steps%20below%20to%20setup%20the%20Ansible%20directory%20and%20create%20the%20inventory%20file%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EOpen%20an%20SSH%20session%20to%20your%20Ansible%20control%20server%3C%2FLI%3E%0A%3CLI%3ERun%20the%20following%20command%20to%20create%20a%20directory%20structure%20for%20Ansible%20under%20your%20users%20home%20directory%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSTRONG%3Emkdir%20-p%20~%2Fansible%2Finventory%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%3CSTRONG%3Ecd%20~%2Fansible%2Finventory%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%223%22%3E%0A%3CLI%3EUse%20the%20editor%20of%20your%20choice%20to%20create%20the%20inventory%20file.%20The%20one%20requirement%20is%20that%20the%20file%20name%20must%20end%20in%20aure_rm%20and%20can%20have%20either%20.yml%20or%20.yaml%20extension.%20For%20example%2C%20I%20named%20my%20file%3A%20demo_azure_rm.yaml.%20Once%20created%2C%20add%20the%20following%20contents%20to%20the%20file%2C%20being%20sure%20to%20replace%20RG-NAME%20with%20the%20name%20of%20the%20resource%20group%20that%20contains%20the%20VMs%20you%20want%20to%20manage.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3Eplugin%3A%26nbsp%3Bazure_rm%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3Einclude_vm_resource_groups%3A%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E-%26nbsp%3BRG-NAME%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3Eauth_source%3A%26nbsp%3Bmsi%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3Ekeyed_groups%3A%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E-%26nbsp%3Bprefix%3A%26nbsp%3Bmytestgroup%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%26nbsp%3Bkey%3A%26nbsp%3Btags.mytag%26nbsp%3B%7C%26nbsp%3Bdefault('none')%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3Eexclude_host_filters%3A%3C%2FP%3E%0A%3CP%20style%3D%22padding-left%3A%2030px%3B%22%3E-%26nbsp%3Bpowerstate%26nbsp%3B!%3D%26nbsp%3B'running'%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EBefore%20moving%20on%20to%20the%20next%20section%2C%20let%E2%80%99s%20take%20a%20second%20and%20go%20over%20the%20inventory%20file%20to%20make%20sure%20all%20of%20the%20parameters%20are%20clear.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EThe%3CSTRONG%3E%20auth_source%3C%2FSTRONG%3E%20parameter%20specifies%20the%20type%20of%20authentication%20to%20use.%20With%20the%20MSI%20value%2C%20Ansible%20will%20use%20the%20assigned%20managed%20identity%20for%20authentication.%20For%20other%20available%20auth%20types%2C%20see%20%3CA%20href%3D%22https%3A%2F%2Fdocs.ansible.com%2Fansible%2Flatest%2Fplugins%2Finventory%2Fazure_rm.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAnsible%E2%80%99s%20azure_rm%20guide%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EThe%20%3CSTRONG%3Ekeyed_groups%3C%2FSTRONG%3E%20section%20will%20create%20a%20group%20based%20on%20the%20specified%20parameter.%20This%20is%20where%20the%20tag%20we%20created%20earlier%20comes%20into%20play.%20The%20prefix%20parameter%20defines%20the%20group%20name%20that%20we'll%20use%20for%20the%20hosts%20that%20match%20the%20condition%20specified%20for%20the%20key%20parameter.%20The%20group%20name%20will%20be%20a%20combination%20of%20the%20string%20you%20specify%20%2B%20_key.%20The%20key%20parameter%20is%20looking%20for%20all%20VMs%20in%20the%20resource%20group%20that%20have%20the%20mytag%20tag.%20All%20of%20these%20VMs%20will%20be%20grouped%20by%20the%20value%20of%20the%20mytag%20tag.%20For%20this%20example%2C%20a%20host%20group%20named%20mytestgroup_demo%20will%20be%20created%20as%20that%20was%20the%20value%20of%20the%20tag%20I%20created.%3C%2FLI%3E%0A%3CLI%3EThe%20%3CSTRONG%3Eexclude_host_filters%3C%2FSTRONG%3E%20section%20will%20exclude%20any%20hosts%20that%20match%20the%20condition.%20In%20this%20case%2C%20any%20hosts%20not%20in%20a%20running%20state%20will%20be%20excluded.%20This%20section%20can%20be%20removed%20if%20you%E2%80%99re%20not%20concerned%20with%20the%20VMs%20status.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH1%20id%3D%22toc-hId-1953899793%22%20id%3D%22toc-hId-1953899793%22%3E%26nbsp%3B%3C%2FH1%3E%0A%3CH1%20id%3D%22toc-hId-146445330%22%20id%3D%22toc-hId-146445330%22%3ETest%20the%20configuration%3C%2FH1%3E%0A%3CP%3ENow%20that%20we%20have%20everything%20setup%2C%20it%E2%80%99s%20time%20to%20test%20our%20configuration.%20In%20order%20to%20do%20so%2C%20let%E2%80%99s%20run%20an%20ad-hoc%20command%20against%20the%20inventory%20file%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eansible%20mytestgroup_demo%20-m%20ping%20-i%20~%2Fansible%2Finventory%2Fdemo_azure_rm.yaml%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20should%20see%20output%20similar%20to%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F165638iBFA1929EC9E99E83%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%20The%20ping%20module%20will%20only%20work%20against%20Linux%20VMs.%20If%20testing%20against%20Windows%2C%20use%20the%20win_ping%20module.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20that%E2%80%99s%20it.%20You%20now%20have%20Azure%20dynamic%20inventory%20setup%20in%20Ansible.%20In%20my%20next%20post%2C%20I%E2%80%99ll%20take%20this%20a%20step%20further%20and%20show%20how%20you%20can%20use%20dynamic%20inventory%20to%20apply%20DSC%20configuration%20to%20Windows%20VMs%20in%20Azure.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1062449%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F165288i34D8BAC44275A838%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Ansible_and_MicrosoftAzure.png%22%20title%3D%22Ansible_and_MicrosoftAzure.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%2C%20I'll%20walk%20you%20through%20configuring%20the%20Ansible%20Azure%20Dynamic%20Inventory%20plugin%20(azure_rm)%20to%20use%20a%20managed%20identity.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1062449%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EChrisWallen%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Hello again, Chris Wallen here and in this post, I’m going to show you how to use a managed identity to manage resources using Azure dynamic inventory with Ansible. If you’re not familiar with managed identities, see this overview of managed identities for Azure resources. Basically, they just provide a method of authenticating to Azure without the need to store credentials in code or on any local resource. Now, before we get into the technical details, let’s go over the different sources of Azure credentials that Ansible can use to authenticate when using dynamic inventory:

When using dynamic inventory, you can use any of the following methods to provide your Azure credentials to Ansible

 

  • Storing them in a plain-text credential file
  • Setting Environment variables that store the credentials
  • Using the currently logged in Azure CLI credentials
  • Using a managed identity

 

Obviously, the first option is the least secure as your credentials are stored in a flat-file that could be read by anyone with access. The second option is more secure; however, it requires you to either create more automation to set the environment variables for the ansible user, permanently set them in the user profile, which requires you again to store them in plain text, or set the variables manually. The third option is also secure, but does not lend itself to automation as you have to manually sign in to the CLI each time you want to use dynamic inventory. This just leaves us with option 4. This is the most secure as you’re not required to store credentials anywhere in code or on the local machine nor do you have to take any manual steps to authenticate. The only requirement is that your Ansible control server must be running in Azure. Once the managed identity is assigned, you can easily control the level of access to resources by using role-based access. Ok, now that we have that out of the way, let’s talk about the prerequisites.

 

Prerequisites

  1. The commands in this guide assume the use of Azure CLI in Azure Cloud Shell. You can also use a local CLI; however, if you’re not running on Linux, you’ll need to convert the commands for Powershell or command prompt.
  2. An Ansible control server (v2.8 at minimum) deployed in Azure. For installation steps, see Ansible’s Microsoft Azure Guide
  3. At least one VM in Azure (Linux or Windows). You’ll also need to have it configured for communication with Ansible (ssh or winrm depending on OS)

Now, on to the dynamic inventory configuration. First, we’ll need a managed identity to assign to our Ansible control server.

 

Create a managed identity

You can use either a system-assigned or user-assigned identity. To decide which type is best for you, see the differences between a system-assigned and user-assigned managed identity. Regardless of which type you choose; we’ll need to first create the identity using Azure CLI in Azure Cloud Shell.

 

NOTE: If you already have an identity created, skip to the section titled Assign RBAC rights to the managed identity

 

User-Assigned Identity

To create a new user-assigned identity, run the below command, replacing RG-NAME with the name of the resource group for the identity, and IDENTITY-NAME with the name of the identity.

 

az identity create -g RG-NAME -n IDENTITY-NAME

 

System-Assigned Identity

For a system-assigned identity, run the following command, where RG-NAME is the name of the resource group and VMNAME is the name of the Ansible control server

 

az vm identity assign -g RG -n VMNAME

 

Assign RBAC rights to the managed identity

Now that we have the identity created, we need to assign it rights to Azure resources. I chose to give mine Reader rights on the resource group that I’ll be using for dynamic inventory. If you want to use multiple resource groups for your inventory, you’ll need to follow this procedure for each one.

 

User-Assigned Identity

To assign Reader for a resource group to a user-assigned identity, run the below commands in the Cloud Shell. To run the commands, you’ll need to replace IDENTITY-NAME with the name of the managed identity you created in the previous step and replace the scope with the resource id of the resource group to assign the rights to (found under properties blade of the resource group)

 

spID=$(az identity show -n IDENTITY-NAME -g RG --query '[principalId]' -o tsv)

az role assignment create --assignee $spID --role ‘Reader’ --scope /subscriptions/SUBID/resourceGroups/RG-NAME

 

Example:

az role assignment create --assignee $spID --role ‘Reader’ --scope /subscriptions/ fddda745-b9e7-43e4-9a2a-45fa890210ed /resourceGroups/myDemoRG

 

System-Assigned Identity

To assign Reader rights for a resource group to a system-assigned identity, run the following commands in the Cloud Shell. To run the commands, replace ANSIBLE-VM with the name of the Ansible VM as it appears in the portal and replace the scope with the resource id of the resource group to assign the rights to (found under properties blade of the resource group

 

spID=$(az resource list -n VM-NAME --query [*].identity.principalId --out tsv)

az role assignment create --assignee $spID --role ‘Reader’ --scope /subscriptions/SUBID/resourceGroups/RG-NAME

 

Example:

az role assignment create --assignee $spID --role ‘Reader’ --scope /subscriptions/ fddda745-b9e7-43e4-9a2a-45fa890210ed /resourceGroups/myDemoRG

 

Configure dynamic inventory

With Ansible 2.8 and later, dynamic inventory is handled through the azure_rm plugin. To configure the VMs for dynamic inventory, I used this tutorial. I’ll summarize the steps below:

 

Configure Azure Resources

Add a tag to the virtual machines you want to inventory. To create the tag, run the below command. Make sure to replace RG-NAME and VM-NAME with values appropriate for your environment.

 

az vm update \

    --resource-group RG-NAME \

    --name VM-NAME \

    --set tags.mytag=demo

 

Create the dynamic inventory file

Use the steps below to setup the Ansible directory and create the inventory file

  1. Open an SSH session to your Ansible control server
  2. Run the following command to create a directory structure for Ansible under your users home directory

 

mkdir -p ~/ansible/inventory

 

cd ~/ansible/inventory

 

  1. Use the editor of your choice to create the inventory file. The one requirement is that the file name must end in aure_rm and can have either .yml or .yaml extension. For example, I named my file: demo_azure_rm.yaml. Once created, add the following contents to the file, being sure to replace RG-NAME with the name of the resource group that contains the VMs you want to manage.

 

plugin: azure_rm

include_vm_resource_groups:

- RG-NAME

auth_source: msi

 

keyed_groups:

- prefix: mytestgroup

  key: tags.mytag | default('none')

 

exclude_host_filters:

- powerstate != 'running'

 

Before moving on to the next section, let’s take a second and go over the inventory file to make sure all of the parameters are clear.

 

  1. The auth_source parameter specifies the type of authentication to use. With the MSI value, Ansible will use the assigned managed identity for authentication. For other available auth types, see Ansible’s azure_rm guide
  2. The keyed_groups section will create a group based on the specified parameter. This is where the tag we created earlier comes into play. The prefix parameter defines the group name that we'll use for the hosts that match the condition specified for the key parameter. The group name will be a combination of the string you specify + _key. The key parameter is looking for all VMs in the resource group that have the mytag tag. All of these VMs will be grouped by the value of the mytag tag. For this example, a host group named mytestgroup_demo will be created as that was the value of the tag I created.
  3. The exclude_host_filters section will exclude any hosts that match the condition. In this case, any hosts not in a running state will be excluded. This section can be removed if you’re not concerned with the VMs status.

 

Test the configuration

Now that we have everything setup, it’s time to test our configuration. In order to do so, let’s run an ad-hoc command against the inventory file:

 

ansible mytestgroup_demo -m ping -i ~/ansible/inventory/demo_azure_rm.yaml

 

You should see output similar to the following:

 

clipboard_image_0.png

 

NOTE: The ping module will only work against Linux VMs. If testing against Windows, use the win_ping module.

 

And that’s it. You now have Azure dynamic inventory setup in Ansible. In my next post, I’ll take this a step further and show how you can use dynamic inventory to apply DSC configuration to Windows VMs in Azure.