How to import Azure Policy from GitHub

%3CLINGO-SUB%20id%3D%22lingo-sub-3260822%22%20slang%3D%22en-US%22%3EHow%20to%20import%20Azure%20Policy%20from%20GitHub%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3260822%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20collection%20of%20policies%20and%20initiative%20I%20have%20built%20on%20a%20subscription.%20Now%20I%20exported%20all%20of%20those%20on%20my%20private%20repository%20in%20GitHub%20as%20for%20Microsoft%20documentation%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fgovernance%2Fpolicy%2Ftutorials%2Fpolicy-as-code-github%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ETutorial%3A%20Implement%20Azure%20Policy%20as%20Code%20with%20GitHub%20-%20Azure%20Policy%20%7C%20Microsoft%20Docs%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20I'd%20like%20to%20import%20all%20those%20policies%20and%20definition%20in%20another%20subscription%20which%20belongs%20to%20another%20Tenant.%20I%20created%20a%20new%20security%20principal%20with%20appropriate%20rights%20on%20this%20new%20tenant%2C%20updated%20the%20credential%20on%20GitHub%20and%20launch%20the%20workflow.%20This%20is%20the%20result%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22GiuseppeTristano_0-1647595422517.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F356791iDAB3F8DEBCD400A9%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22GiuseppeTristano_0-1647595422517.png%22%20alt%3D%22GiuseppeTristano_0-1647595422517.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20question%20is%3A%20is%20there%20a%20way%20to%20export%20policy%20from%20a%20subscription%20and%20import%20it%20in%20another%20subscription%3F%20Tenants%20as%20stated%20above%20are%20different.%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20help%20and%20best%20regards.%3C%2FP%3E%3CP%3Egiuseppe%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3260822%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20Policy%20export%20import%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3381086%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20import%20Azure%20Policy%20from%20GitHub%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3381086%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1336835%22%20target%3D%22_blank%22%3E%40GiuseppeTristano%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20see%20Azure%20Policy%20policy%20as%20two%20parts%3C%2FP%3E%3COL%3E%3CLI%3EPolicy%20Definition%3C%2FLI%3E%3CLI%3EPolicy%20Assignments%3C%2FLI%3E%3C%2FOL%3E%3CP%3EIf%20the%20Policy%20Definitions%20are%20exactly%20the%20same%2C%20then%20create%20it%20once%20by%20defining%20it%20at%20the%20highest%20Management%20Group%20Level%20possible.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20your%20organization%20is%20not%20using%20Management%20Groups%20and%20placing%20Subscriptions%20under%20a%20Management%20Group%20Tree%2C%20they%20should%20rethink%20their%20approach%20on%20how%20to%20use%20Azure.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EYes%20you%20may%20have%20two%20subscriptions%20under%20two%20different%20Level%201%20management%20groups.%20In%20that%20case%2C%20you%20have%20to%20create%20a%20Policy%20Definition%20under%20each%20Level%201%20Management%20group.%3CBR%20%2F%3E%3CBR%20%2F%3EHow%20you%20plan%20to%20create%20a%20policy%20definition%20using%20automation%20will%20determine%20what%20is%20needed%20to%20prepare%20the%20necessary%20properties%20and%20values.%20The%20choices%20are%20using%20ARM%20Templates%2C%20Azure%20REST%20APIs%2C%20PowerShell%2C%20and%20CLI.%3CBR%20%2F%3E%3CBR%20%2F%3EPersonally%2C%20I%20do%20not%20use%20the%20MS%20Open%20Source%20Policy-as-Code%20(PaC)%20approach%20but%20a%20different%20PaC%20Git%20Repo%20approach%20we%20developed%20in%202018%20where%20the%20Policy%20Definitions%20%26amp%3B%20Assignments%20are%20destructed%20into%20Folder%20(one%20for%20each%20policy)%20and%20the%20properties%20%26amp%3B%20values%20are%20organized%20into%20PSD1%20%26amp%3B%20JSON%20files.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThe%20second%20part%20is%20the%20Policy%20Assignment%20which%20can%20be%20created%20at%20the%20Management%20Group%2C%20Subscription%2C%20or%20Resource%20Group.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20you%20have%20all%20your%20policy%20assignments%20at%20a%20Subscription%2C%20then%20query%20all%20the%20assignments%2C%20loop%20through%20them%2C%20build%20a%20Splat%20(HashTable%20of%20Property%20Names%20and%20Values)%2C%20and%20change%20the%20subscription%20references%20to%20the%20new%20subscription%2C%20and%20call%20New-AzPolicyAssignment%26nbsp%3B%40splat.%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3EI%20might%20be%20able%20to%20provide%20better%20guidance%20if%20I%20know%20better%20how%20things%20are%20organized%20and%20what%20policy%20deployment%20method%20you%20are%20comfortable%20with.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EIf%20you%20have%20had%20success%20using%20the%20MS%20PaS%20Approach%20and%20tools%2C%20then%20you%20should%20be%20able%20to%20copy%20the%20folder%20and%20files%20in%20GitHub%20to%20a%20folder%20to%20be%20used%20for%20the%20new%20subscription%2C%20then%20search%20and%20replace%20the%20old%20Subscription%20Ids%20with%20the%20new%20Subscription%20Id%20in%20each%20of%20the%20files.%20You%20may%20have%20to%20do%20that%20too%20with%20the%20Policy%20Definition%20Id%20if%20they%20are%20different.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards...%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

Dear All,

 

I have a collection of policies and initiative I have built on a subscription. Now I exported all of those on my private repository in GitHub as for Microsoft documentation:

 

Tutorial: Implement Azure Policy as Code with GitHub - Azure Policy | Microsoft Docs

 

Now I'd like to import all those policies and definition in another subscription which belongs to another Tenant. I created a new security principal with appropriate rights on this new tenant, updated the credential on GitHub and launch the workflow. This is the result:

 

GiuseppeTristano_0-1647595422517.png

 

My question is: is there a way to export policy from a subscription and import it in another subscription? Tenants as stated above are different.

Thank you for your help and best regards.

giuseppe

 

1 Reply

@GiuseppeTristano 

 

I see Azure Policy policy as two parts

  1. Policy Definition
  2. Policy Assignments

If the Policy Definitions are exactly the same, then create it once by defining it at the highest Management Group Level possible.

 

If your organization is not using Management Groups and placing Subscriptions under a Management Group Tree, they should rethink their approach on how to use Azure.


Yes you may have two subscriptions under two different Level 1 management groups. In that case, you have to create a Policy Definition under each Level 1 Management group.

How you plan to create a policy definition using automation will determine what is needed to prepare the necessary properties and values. The choices are using ARM Templates, Azure REST APIs, PowerShell, and CLI.

Personally, I do not use the MS Open Source Policy-as-Code (PaC) approach but a different PaC Git Repo approach we developed in 2018 where the Policy Definitions & Assignments are destructed into Folder (one for each policy) and the properties & values are organized into PSD1 & JSON files.


The second part is the Policy Assignment which can be created at the Management Group, Subscription, or Resource Group.

If you have all your policy assignments at a Subscription, then query all the assignments, loop through them, build a Splat (HashTable of Property Names and Values), and change the subscription references to the new subscription, and call New-AzPolicyAssignment @splat.

I might be able to provide better guidance if I know better how things are organized and what policy deployment method you are comfortable with.


If you have had success using the MS PaS Approach and tools, then you should be able to copy the folder and files in GitHub to a folder to be used for the new subscription, then search and replace the old Subscription Ids with the new Subscription Id in each of the files. You may have to do that too with the Policy Definition Id if they are different.

 

Regards...