Learn Idem

Learn Idem

Powered by Idem Project

Describe 2 State

You probably noticed that when you execute a describe operation fron CLI, the output resembles the same kind of data we have defined in states examples, that is because it is exactly the same, meaning, you don’t need to start from scratch to create, modify or even delete resources, you can simply issue the describe operation and save the output into an SLS file then “tweak” accordingly to meet your intention.

Let’s see it with two different Azure resources

Let’s say we want to add a new Azure resource group, for that I could simply describe an existing “Azure Resource Group Name” and save the output into a SLS file.

 idem describe azure.resource_management.resource_groups --filter="[?resource[?resource_group_name=='<Existing Azure Resource Group Name>']]" >> <states folder>/my_other_resource_group_state.sls

Then we can edit the file, removing any property we don’t need, “Provison States” sentences and “ID” entries, inlcuding any long names with specific “IDs”, as they are generated by the system, then updating the relevant ones, such as the name or the region, or any parameter associated to the new resource
Please note, that when editing, you always need to include the minimum parameters to create any given resource (Azure Plug-In Documentation and even Azure Cloud API documentation provides details in this regard).

<New Azure Resource Group Name>:
 azure.resource_management.resource_groups.present:
 - resource_group_name: <New Azure Resource Group Name>
 - parameters:
     location: <Azure Region>
     name: <New Azure Resource Group Name>
     properties:
     tags: 
       <tag name>: <tag value>
       <tag name X>: <tag value X>
     type: Microsoft.Resources/resourceGroups

Of course you can use an existing SLS File, e,g. you may have all your Resource Groups in one single SLS State file. Please make sure to indicate the state directive present , for instructing idem to create a new resource group in Azure.

Then State file can be executed with:

idem state <file_path>/my_other_resource_group_state.sls

In this example:

idem state states/my_other_resource_group_state.sls

After that you will see Idem creating a new resource to meet our new intention and state.

State “Describe 2 State”:

What about an Azure Virtual Network?, once again, I could simply describe an existing “Azure Virtual Network Name” and save the output into a SLS file.

 idem describe azure.virtual_networks.virtual_networks --filter="[?resource[?virtual_network_name=='<Existing Azure Virtual Network Name>']]" >> <states folder>/my_other_virtual_network_state.sls

Then we can edit the file, removing any property we don’t need, “Provison States” sentences and “ID” entries, inlcuding any long names with specific “IDs”, as they are generated by the system, then updating the relevant ones, such as the name or the region, or any parameter associated to the new resource
Please note, that when editing, you always need to include the minimum parameters to create any given resource (Azure Plug-In Documentation and even Azure Cloud API documentation provides details in this regard).

<New Azure Virtual Network Name>:
 azure.virtual_networks.virtual_networks.present:
 - resource_group_name: <Azure Resource Group Name>
 - virtual_network_name: <New Azure Virtual Network Name>
 - parameters:
     location: <Azure Region>
     name: <New Azure Virtual Network Name>
     properties:
       addressSpace:
         addressPrefixes:
         - 10.0.0.0/24
       enableDdosProtection: false

Of course you can use an existing SLS File, e,g. you may have all your Resource Groups in one single SLS State file. Please make sure to indicate the state directive present , for instructing idem to create a new resource group in Azure.

Then State file can be executed with:

idem state <file_path>/my_other_virtual_network_state.sls

In this example:

idem state states/my_other_virtual_network_state.sls

After that you will see Idem creating a new virtual network resource to meet our new intention and state.

Last updated on 8 Feb 2022
 Edit on GitHub