1

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

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.