Repository: ansible/ansible
Pull request: Fix for Azure loadbalancer tags.
It was possible to add tags to an Azure loadbalancer, but the tags were never set in Azure.
This patch fixes that.

##### SUMMARY
It was possible to add tags to an Azure loadbalancer in a playbook, but the tags were never set in Azure. This patch fixes that.

fixes #39896 

##### ISSUE TYPE
 - Bugfix Pull Request

##### COMPONENT NAME
azure_rm_loadbalancer

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
```


##### ADDITIONAL INFORMATION


Problem statement:
azure_rm_loadbalancer does not set tags
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
azure_rm_loadbalancer 

##### ANSIBLE VERSION
```
ansible 2.6.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
```

##### CONFIGURATION
```
DEFAULT_TIMEOUT(/etc/ansible/ansible.cfg) = 120
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False
```

##### OS / ENVIRONMENT
N/A

##### SUMMARY
When creating a loadbalancer with tags, the tags do not show up in azure.

##### STEPS TO REPRODUCE
Create a load balancer with tags:
```yaml
    - name: Create loadbalancer
      azure_rm_loadbalancer:
        name: "mylb"
...
        tags:
          "mytag": "myvalue"
```

##### EXPECTED RESULTS
The tag has been added to the load balancer

##### ACTUAL RESULTS
The tag is not there

##### Patch
The following patch fixes my issue; not sure it will actually honor the update_tags parameter.

```
--- a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py
@@ -620,7 +620,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):

     def exec_module(self, **kwargs):
         """Main module execution method"""
-        for key in self.module_args.keys():
+        for key in self.module_args.keys() + ['tags']:
             setattr(self, key, kwargs[key])

         changed = False
@@ -761,6 +761,7 @@ class AzureRMLoadBalancer(AzureRMModuleBase):
             param = self.network_models.LoadBalancer(
                 sku=self.network_models.LoadBalancerSku(self.sku) if self.sku else None,
                 location=self.location,
+                tags=self.tags,
                 frontend_ip_configurations=frontend_ip_configurations_param,
                 backend_address_pools=backend_address_pools_param,
                 probes=probes_param,
```

Hints from issue discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.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 @trstringer @tstringer @xscript @yuwzho @zikalino
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
+label easyfix

Add a label to welcome contributor to fix this, before I have bandwidth.
@yuwzho Are you requesting me to send a pull request for this fix?
@yuwzho I meet the same error when used azure_rm_loadbalancer to create a loadbalancer with tags, the tag will not create, it need to be fixed. Thanks !

@vogon1 If you are interested in it hhhh 😄 
@yuwzho I created a pull request, you will see the name SietseVisser - that is me as well, it is my job account.

Commit message: azure_rm_loadbalancer_facts requires rg

Getting facts of all loadbalancers via azure_rm_loadbalancer_facts requires a resource group.
This fix adds the rg as parameter to the list() call.

Patch to review:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py
index 8eb43542731e69..8b2ebebd856684 100755
--- a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer.py
@@ -610,6 +610,7 @@ def __init__(self):
         self.natpool_frontend_port_end = None
         self.natpool_backend_port = None
         self.natpool_protocol = None
+        self.tags = None
 
         self.results = dict(changed=False, state=dict())
 
@@ -620,7 +621,7 @@ def __init__(self):
 
     def exec_module(self, **kwargs):
         """Main module execution method"""
-        for key in self.module_args.keys():
+        for key in list(self.module_args.keys()) + ['tags']:
             setattr(self, key, kwargs[key])
 
         changed = False
@@ -685,6 +686,13 @@ def exec_module(self, **kwargs):
             changed = True
 
         self.results['state'] = load_balancer_to_dict(load_balancer)
+        if 'tags' in self.results['state']:
+            update_tags, self.results['state']['tags'] = self.update_tags(self.results['state']['tags'])
+            if update_tags:
+                changed = True
+        else:
+            if self.tags:
+                changed = True
         self.results['changed'] = changed
 
         if self.state == 'present' and changed:
@@ -761,6 +769,7 @@ def exec_module(self, **kwargs):
             param = self.network_models.LoadBalancer(
                 sku=self.network_models.LoadBalancerSku(self.sku) if self.sku else None,
                 location=self.location,
+                tags=self.tags,
                 frontend_ip_configurations=frontend_ip_configurations_param,
                 backend_address_pools=backend_address_pools_param,
                 probes=probes_param,
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
index 1a7f82425424aa..d067b6a53ec4f2 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
@@ -44,6 +44,7 @@
     resource_group:
         description:
             - The resource group to search for the desired load balancer
+        required: true
     tags:
         description:
             - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
@@ -153,7 +154,7 @@ def list_items(self):
         self.log('List all load balancers')
 
         try:
-            response = self.network_client.load_balancers.list()
+            response = self.network_client.load_balancers.list(self.resource_group)
         except AzureHttpError as exc:
             self.fail('Failed to list all items - {}'.format(str(exc)))
 


Code review comments:
diff_hunk: @@ -44,6 +44,7 @@
     resource_group:
         description:
             - The resource group to search for the desired load balancer
+        required: true
path: lib/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
text: @user1:
How about I just want to list all LB in this subscription? It can accept None value