Repository: ansible/ansible
Pull request: Fixes #30863: support update source_address_prefix in azure_rm_securitygroup
##### SUMMARY
<!--- Describe the change, including rationale and design decisions -->

Fixes #30863
Fix the security group cannot update source data prefix problem

<!---
If you are fixing an existing issue, please include "Fixes #nnn" in your
commit message and your description; but you should still explain what
the change does.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bugfix Pull Request

##### COMPONENT NAME
<!--- Name of the module/plugin/module/task -->
azure_rm_securitygroup

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.4
```


##### ADDITIONAL INFORMATION
<!---
Include additional information to help people understand the change here.
For bugs that don't have a linked bug report, a step-by-step reproduction
of the problem is helpful.
  -->

<!--- Paste verbatim command output below, e.g. before and after your change -->
```

```


Problem statement:
Try to update azure_rm_securitygroup : source_address_prefix
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->

`azure_rm_securitygroup`

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.4.0
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).

-->

##### OS / ENVIRONMENT

CentOS 7.3

Steps of the Ansible installation
```
sudo yum check-update; sudo yum install -y gcc libffi-devel python-devel openssl-devel epel-release
sudo yum install -y python-pip python-wheel

sudo pip install ansible
sudo pip install ansible[azure]
sudo pip install dnspython
```

##### SUMMARY

Hello,

When I try to update network security group rules for Azure, in fact the option **source_address_prefix**, the value updated in the YML is not updated in Azure. However option like destination_port_range are updated when modified

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->

Original YML

```yaml

  - name: Create security group that allows SSH and HTTP
    azure_rm_securitygroup: 
      resource_group: '{{ resourceGroup }}'
      name: 'NSG-{{ nsgName }}'
      purge_rules: yes
      state: present
      rules: 
        - name: SSH-Ansible
          protocol: Tcp
          destination_port_range: 22
          access: Allow
          priority: 100
          direction: Inbound
          source_address_prefix: 1.1.1.1


```

Modified YML

```yaml

  - name: Create security group that allows SSH and HTTP
    azure_rm_securitygroup: 
      resource_group: '{{ resourceGroup }}'
      name: 'NSG-{{ nsgName }}'
      purge_rules: yes
      state: present
      rules: 
        - name: SSH-Ansible
          protocol: Tcp
          destination_port_range: 22
          access: Allow
          priority: 100
          direction: Inbound
          source_address_prefix: 2.2.2.2


```
_strings in {{ }} are parameters passed with --extra-vars_

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS

The IP source has to be updated in Azure.

##### ACTUAL RESULTS

Nothing is updated

Hints from issue 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 --->

Commit message: compare source address prefix

Patch to review:
diff --git a/lib/ansible/modules/cloud/azure/azure_rm_securitygroup.py b/lib/ansible/modules/cloud/azure/azure_rm_securitygroup.py
index c934eddb27b603..9a4653f59fed98 100644
--- a/lib/ansible/modules/cloud/azure/azure_rm_securitygroup.py
+++ b/lib/ansible/modules/cloud/azure/azure_rm_securitygroup.py
@@ -426,6 +426,9 @@ def compare_rules(r, rule):
         if rule['direction'] != r['direction']:
             changed = True
             r['direction'] = rule['direction']
+        if rule['source_address_prefix'] != str(r['source_address_prefix']):
+            changed = True
+            r['source_address_prefix'] = rule['source_address_prefix']
     return matched, changed
 
 
diff --git a/test/integration/targets/azure_rm_securitygroup/tasks/main.yml b/test/integration/targets/azure_rm_securitygroup/tasks/main.yml
index 82aed4269d50ac..50eaa8d0d2794b 100644
--- a/test/integration/targets/azure_rm_securitygroup/tasks/main.yml
+++ b/test/integration/targets/azure_rm_securitygroup/tasks/main.yml
@@ -41,11 +41,12 @@
       resource_group: "{{ resource_group }}"
       name: mysecgroup
       rules:
-          - name: DenySSH
+          - name: 'AllowSSH'
             protocol: Tcp
-            destination_port_range: 22-23
-            access: Deny
-            priority: 100
+            source_address_prefix: '174.108.158.0/24'
+            destination_port_range: 22
+            access: Allow
+            priority: 101
           - name: AllowSSHFromHome
             protocol: Tcp
             source_address_prefix: '174.109.158.0/24'
@@ -53,18 +54,22 @@
             priority: 102
   register: output
 
-- assert: { that: "{{ output.state.rules | length }} == 3" }
+- assert: 
+      that:
+          - "{{ output.state.rules | length }} == 3"
+          - output.state.rules[1].source_address_prefix == '174.108.158.0/24'
 
 - name: Test idempotence
   azure_rm_securitygroup:
       resource_group: "{{ resource_group }}"
       name: mysecgroup
       rules:
-          - name: DenySSH
+          - name: 'AllowSSH'
             protocol: Tcp
-            destination_port_range: 22-23
-            access: Deny
-            priority: 100
+            source_address_prefix: '174.108.158.0/24'
+            destination_port_range: 22
+            access: Allow
+            priority: 101
           - name: AllowSSHFromHome
             protocol: Tcp
             source_address_prefix: '174.109.158.0/24'


Code review comments:
diff_hunk: @@ -41,30 +41,35 @@
       resource_group: "{{ resource_group }}"
       name: mysecgroup
       rules:
-          - name: DenySSH
+          - name: 'AllowSSH'
path: test/integration/targets/azure_rm_securitygroup/tasks/main.yml
text: @user1:
quote is unnecessary

diff_hunk: @@ -41,30 +41,35 @@
       resource_group: "{{ resource_group }}"
       name: mysecgroup
       rules:
-          - name: DenySSH
+          - name: 'AllowSSH'
             protocol: Tcp
-            destination_port_range: 22-23
-            access: Deny
-            priority: 100
+            source_address_prefix: '174.108.158.0/24'
+            destination_port_range: 22
+            access: Allow
+            priority: 101
           - name: AllowSSHFromHome
             protocol: Tcp
             source_address_prefix: '174.109.158.0/24'
             destination_port_range: 22-23
             priority: 102
   register: output
 
-- assert: { that: "{{ output.state.rules | length }} == 3" }
+- assert: 
+      that:
+          - "{{ output.state.rules | length }} == 3"
+          - output.state.rules[1].source_address_prefix == '174.108.158.0/24'
 
 - name: Test idempotence
   azure_rm_securitygroup:
       resource_group: "{{ resource_group }}"
       name: mysecgroup
       rules:
-          - name: DenySSH
+          - name: 'AllowSSH'
path: test/integration/targets/azure_rm_securitygroup/tasks/main.yml
text: @user1:
remove quote