Repository: ansible/ansible

Issue:
ansible_module_azure_rm_loadbalancer_facts.py: The error was: TypeError: list() takes at least 2 arguments (1 given)
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
azure_rm_loadbalancer_facts.py

##### ANSIBLE VERSION
```
ansible 2.4.0
  config file = /home/vmadmin/cluster_build/ansible.cfg
  configured module search path = [u'/home/vmadmin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vmadmin/.local/lib/python2.7/site-packages/ansible
  executable location = /home/vmadmin/.local/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
```
cat ansible.cfg 
[defaults]
host_key_checking = False
```

##### OS / ENVIRONMENT
CentOS 7.2, Azure, Region Western Europe,  `azure-mgmt-network (1.4.0)`

##### SUMMARY
When trying to list all load balancers within an Azure subscription or when trying to list load balancers within a resource group I get a stack trace.

There are two issues here:
* Cannot list all load balancers (as the documentation mentions)
* Cannot list all load balancers within an RG

In the module code you can see:
```
        try:
            response = self.network_client.load_balancers.list()
        except AzureHttpError as exc:
            self.fail('Failed to list all items - {}'.format(str(exc)))
```

However the `list` method is defined as:
```
    def list(
            self, resource_group_name, custom_headers=None, raw=False, **operation_config):
        """
        The List loadBalancer opertion retrieves all the loadbalancers in a
        resource group.
```

##### STEPS TO REPRODUCE
```yaml
- azure_rm_loadbalancer_facts:
    subscription_id: "{{ azure_subscriptionId }}"
    client_id: "{{ azure_clientId }}"
    tenant: "{{ azure_tenantId }}"
    secret: "{{ azure_clientSecret }}"
```

```yaml
- azure_rm_loadbalancer_facts:
    resource_group: "{{ azure_resource_group }}"
    subscription_id: "{{ azure_subscriptionId }}"
    client_id: "{{ azure_clientId }}"
    tenant: "{{ azure_tenantId }}"
    secret: "{{ azure_clientSecret }}"
```

##### EXPECTED RESULTS
Output a list of loadbalancers.

##### ACTUAL RESULTS
```
TASK [azure_load_balancer : Get lbs] *********************************************************************************************
task path: /home/vmadmin/cluster_build/roles/azure_load_balancer/tasks/main.yaml:15
Using module file /home/vmadmin/.local/lib/python2.7/site-packages/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: vmadmin
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814 `" && echo ansible-tmp-1504708358.3-41710485333814="` echo /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814 `" ) && sleep 0'
<localhost> PUT /tmp/tmpy_CiqC TO /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/azure_rm_loadbalancer_facts.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/ /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/azure_rm_loadbalancer_facts.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/azure_rm_loadbalancer_facts.py; rm -rf "/home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 180, in <module>
    main()
  File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 177, in main
    AzureRMLoadBalancerFacts()
  File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 123, in __init__
    facts_module=True
  File "/tmp/ansible_VTaHxN/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 255, in __init__
  File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 133, in exec_module
    else self.list_items()
  File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 162, in list_items
    response = self.network_client.load_balancers.list()
TypeError: list() takes at least 2 arguments (1 given)

fatal: [localhost]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 180, in <module>\n    main()\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 177, in main\n    AzureRMLoadBalancerFacts()\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 123, in __init__\n    facts_module=True\n  File \"/tmp/ansible_VTaHxN/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 255, in __init__\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 133, in exec_module\n    else self.list_items()\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 162, in list_items\n    response = self.network_client.load_balancers.list()\nTypeError: list() takes at least 2 arguments (1 given)\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}
```
@tstringer Hi Thomas, I have tagged you since I believe you have recently done the development for this module. Hope that's okay.


Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
--- a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
@@ -70,6 +70,10 @@
     - name: Get facts for all load balancers
       azure_rm_loadbalancer_facts:
 
+    - name: Get facts for all load balancers in a specific resource group
+      azure_rm_loadbalancer_facts:
+        resource_group: TestRG
+
     - name: Get facts by tags
       azure_rm_loadbalancer_facts:
         tags:
@@ -158,10 +162,16 @@ def list_items(self):
 
         self.log('List all load balancers')
 
-        try:
-            response = self.network_client.load_balancers.list()
-        except AzureHttpError as exc:
-            self.fail('Failed to list all items - {}'.format(str(exc)))
+        if self.resource_group:
+            try:
+                response = self.network_client.load_balancers.list(self.resource_group)
+            except AzureHttpError as exc:
+                self.fail('Failed to list items in resource group {} - {}'.format(self.resource_group, str(exc)))
+        else:
+            try:
+                response = self.network_client.load_balancers.list_all()
+            except AzureHttpError as exc:
+                self.fail('Failed to list all items - {}'.format(str(exc)))
 
         results = []
         for item in response:


Test patch: