Repository: ansible/ansible-modules-extras

Issue:
zypper_repository: Adding repo with import of custom GPG key fails due to wrong command line option ordering
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

packaging/os/zypper_repository.py
##### ANSIBLE VERSION

```
ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
```

Latest Git versions of the `packaging/os/zypper.py` and `packaging/os/zypper_repository.py` modules placed in `library/`
##### CONFIGURATION

Running Vagrant with the `ansible_local` provisioner
##### OS / ENVIRONMENT

openSUSE Tumbleweed as Vagrant Base Box
##### SUMMARY

When adding a zypper repository that requires a custom GPG key to be imported, the zypper_repository module fails because it puts the `--gpg-auto-import-keys` command line option at the wrong place in the command line:

```
/usr/bin/zypper --quiet --non-interactive refresh --force --gpg-auto-import-keys -r http://download.opensuse.org/repositories/Apache:/Modules/openSUSE_Tumbleweed/
```

Zypper differentiates between options _before_ the command (here `refresh`) and _behind_ it. The correct call would be:

```
/usr/bin/zypper --quiet --non-interactive --gpg-auto-import-keys refresh --force -r http://download.opensuse.org/repositories/Apache:/Modules/openSUSE_Tumbleweed/
```
##### STEPS TO REPRODUCE

Create a playbook such as the following:

```

---
- hosts: all

  tasks:
  - name: "Test adding a repo with custom GPG key"
    zypper_repository:
      name: "Apache_Modules"
      repo: "http://download.opensuse.org/repositories/Apache:/Modules/openSUSE_Tumbleweed/"
      priority: 100
      auto_import_keys: true
      state: "present"
```

Run Ansible
##### EXPECTED RESULTS

Repository added successfully
##### ACTUAL RESULTS

```
TASK [Required extra repository for auth_schemes defined] **********************
task path: /vagrant/usbstick/ansible/repo-playbook.yml:5
<zeus> ESTABLISH LOCAL CONNECTION FOR USER: vagrant
<zeus> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1475574977.19-103534347658621 `" && echo ansible-tmp-1475574977.19-103534347658621="` echo $HOME/.ansible/tmp/ansible-tmp-1475574977.19-103534347658621 `" ) && sleep 0'
<zeus> PUT /tmp/tmpaE6SGq TO /home/vagrant/.ansible/tmp/ansible-tmp-1475574977.19-103534347658621/zypper_repository
<zeus> EXEC /bin/sh -c 'sudo -H -S -n -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ttdgngwywjmakvvnqrltktyuyopcwlhr; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/vagrant/.ansible/tmp/ansible-tmp-1475574977.19-103534347658621/zypper_repository; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1475574977.19-103534347658621/" > /dev/null 2>&1'"'"' && sleep 0'
fatal: [zeus]: FAILED! => {"changed": false, "cmd": "/usr/bin/zypper --quiet --non-interactive refresh --force --gpg-auto-import-keys -r http://download.opensuse.org/repositories/Apache:/Modules/openSUSE_Tumbleweed/", "failed": true, "invocation": {"module_args": {"auto_import_keys": true, "autorefresh": true, "description": null, "disable_gpg_check": false, "enabled": true, "name": "Apache_Modules", "overwrite_multiple": false, "priority": 100, "repo": "http://download.opensuse.org/repositories/Apache:/Modules/openSUSE_Tumbleweed/", "runrefresh": false, "state": "present"}, "module_name": "zypper_repository"}, "msg": "Unknown option '--gpg-auto-import-keys'", "rc": 2, "stderr": "Unknown option '--gpg-auto-import-keys'\n", "stdout": "", "stdout_lines": []}
```



Discussion:
<!-- @pief: Thanks for filing this bug report! -->

@matze, @robinro, ping. This issue is waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->


Resolving patch:
diff --git a/packaging/os/zypper_repository.py b/packaging/os/zypper_repository.py
--- a/packaging/os/zypper_repository.py
+++ b/packaging/os/zypper_repository.py
@@ -274,9 +274,10 @@ def get_zypper_version(module):
 
 def runrefreshrepo(module, auto_import_keys=False, shortname=None):
     "Forces zypper to refresh repo metadata."
-    cmd = _get_cmd('refresh', '--force')
     if auto_import_keys:
-        cmd.append('--gpg-auto-import-keys')
+        cmd = _get_cmd('--gpg-auto-import-keys', 'refresh', '--force')
+    else:
+        cmd = _get_cmd('refresh', '--force')
     if shortname is not None:
         cmd.extend(['-r', shortname])
 


Test patch: