Repository: ansible/ansible-modules-core

Issue:
apt_key fails to fetch GPG key sometimes
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

apt_key
##### ANSIBLE VERSION

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

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

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

Ubuntu 16.04 (Xenial)
##### SUMMARY

<!--- Explain the problem briefly -->

ansible node1 -b --become-user=root apt_key -m apt_key -a "keyserver=hkp://p80.pool.sks-keyservers.net:80 id=58118E89F3A912897C070ADBF76221572C52609D"
sometimes has error:
gpg: requesting key 2C52609D from hkp server p80.pool.sks-keyservers.net\ngpg: no valid OpenPGP data found.\ngpg: Total number processed: 0\ngpg: keyserver communications error: keyserver helper general error\ngpg: keyserver communications error: unknown pubkey algorithm\ngpg: keyserver receive failed: unknown pubkey algorithm

Repeating would mitigate this problem.
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

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

```
ansible node1 -b --become-user=root apt_key -m apt_key -a "keyserver=hkp://p80.pool.sks-keyservers.net:80 id=58118E89F3A912897C070ADBF76221572C52609D"
```

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

<!--- What did you expect to happen when running the steps above? -->
##### ACTUAL RESULTS

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

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

```

```

apt_key fails to fetch GPG key sometimes
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

apt_key
##### ANSIBLE VERSION

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

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

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

Ubuntu 16.04 (Xenial)
##### SUMMARY

<!--- Explain the problem briefly -->

ansible node1 -b --become-user=root apt_key -m apt_key -a "keyserver=hkp://p80.pool.sks-keyservers.net:80 id=58118E89F3A912897C070ADBF76221572C52609D"
sometimes has error:
gpg: requesting key 2C52609D from hkp server p80.pool.sks-keyservers.net\ngpg: no valid OpenPGP data found.\ngpg: Total number processed: 0\ngpg: keyserver communications error: keyserver helper general error\ngpg: keyserver communications error: unknown pubkey algorithm\ngpg: keyserver receive failed: unknown pubkey algorithm

Repeating would mitigate this problem.
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

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

```
ansible node1 -b --become-user=root apt_key -m apt_key -a "keyserver=hkp://p80.pool.sks-keyservers.net:80 id=58118E89F3A912897C070ADBF76221572C52609D"
```

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

<!--- What did you expect to happen when running the steps above? -->
##### ACTUAL RESULTS

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

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

```

```



Discussion:

Resolving patch:
diff --git a/packaging/os/apt_key.py b/packaging/os/apt_key.py
--- a/packaging/os/apt_key.py
+++ b/packaging/os/apt_key.py
@@ -171,7 +171,14 @@ def import_key(module, keyring, keyserver, key_id):
         cmd = "apt-key --keyring %s adv --keyserver %s --recv %s" % (keyring, keyserver, key_id)
     else:
         cmd = "apt-key adv --keyserver %s --recv %s" % (keyserver, key_id)
-    (rc, out, err) = module.run_command(cmd, check_rc=True)
+    for retry in xrange(5):
+        (rc, out, err) = module.run_command(cmd)
+        if rc == 0:
+            break
+    else:
+        # Out of retries
+        module.fail_json(cmd=cmd, msg="error fetching key from keyserver: %s" % keyserver,
+                         rc=rc, stdout=out, stderr=err)
     return True
 
 def add_key(module, keyfile, keyring, data=None):


Test patch: