<p>With a bit trial and error I was able to work out the values for my &quot;vnet_id&quot; and &quot;snet_id&quot; variables.</p>
<p>I found using the full ID for the virtual network and not having a &quot;/&quot; before the subnet variable (snet_id) value fixed my problem.</p>
<p>At the moment I don't fully understand how Ansible/Azure puts it all together, but this has given me enough information to start to understand.</p>
<p>If anyone could point me to some documentation that explains the construction/interpolation of the variables it would be helpful.</p>
<pre><code>  tasks:
   - set_fact:
       vnet_rg_id: &quot;/subscriptions/REVMOVED-SUB-ID/resourceGroups/rg-ansible-test&quot;
       vnet_id: &quot;/subscriptions/REVMOVED-SUB-ID/resourceGroups/rg-ansible-test/providers/Microsoft.Network/virtualNetworks/vnet-ansible-test&quot;
       snet_id: &quot;snet-ansible-test&quot;

   - name: Create resource group
     azure_rm_resourcegroup:
       name: &quot;{{ rg }}&quot; # declared earlier in the playbook
       location: &quot;{{ location }}&quot; # declared earlier in the playbook

   - name: Create virtual network inteface cards for VM
     azure_rm_networkinterface:
       resource_group: &quot;{{ rg }}&quot; # declared earlier in the playbook
       name: &quot;{{ nic_name }}&quot; # declared earlier in the playbook
       virtual_network: &quot;{{ vnet_id }}&quot;
       subnet: &quot;{{ snet_id }}&quot;
       location: &quot;{{ location }}&quot; # declared earlier in the playbook
</code></pre>

CC BY-SA 4.0
2021-02-19T11:48:20.537Z
2021-02-19T11:48:20.537Z
user808
<p>My aim is to have my vnet and subnet in a seperate resource group from the other parts of my infrastructure i.e Virtual Machines.</p>
<p>I have a short playbook that creates the resource group for my network (vnet &amp; subnet), which works fine. I can build a VM via the portal and all is well.</p>
<p>My other playbook creates a resource group, creates a vmnic and VM. This playbook references the vnet and subnet created in the other resource group.</p>
<p>I'm encountering an error (see below) when referencing the resources in the network resource group I created. I found a <a href="https://devops.stackexchange.com/questions/3741/can-i-create-an-azure-vm-from-ansible-with-nics-in-different-resource-groups">previous post</a> that has helped by suggesting the use of the &quot;set_fact&quot; module.</p>
<p>I've tried different ways of entering set_fact key values (full resource path / part resource path), but I keep getting an error. I suspect it maybe due to interpolation? Note the double &quot;//&quot; in the error message.</p>
<p>Could anyone provide some guidance on where I'm going wrong, please?</p>
<p>Error message:</p>
<blockquote>
<p>fatal: [localhost]: FAILED! =&gt; {&quot;changed&quot;: false, &quot;msg&quot;: &quot;Error creating or updating network interface test-vm-vnic1 - Azure Error: InvalidRequestFormat\nMessage: Cannot parse the request.\nException Details:\n\tError Code: InvalidJsonReferenceFormat\n\tMessage: Reference Id /subscriptions/REMOVED-SUB-ID/resourceGroups/test-rg/providers/Microsoft.Network/virtualNetworks//providers/Microsoft.Network/virtualNetworks/vnet-ansible-test/subnets//subnets/snet-ansible-test is not formatted correctly. The Id is expected to reference resources of type virtualNetworks/subnets. Path properties.ipConfigurations[0].properties.subnet.&quot;}</p>
</blockquote>
<p>Part of playbook:</p>
<pre><code>  tasks:
   - set_fact:
       vnet_rg_id: &quot;/subscriptions/REMOVED-SUB-ID/resourceGroups/rg-ansible-test&quot;
       vnet_id: &quot;/providers/Microsoft.Network/virtualNetworks/vnet-ansible-test&quot;
       snet_id: &quot;/subnets/snet-ansible-test&quot;

   - name: Create resource group
     azure_rm_resourcegroup:
       name: &quot;{{ rg }}&quot; # this is var declared earlier in the playbook
       location: &quot;{{ location }}&quot; # this is var declared earlier in the playbook

   - name: Create virtual network inteface cards for VM
     azure_rm_networkinterface:
       resource_group: &quot;{{ rg }}&quot; # this is var declared earlier in the playbook
       name: &quot;{{ nic_name }}&quot; # this is var declared earlier in the playbook
       virtual_network: &quot;{{ vnet_id }}&quot;
       subnet: &quot;{{ snet_id }}&quot;
       location: &quot;{{ location }}&quot; # this is var declared earlier in the playbook
</code></pre>

<ansible><azure-devops><azure>
Ansible - Azure resource referencing
user808
# Ansible - Azure resource referencing

(Asked by: username_0 on 2021-02-18)

My aim is to have my vnet and subnet in a seperate resource group from the other parts of my infrastructure i.e Virtual Machines.
I have a short playbook that creates the resource group for my network (vnet & subnet), which works fine. I can build a VM via the portal and all is well.
My other playbook creates a resource group, creates a vmnic and VM. This playbook references the vnet and subnet created in the other resource group.
I'm encountering an error (see below) when referencing the resources in the network resource group I created. I found a previous post that has helped by suggesting the use of the "set_fact" module.
I've tried different ways of entering set_fact key values (full resource path / part resource path), but I keep getting an error. I suspect it maybe due to interpolation? Note the double "//" in the error message.
Could anyone provide some guidance on where I'm going wrong, please?
Error message:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error creating or updating network interface test-vm-vnic1 - Azure Error: InvalidRequestFormat\nMessage: Cannot parse the request.\nException Details:\n\tError Code: InvalidJsonReferenceFormat\n\tMessage: Reference Id /subscriptions/REMOVED-SUB-ID/resourceGroups/test-rg/providers/Microsoft.Network/virtualNetworks//providers/Microsoft.Network/virtualNetworks/vnet-ansible-test/subnets//subnets/snet-ansible-test is not formatted correctly. The Id is expected to reference resources of type virtualNetworks/subnets. Path properties.ipConfigurations[0].properties.subnet."}

Part of playbook:
  tasks:
   - set_fact:
       vnet_rg_id: "/subscriptions/REMOVED-SUB-ID/resourceGroups/rg-ansible-test"
       vnet_id: "/providers/Microsoft.Network/virtualNetworks/vnet-ansible-test"
       snet_id: "/subnets/snet-ansible-test"

   - name: Create resource group
     azure_rm_resourcegroup:
       name: "{{ rg }}" # this is var declared earlier in the playbook
       location: "{{ location }}" # this is var declared earlier in the playbook

   - name: Create virtual network inteface cards for VM
     azure_rm_networkinterface:
       resource_group: "{{ rg }}" # this is var declared earlier in the playbook
       name: "{{ nic_name }}" # this is var declared earlier in the playbook
       virtual_network: "{{ vnet_id }}"
       subnet: "{{ snet_id }}"
       location: "{{ location }}" # this is var declared earlier in the playbook

**Tags:** ansible, azure-devops, azure


---

## 1 answers

### Answer by username_0 on 2021-02-19. Score: 1

With a bit trial and error I was able to work out the values for my "vnet_id" and "snet_id" variables.
I found using the full ID for the virtual network and not having a "/" before the subnet variable (snet_id) value fixed my problem.
At the moment I don't fully understand how Ansible/Azure puts it all together, but this has given me enough information to start to understand.
If anyone could point me to some documentation that explains the construction/interpolation of the variables it would be helpful.
  tasks:
   - set_fact:
       vnet_rg_id: "/subscriptions/REVMOVED-SUB-ID/resourceGroups/rg-ansible-test"
       vnet_id: "/subscriptions/REVMOVED-SUB-ID/resourceGroups/rg-ansible-test/providers/Microsoft.Network/virtualNetworks/vnet-ansible-test"
       snet_id: "snet-ansible-test"

   - name: Create resource group
     azure_rm_resourcegroup:
       name: "{{ rg }}" # declared earlier in the playbook
       location: "{{ location }}" # declared earlier in the playbook

   - name: Create virtual network inteface cards for VM
     azure_rm_networkinterface:
       resource_group: "{{ rg }}" # declared earlier in the playbook
       name: "{{ nic_name }}" # declared earlier in the playbook
       virtual_network: "{{ vnet_id }}"
       subnet: "{{ snet_id }}"
       location: "{{ location }}" # declared earlier in the playbook


---

