Repository: ansible/ansible

Issue:
Can't create VM in a VNET that exists in a different resource group in v2.4
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py 

##### ANSIBLE VERSION
```
ansible 2.4.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansibleadmin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
```

##### CONFIGURATION
- none
##### OS / ENVIRONMENT
CentOS 7.2 on Azure
Linux anscontrol01 3.10.0-327.28.2.el7.x86_64 #1 SMP Wed Aug 3 11:11:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

##### SUMMARY
I am trying to create a new VM in an existing resource group. The VM needs to be part of a vnet/subnet that belongs to another resource group in the same region.

Following is the snippet from the playbook -
  - name: Create a VM in the web server rg
    azure_rm_virtualmachine:
      resource_group: webserverrg <-- This is the resource group where the VM needs to be created
      name: webvm02
      vm_size: Standard_D1_v2
      admin_username: admin2
      admin_password: MyPassword@1
      image:
        offer: UbuntuServer
        publisher: Canonical
        sku: '16.04-LTS'
        version: latest
      os_type: Linux
      virtual_network_resource_group: baserg  <-- This is where the VNET already exists
      virtual_network_name: intvnet  <-- The name of the existing VNET
      subnet_name: websubnet <-- The subnet this VM needs to be part of

When running the playbook - I get the following error -
TASK [Create a VM in the web server rg] *************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Error: fetching subnet websubnet - Azure Error: ResourceNotFound\nMessage: The Resource 'Microsoft.Network/virtualNetworks/intvnet' under resource group 'webserverrg' was not found."}

It seems the virtual_network_resource_group parameter is not taking effect.

##### STEPS TO REPRODUCE
<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Executing playbook
  hosts: localhost
  connection: local
  tasks:
  - name: Create base resource group
    azure_rm_resourcegroup:
      name: baserg
      location: southindia

  - name: Create internal virtual network
    azure_rm_virtualnetwork:
      name: intvnet
      resource_group: baserg
      address_prefixes_cidr:
        - "10.2.0.0/16"

  - name: Create web subnet
    azure_rm_subnet:
      name: websubnet
      virtual_network_name: intvnet
      resource_group: baserg
      address_prefix_cidr: "10.2.1.0/24"

  - name: Create resource group for web servers
    azure_rm_resourcegroup:
      name: webserverrg
      location: southindia

  - name: Create a VM in the web server rg
    azure_rm_virtualmachine:
      resource_group: webserverrg
      name: webvm01
      vm_size: Standard_D1_
      admin_username: admin2
      admin_password: qazWSX@098
      image:
        offer: UbuntuServer
        publisher: Canonical
        sku: '16.04-LTS'
        version: latest
      os_type: Linux
      virtual_network_name: intnet
      virtual_network_resource_group: baserg
      subnet_name: websubnet
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Expectation was to be able to create a VM in the RG webserverrg which uses the Subnet websubnet from intvnet VNet which exists in RG baserg

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Failed to create VM with the following error -
"msg": "Error: fetching subnet websubnet - Azure Error: ResourceNotFound\nMessage: The Resource 'Microsoft.Network/virtualNetworks/intvnet' under resource group 'webserverrg' was not found."

<!--- Paste verbatim command output between quotes below -->
```
The full traceback is:
  File "/tmp/ansible_XGz1AX/ansible_module_azure_rm_virtualmachine.py", line 1214, in create_default_nic
    subnet = self.network_client.subnets.get(self.resource_group, virtual_network_name, self.subnet_name)
  File "/usr/lib/python2.7/site-packages/azure/mgmt/network/operations/subnets_operations.py", line 183, in get
    raise exp

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "ad_user": null,
            "admin_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "admin_username": "admin2",
            "allocated": true,
            "append_tags": true,
            "client_id": null,
            "image": {
                "offer": "UbuntuServer",
                "publisher": "Canonical",
                "sku": "16.04-LTS",
                "version": "16.04.201708030"
            },
            "location": null,
            "name": "webvm02",
            "network_interface_names": null,
            "open_ports": null,
            "os_disk_caching": "ReadOnly",
            "os_type": "Linux",
            "password": null,
            "profile": null,
            "public_ip_allocation_method": "Static",
            "remove_on_absent": [
                "all"
            ],
            "resource_group": "webserverrg",
            "restarted": false,
            "secret": null,
            "short_hostname": null,
            "ssh_password_enabled": true,
            "ssh_public_keys": null,
            "started": true,
            "state": "present",
            "storage_account_name": null,
            "storage_blob_name": null,
            "storage_container_name": "vhds",
            "subnet_name": "websubnet",
            "subscription_id": null,
            "tags": null,
            "tenant": null,
            "virtual_network_name": "intvnet",
            "virtual_network_resource_group": "baserg",
            "vm_size": "Standard_D1_v2"
        }
    },
    "msg": "Error: fetching subnet websubnet - Azure Error: ResourceNotFound\nMessage: The Resource 'Microsoft.Network/virtualNetworks/intvnet' under resource group 'webserverrg' was not found."
}

```



Discussion:
cc @haroldwongms @nitzmahone @tstringer
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @chouseknecht
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @xscript @yaweiw @zikalino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @yuwzho
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @trstringer
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Thanks for reporting this issue, I will fix it soon!

Resolving patch:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
--- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
@@ -1605,11 +1605,11 @@ def create_default_nic(self):
             # Find a virtual network
             no_vnets_msg = "Error: unable to find virtual network in resource group {0}. A virtual network " \
                            "with at least one subnet must exist in order to create a NIC for the virtual " \
-                           "machine.".format(self.resource_group)
+                           "machine.".format(virtual_network_resource_group)
 
             virtual_network_name = None
             try:
-                vnets = self.network_client.virtual_networks.list(self.resource_group)
+                vnets = self.network_client.virtual_networks.list(virtual_network_resource_group)
             except CloudError:
                 self.log('cloud error!')
                 self.fail(no_vnets_msg)
@@ -1624,7 +1624,7 @@ def create_default_nic(self):
 
         if self.subnet_name:
             try:
-                subnet = self.network_client.subnets.get(self.resource_group, virtual_network_name, self.subnet_name)
+                subnet = self.network_client.subnets.get(virtual_network_resource_group, virtual_network_name, self.subnet_name)
                 subnet_id = subnet.id
             except Exception as exc:
                 self.fail("Error: fetching subnet {0} - {1}".format(self.subnet_name, str(exc)))


Test patch: