Repository: ansible/ansible
Pull request: Azure _info modules: fix returned ansible_facts when called as _info
##### SUMMARY
Fixes #61569. Also, `azure_rm_virtualmachinescaleset_info` current always returns an empty result (or `ansible_facts`) when called as `_info` module.

##### ISSUE TYPE
- Bugfix Pull Request

##### COMPONENT NAME
azure_rm_dnsrecordset_info
azure_rm_resourcegroup_info
azure_rm_storageaccount_info
azure_rm_virtualmachineimage_info
azure_rm_virtualmachinescaleset_info
azure_rm_virtualnetwork_info


Problem statement:
Some azure _info modules still return ansible_facts when called as _info
##### SUMMARY
Some of the modules still return something in `ansible_facts`, even though the result in there is an empty list:
 - ~~`azure_rm_publicipaddress_info`~~
 - ~~`azure_rm_networkinterface_info`~~
 - `azure_rm_storageaccount_info`
 - `azure_rm_virtualmachineimage_info`
 - `azure_rm_virtualmachinescaleset_info`
 - `azure_rm_virtualnetwork_info`

Finally, `azure_rm_resourcegroup_info` always returns `ansible_facts` with content (which is also returned as non-`ansible_facts`).

When called as `_info` modules, these modules should not have anything in `ansible_facts` when returning.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
~~azure_rm_publicipaddress_info~~
~~azure_rm_networkinterface_info~~
azure_rm_resourcegroup_info
azure_rm_storageaccount_info
azure_rm_virtualmachineimage_info
azure_rm_virtualmachinescaleset_info
azure_rm_virtualnetwork_info

##### ANSIBLE VERSION
```paste below
2.9
```

Hints from issue discussion:
Files identified in the description:
* [`lib/ansible/modules/cloud/azure/azure_rm_publicipaddress_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_publicipaddress_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @haroldwongms @nitzmahone @sozercan @yungezz @yuwzho
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@felixfontein, just so you are aware we have a dedicated Working Group for azure.
You can find other people interested in this in `#ansible-azure` on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: community_workgroups --->
Looks like some have been fixed in #61584. Thanks @jborean93!
Files identified in the description:
* [`lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py)
* [`lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Commit message: Adjust PR #.

Patch to review:
diff --git a/changelogs/fragments/61805-azure-facts-info.yml b/changelogs/fragments/61805-azure-facts-info.yml
new file mode 100644
index 00000000000000..aa04f7e5ca2fe6
--- /dev/null
+++ b/changelogs/fragments/61805-azure-facts-info.yml
@@ -0,0 +1,7 @@
+bugfixes:
+- "azure_rm_dnsrecordset_info - no longer returns empty ``azure_dnsrecordset`` facts when called as ``_info`` module."
+- "azure_rm_resourcegroup_info - no longer returns ``azure_resourcegroups`` facts when called as ``_info`` module."
+- "azure_rm_storageaccount_info - no longer returns empty ``azure_storageaccounts`` facts when called as ``_info`` module."
+- "azure_rm_virtualmachineimage_info - no longer returns empty ``azure_vmimages`` facts when called as ``_info`` module."
+- "azure_rm_virtualmachinescaleset_info - fix wrongly empty result, or ``ansible_facts`` result, when called as ``_info`` module."
+- "azure_rm_virtualnetwork_info - no longer returns empty ``azure_virtualnetworks`` facts when called as ``_info`` module."
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_info.py b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_info.py
index e918e89f318f8d..8968c33539091b 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_containerinstance_info.py
@@ -202,7 +202,6 @@ def __init__(self):
         # store the results of the module operation
         self.results = dict(
             changed=False,
-            ansible_facts=dict()
         )
         self.resource_group = None
         self.name = None
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_dnsrecordset_info.py b/lib/ansible/modules/cloud/azure/azure_rm_dnsrecordset_info.py
index e6f6220c5f43c5..1cf8a3da0873f3 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_dnsrecordset_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_dnsrecordset_info.py
@@ -179,7 +179,6 @@ def __init__(self):
         # store the results of the module operation
         self.results = dict(
             changed=False,
-            ansible_info=dict(azure_dnsrecordset=[])
         )
 
         self.relative_name = None
@@ -221,7 +220,9 @@ def exec_module(self, **kwargs):
             results = self.list_zone()
 
         if is_old_facts:
-            self.results['ansible_facts']['azure_dnsrecordset'] = self.serialize_list(results)
+            self.results['ansible_facts'] = {
+                'azure_dnsrecordset': self.serialize_list(results)
+            }
         self.results['dnsrecordsets'] = self.curated_list(results)
         return self.results
 
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py b/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py
index 08bb991f5e865e..949cbdcf155176 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_resourcegroup_info.py
@@ -154,7 +154,6 @@ def __init__(self):
 
         self.results = dict(
             changed=False,
-            ansible_facts=dict(azure_resourcegroups=[]),
             resourcegroups=[]
         )
 
@@ -167,20 +166,25 @@ def __init__(self):
                                                        facts_module=True)
 
     def exec_module(self, **kwargs):
+        is_old_facts = self.module._name == 'azure_rm_resourcegroup_facts'
+        if is_old_facts:
+            self.module.deprecate("The 'azure_rm_resourcegroup_facts' module has been renamed to 'azure_rm_resourcegroup_info'", version='2.13')
 
         for key in self.module_arg_spec:
             setattr(self, key, kwargs[key])
 
         if self.name:
-            self.results['ansible_facts']['azure_resourcegroups'] = self.get_item()
+            result = self.get_item()
         else:
-            self.results['ansible_facts']['azure_resourcegroups'] = self.list_items()
+            result = self.list_items()
 
         if self.list_resources:
-            for item in self.results['ansible_facts']['azure_resourcegroups']:
+            for item in result:
                 item['resources'] = self.list_by_rg(item['name'])
 
-        self.results['resourcegroups'] = self.results['ansible_facts']['azure_resourcegroups']
+        if is_old_facts:
+            self.results['ansible_facts']['azure_resourcegroups'] = result
+        self.results['resourcegroups'] = result
 
         return self.results
 
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_sqlserver_info.py b/lib/ansible/modules/cloud/azure/azure_rm_sqlserver_info.py
index bae4ab8a4a48da..2a90dd19293e1b 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_sqlserver_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_sqlserver_info.py
@@ -138,7 +138,6 @@ def __init__(self):
         # store the results of the module operation
         self.results = dict(
             changed=False,
-            ansible_facts=dict()
         )
         self.resource_group = None
         self.server_name = None
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py b/lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py
index 82906c98bbeed8..13fb556f77ba22 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_storageaccount_info.py
@@ -371,7 +371,6 @@ def __init__(self):
 
         self.results = dict(
             changed=False,
-            ansible_facts=dict(azure_storageaccounts=[]),
             storageaccounts=[]
         )
 
@@ -407,8 +406,10 @@ def exec_module(self, **kwargs):
         filtered = self.filter_tag(results)
 
         if is_old_facts:
-            self.results['ansible_facts']['azure_storageaccounts'] = self.serialize(filtered)
-            self.results['ansible_facts']['storageaccounts'] = self.format_to_dict(filtered)
+            self.results['ansible_facts'] = {
+                'azure_storageaccounts': self.serialize(filtered),
+                'storageaccounts': self.format_to_dict(filtered),
+            }
         self.results['storageaccounts'] = self.format_to_dict(filtered)
         return self.results
 
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py
index c598432a690835..52212731845208 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachineimage_info.py
@@ -139,7 +139,6 @@ def __init__(self, **kwargs):
 
         self.results = dict(
             changed=False,
-            ansible_facts=dict(azure_vmimages=[])
         )
 
         self.location = None
@@ -159,6 +158,7 @@ def exec_module(self, **kwargs):
             setattr(self, key, kwargs[key])
 
         if is_old_facts:
+            self.results['ansible_facts'] = dict()
             if self.location and self.publisher and self.offer and self.sku and self.version:
                 self.results['ansible_facts']['azure_vmimages'] = self.get_item()
             elif self.location and self.publisher and self.offer and self.sku:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py
index 849b35e560faf5..ab507159716bf9 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py
@@ -280,9 +280,6 @@ def __init__(self):
 
         self.results = dict(
             changed=False,
-            ansible_facts=dict(
-                azure_vmss=[]
-            )
         )
 
         self.name = None
@@ -308,20 +305,20 @@ def exec_module(self, **kwargs):
         if self.name and not self.resource_group:
             self.fail("Parameter error: resource group required when filtering by name.")
 
+        if self.name:
+            result = self.get_item()
+        else:
+            result = self.list_items()
         if is_old_facts:
-            if self.name:
-                self.results['ansible_facts']['azure_vmss'] = self.get_item()
-            else:
-                self.results['ansible_facts']['azure_vmss'] = self.list_items()
+            self.results['ansible_facts'] = {
+                'azure_vmss': result
+            }
         else:
-            if self.name:
-                self.results['vmss'] = self.get_item()
-            else:
-                self.results['vmss'] = self.list_items()
+            self.results['vmss'] = result
 
         if self.format == 'curated':
-            for index in range(len(self.results['ansible_facts']['azure_vmss'])):
-                vmss = self.results['ansible_facts']['azure_vmss'][index]
+            for index in range(len(result)):
+                vmss = result[index]
                 subnet_name = None
                 load_balancer_name = None
                 virtual_network_name = None
@@ -385,13 +382,11 @@ def exec_module(self, **kwargs):
                     'tags': vmss.get('tags')
                 }
 
-                self.results['ansible_facts']['azure_vmss'][index] = updated
+                result[index] = updated
 
             # proper result format we want to support in the future
             # dropping 'ansible_facts' and shorter name 'vmss'
-            self.results['vmss'] = self.results['ansible_facts']['azure_vmss']
-            if not is_old_facts:
-                self.results.pop('ansible_facts', None)
+            self.results['vmss'] = result['azure_vmss']
 
         return self.results
 
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py
index 5e785ff2635cef..d52ff1bfe51253 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualnetwork_info.py
@@ -214,7 +214,6 @@ def __init__(self):
 
         self.results = dict(
             changed=False,
-            ansible_facts=dict(azure_virtualnetworks=[]),
             virtualnetworks=[]
         )
 
@@ -242,7 +241,9 @@ def exec_module(self, **kwargs):
             results = self.list_items()
 
         if is_old_facts:
-            self.results['ansible_facts']['azure_virtualnetworks'] = self.serialize(results)
+            self.results['ansible_facts'] = {
+                'azure_virtualnetworks': self.serialize(results)
+            }
         self.results['virtualnetworks'] = self.curated(results)
 
         return self.results


Code review comments:
diff_hunk: @@ -385,13 +382,11 @@ def exec_module(self, **kwargs):
                     'tags': vmss.get('tags')
                 }
 
-                self.results['ansible_facts']['azure_vmss'][index] = updated
+                result[index] = updated
 
             # proper result format we want to support in the future
             # dropping 'ansible_facts' and shorter name 'vmss'
-            self.results['vmss'] = self.results['ansible_facts']['azure_vmss']
-            if not is_old_facts:
-                self.results.pop('ansible_facts', None)
+            self.results['vmss'] = result['azure_vmss']
path: lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset_info.py
text: @author:
```suggestion
            self.results['vmss'] = result
```