Repository: ansible/ansible-modules-extras

Issue:
Zypper modules fails when running with --diff
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

zypper
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.2.0.0
  config file = /home1/users/hv11267/integration-infrastructure/mule-platform/ansible.cfg
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

override for roles_path, vault_password_file, ask_pass, transport and ssh pipelining
##### OS / ENVIRONMENT

control:

```
$ lsb-release -a
LSB Version:    n/a
Distributor ID: SUSE LINUX
Description:    SUSE Linux Enterprise Server 12 SP1
Release:        12.1
Codename:       n/a
```

target:

```
$ lsb-release -a
LSB Version:    n/a
Distributor ID: SUSE LINUX
Description:    SUSE Linux Enterprise Server 12
Release:        12
Codename:       12
```
##### SUMMARY

Running the zypper module with diff mode fails
##### STEPS TO REPRODUCE

Run a task using the zypper module in diff mode.
The wanted package is already present and up-to-date

<!--- Paste example playbooks or commands between quotes below -->

```
- name: Install Java
  zypper: name=java{{ java_version }}
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

successful run showing package diff
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with high verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->

```
TASK [java : Install Java] ************************************************
fatal: [bhvxbas001]: FAILED! => {"changed": false, "failed": true, "module_stderr": "", "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_xN1pW1/ansible_module_zypper.py\", line 448, in <module>\r\n    main()\r\n  File \"/tmp/ansible_xN1pW1/ansible_module_zypper.py\", line 434, in main\r\n    set_diff(module, retvals, packages_changed)\r\n  File \"/tmp/ansible_xN1pW1/ansible_module_zypper.py\", line 270, in set_diff\r\n    for p in result:\r\nTypeError: 'NoneType' object is not iterable\r\n", "msg": "MODULE FAILURE"}
```



Discussion:

Resolving patch:
diff --git a/packaging/os/zypper.py b/packaging/os/zypper.py
--- a/packaging/os/zypper.py
+++ b/packaging/os/zypper.py
@@ -267,15 +267,16 @@ def get_cmd(m, subcommand):
 def set_diff(m, retvals, result):
     # TODO: if there is only one package, set before/after to version numbers
     packages = {'installed': [], 'removed': [], 'upgraded': []}
-    for p in result:
-        group = result[p]['group']
-        if group == 'to-upgrade':
-            versions = ' (' + result[p]['oldversion'] + ' => ' + result[p]['version'] + ')'
-            packages['upgraded'].append(p + versions)
-        elif group == 'to-install':
-            packages['installed'].append(p)
-        elif group == 'to-remove':
-            packages['removed'].append(p)
+    if result:
+        for p in result:
+            group = result[p]['group']
+            if group == 'to-upgrade':
+                versions = ' (' + result[p]['oldversion'] + ' => ' + result[p]['version'] + ')'
+                packages['upgraded'].append(p + versions)
+            elif group == 'to-install':
+                packages['installed'].append(p)
+            elif group == 'to-remove':
+                packages['removed'].append(p)
 
     output = ''
     for state in packages:


Test patch: