<p>From close reading of the <a href="https://docs.ansible.com/ansible/latest/modules/azure_rm_virtualmachine_module.html" rel="nofollow noreferrer">Ansible doc page</a> on creating Azure VMs, it's clear that you can refer to a resource such as a virtual network or NIC using its resource ID.  This lets you specify any resource, just like RM templates.</p>

<p>To specify a NIC created in another resource group by the networking team, with the <code>networking_rg_name</code> var set to that group name, you would use a resource ID like this:</p>

<pre><code>- set_fact:
    nic_id: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ networking_rg_name }}/providers/Microsoft.Network/networkInterfaces/{{ nic_name }}"

- name: "Create VM {{vm_type}} - {{name}}"
  azure_rm_virtualmachine:
    resource_group: "{{rg_name}}"
    name: "{{name}}"
    vm_size: "{{size}}"
    admin_username: "{{user}}"
    admin_password: "{{pass}}"
    os_type: "{{os_type}}"
    network_interfaces: "{{nic_id}}"     # Use the nic_id instead of name
    image: "{{image}}"
    tags: "{{tags}}"
</code></pre>

<p>This works for any resources that are in a different resource group, including virtual networks.</p>

CC BY-SA 4.0
2019-11-20T16:20:33.363Z
2019-11-23T10:46:11.903Z
2019-11-23T10:46:11.903Z
RichVel
<p>I am working on creating an Ansible playbook to</p>

<ul>
<li>Ensure required network resources for an Azure VM are present, and </li>
<li>Create an Azure VM attached to those resources. </li>
</ul>

<p>Due to the way the organization is structured, all <em>network resources</em> (such as NIC, NSG, Subnets and Public IPs) are placed in a <code>Networks</code> Resource Group in Azure. And these are managed by a separate team.</p>

<p>Also, the mandate is to create the Azure VMs and all <em>non network</em> resources in a separate resource group (let's call it <code>AZRVMRG</code>. </p>

<p>The chunk of the playbook that creates the VM looks like this:</p>

<pre><code>---
- name: "Create VM {{vm_type}} - {{name}}"
  azure_rm_virtualmachine:
    resource_group: "{{rg_name}}"
    name: "{{name}}"
    vm_size: "{{size}}"
    admin_username: "{{user}}"
    admin_password: "{{pass}}"
    os_type: "{{os_type}}"
    network_interfaces: "{{nic_name}}"
    image: "{{image}}"
    tags: "{{tags}}"
</code></pre>

<p>Where <code>{{rg_name}}</code> is the <code>AZRVMRG</code> RG, and <code>{{nic_name}}</code> is an existing NIC present in the <code>Networks</code> RG.</p>

<h2>Question</h2>

<p>How do I reference the NIC present in the <code>Networks</code> resource group while creating the Azure VM within <code>AZRVMRG</code> resource group? </p>

<h2>What I've tried</h2>

<p>I have tried "just" referencing the NIC name, but that gives me an error similar to:</p>

<pre><code> "msg": "Error fetching network interface AZRVMNIC - Azure Error: ResourceNotFound\nMessage: The Resource 'Microsoft.Network/networkInterfaces/AZRVMNIC' under resource group 'AZRVMRG' was not found."
</code></pre>

<p>So clearly, its looking under the same resource group.</p>

<h2>Notes</h2>

<p>(I have seen VMs created in the Azure subscription this way, so this is possible in Azure. But Ansible does not document this approach in its <a href="http://docs.ansible.com/ansible/latest/azure_rm_virtualmachine_module.html" rel="nofollow noreferrer">official docs.</a> )</p>

<ansible><azure>
Can I create an Azure VM from Ansible with NICs in different resource groups?
Vish
# Can I create an Azure VM from Ansible with NICs in different resource groups?

(Asked by: username_0 on 2018-03-27)

I am working on creating an Ansible playbook to

Ensure required network resources for an Azure VM are present, and 
Create an Azure VM attached to those resources. 

Due to the way the organization is structured, all network resources (such as NIC, NSG, Subnets and Public IPs) are placed in a Networks Resource Group in Azure. And these are managed by a separate team.
Also, the mandate is to create the Azure VMs and all non network resources in a separate resource group (let's call it AZRVMRG. 
The chunk of the playbook that creates the VM looks like this:
---
- name: "Create VM {{vm_type}} - {{name}}"
  azure_rm_virtualmachine:
    resource_group: "{{rg_name}}"
    name: "{{name}}"
    vm_size: "{{size}}"
    admin_username: "{{user}}"
    admin_password: "{{pass}}"
    os_type: "{{os_type}}"
    network_interfaces: "{{nic_name}}"
    image: "{{image}}"
    tags: "{{tags}}"

Where {{rg_name}} is the AZRVMRG RG, and {{nic_name}} is an existing NIC present in the Networks RG.
Question
How do I reference the NIC present in the Networks resource group while creating the Azure VM within AZRVMRG resource group? 
What I've tried
I have tried "just" referencing the NIC name, but that gives me an error similar to:
 "msg": "Error fetching network interface AZRVMNIC - Azure Error: ResourceNotFound\nMessage: The Resource 'Microsoft.Network/networkInterfaces/AZRVMNIC' under resource group 'AZRVMRG' was not found."

So clearly, its looking under the same resource group.
Notes
(I have seen VMs created in the Azure subscription this way, so this is possible in Azure. But Ansible does not document this approach in its official docs. )

**Tags:** ansible, azure


---

## 1 answers

### Answer by username_1 on 2019-11-20. Score: 1

From close reading of the Ansible doc page on creating Azure VMs, it's clear that you can refer to a resource such as a virtual network or NIC using its resource ID.  This lets you specify any resource, just like RM templates.
To specify a NIC created in another resource group by the networking team, with the networking_rg_name var set to that group name, you would use a resource ID like this:
- set_fact:
    nic_id: "/subscriptions/{{ subscription_id }}/resourceGroups/{{ networking_rg_name }}/providers/Microsoft.Network/networkInterfaces/{{ nic_name }}"

- name: "Create VM {{vm_type}} - {{name}}"
  azure_rm_virtualmachine:
    resource_group: "{{rg_name}}"
    name: "{{name}}"
    vm_size: "{{size}}"
    admin_username: "{{user}}"
    admin_password: "{{pass}}"
    os_type: "{{os_type}}"
    network_interfaces: "{{nic_id}}"     # Use the nic_id instead of name
    image: "{{image}}"
    tags: "{{tags}}"

This works for any resources that are in a different resource group, including virtual networks.


---

