Repository: ansible/ansible-modules-core

Issue:
apt_key always fails to import a subkey
<!--- 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

<!--- Name of the plugin/module/task -->

apt_key
##### ANSIBLE VERSION

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

```
ansible 2.1.1.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.
-->

host OS: Ubuntu 14.04
controlled nodes' OS: Ubuntu 14.04
##### SUMMARY

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

Importing a (sign only) subkey with apt_key always fails, however the actual keyring gets created and contains the correct (sub)key.
##### 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 -->

```
 $ cat >> aptrepo.yml <<EOF
    ---

    - hosts: all
      become: True
      tasks:
        - name: import custom repo keyring
          apt_key: >
            id=A254F5F0
            keyserver=keyserver.ubuntu.com
 EOF

$ ansible-playbook aptrepo.yml

```

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

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

The specified (sub)key gets successfuly imported, ansible returns exit code 0 (success)
##### ACTUAL RESULTS

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

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

```
   fatal: [saceph-osd2.maas]: FAILED! => {"changed": false, "failed": true, "id": "A254F5F0", "msg": "key does not seem to have been added"}
   fatal: [saceph-osd1.maas]: FAILED! => {"changed": false, "failed": true, "id": "A254F5F0", "msg": "key does not seem to have been added"}
   fatal: [saceph-osd3.maas]: FAILED! => {"changed": false, "failed": true, "id": "A254F5F0", "msg": "key does not seem to have been added"}

However the key has been successfully imported:

$ ansible all -m shell -a 'apt-key --keyring /etc/apt/trusted.gpg.d/ceph.gpg adv --list-public-keys | grep -e A254F5F0'

    saceph-osd2.maas | SUCCESS | rc=0 >>
    sub   4096R/A254F5F0 2016-07-29 [expires: 2026-07-27]

    saceph-osd1.maas | SUCCESS | rc=0 >>
    sub   4096R/A254F5F0 2016-07-29 [expires: 2026-07-27]

    saceph-osd3.maas | SUCCESS | rc=0 >>
    sub   4096R/A254F5F0 2016-07-29 [expires: 2026-07-27]

```



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
@@ -132,7 +132,7 @@ def all_keys(module, keyring, short_format):
     results = []
     lines = out.split('\n')
     for line in lines:
-        if line.startswith("pub"):
+        if line.startswith("pub") or line.startswith("sub"):
             tokens = line.split()
             code = tokens[1]
             (len_type, real_code) = code.split("/")


Test patch: