mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
List azure resources under resource group in azure_rm_resourcegroup_facts (#54322)
This commit is contained in:
parent
9e42038747
commit
9660805380
2 changed files with 79 additions and 14 deletions
|
@ -205,7 +205,8 @@ class AzureRMResourceGroup(AzureRMModuleBase):
|
|||
elif self.state == 'absent':
|
||||
if contains_resources and not self.force_delete_nonempty:
|
||||
self.fail("Error removing resource group {0}. Resources exist within the group. "
|
||||
"Use `force_delete_nonempty` to force delete.".format(self.name))
|
||||
"Use `force_delete_nonempty` to force delete. "
|
||||
"To list resources under {0}, use `azure_rm_resourcegroup_facts` module with `list_resources` option.".format(self.name))
|
||||
self.delete_resource_group()
|
||||
|
||||
return self.results
|
||||
|
|
|
@ -32,6 +32,11 @@ options:
|
|||
tags:
|
||||
description:
|
||||
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
|
||||
list_resources:
|
||||
description:
|
||||
- List all resources under the resource group.
|
||||
- Note this will cost network overhead for each resource group. Suggest use this when C(name) set.
|
||||
version_added: 2.8
|
||||
|
||||
extends_documentation_fragment:
|
||||
- azure
|
||||
|
@ -55,24 +60,64 @@ EXAMPLES = '''
|
|||
tags:
|
||||
- testing
|
||||
- foo:bar
|
||||
|
||||
- name: List resources under resource group
|
||||
azure_rm_resourcegroup_facts:
|
||||
name: foo
|
||||
list_resources: yes
|
||||
'''
|
||||
RETURN = '''
|
||||
azure_resourcegroups:
|
||||
description: List of resource group dicts.
|
||||
returned: always
|
||||
type: list
|
||||
example: [{
|
||||
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup",
|
||||
"location": "westus",
|
||||
"name": "Testing",
|
||||
"properties": {
|
||||
"provisioningState": "Succeeded"
|
||||
},
|
||||
"tags": {
|
||||
"delete": "never",
|
||||
"testing": "testing"
|
||||
}
|
||||
}]
|
||||
contains:
|
||||
id:
|
||||
description:
|
||||
- Resource id.
|
||||
type: str
|
||||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/foo"
|
||||
name:
|
||||
description:
|
||||
- Resource group name.
|
||||
type: str
|
||||
sample: foo
|
||||
tags:
|
||||
description:
|
||||
- Tags to assign to the managed disk.
|
||||
type: dict
|
||||
sample: { "tag": "value" }
|
||||
resources:
|
||||
description:
|
||||
- List of resources under the resource group.
|
||||
- Only shows when C(list_resources) set to C(True).
|
||||
type: list
|
||||
contains:
|
||||
id:
|
||||
description:
|
||||
- Resource id.
|
||||
type: str
|
||||
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/foo/providers/Microsoft.XX/XXX/bar"
|
||||
name:
|
||||
description:
|
||||
- Resource name.
|
||||
type: str
|
||||
sample: bar
|
||||
location:
|
||||
description:
|
||||
- Resource region.
|
||||
type: str
|
||||
sample: eastus
|
||||
type:
|
||||
description:
|
||||
- Resource type.
|
||||
type: str
|
||||
sample: "Microsoft.Compute/virtualMachines"
|
||||
tags:
|
||||
description:
|
||||
- Tags to assign to the managed disk.
|
||||
type: dict
|
||||
sample: { "tag": "value" }
|
||||
'''
|
||||
|
||||
try:
|
||||
|
@ -93,7 +138,8 @@ class AzureRMResourceGroupFacts(AzureRMModuleBase):
|
|||
|
||||
self.module_arg_spec = dict(
|
||||
name=dict(type='str'),
|
||||
tags=dict(type='list')
|
||||
tags=dict(type='list'),
|
||||
list_resources=dict(type='bool')
|
||||
)
|
||||
|
||||
self.results = dict(
|
||||
|
@ -103,6 +149,7 @@ class AzureRMResourceGroupFacts(AzureRMModuleBase):
|
|||
|
||||
self.name = None
|
||||
self.tags = None
|
||||
self.list_resources = None
|
||||
|
||||
super(AzureRMResourceGroupFacts, self).__init__(self.module_arg_spec,
|
||||
supports_tags=False,
|
||||
|
@ -118,6 +165,10 @@ class AzureRMResourceGroupFacts(AzureRMModuleBase):
|
|||
else:
|
||||
self.results['ansible_facts']['azure_resourcegroups'] = self.list_items()
|
||||
|
||||
if self.list_resources:
|
||||
for item in self.results['ansible_facts']['azure_resourcegroups']:
|
||||
item['resources'] = self.list_by_rg(item['name'])
|
||||
|
||||
return self.results
|
||||
|
||||
def get_item(self):
|
||||
|
@ -148,6 +199,19 @@ class AzureRMResourceGroupFacts(AzureRMModuleBase):
|
|||
results.append(self.serialize_obj(item, AZURE_OBJECT_CLASS))
|
||||
return results
|
||||
|
||||
def list_by_rg(self, name):
|
||||
self.log('List resources under resource group')
|
||||
results = []
|
||||
try:
|
||||
response = self.rm_client.resources.list_by_resource_group(name)
|
||||
while True:
|
||||
results.append(response.next().as_dict())
|
||||
except StopIteration:
|
||||
pass
|
||||
except CloudError as exc:
|
||||
self.fail('Error when listing resources under resource group {0}: {1}'.format(name, exc.message or str(exc)))
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
AzureRMResourceGroupFacts()
|
||||
|
|
Loading…
Reference in a new issue