Issue 1:
ISSUE TYPE

Bug Report

COMPONENT NAME

apt_key

ANSIBLE VERSION

ansible 2.4.0.0
config file = None
configured module search path = [u'/home/mg/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/mg/.venv/local/lib/python2.7/site-packages/ansible
executable location = /home/mg/.venv/bin/ansible
python version = 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]

CONFIGURATION

N/A

OS / ENVIRONMENT

Ubuntu

SUMMARY

ansible <hostname> -m apt_key -a url=https://downloads.linux.hpe.com/SDR/hpePublicKey2048_key1.pub --check

goes ahead and adds the apt key, ignoring the --check mode.

STEPS TO REPRODUCE

See above.

EXPECTED RESULTS

No key is added; status is "changed".

ACTUAL RESULTS

Key is added; status is "changed".

Issue 2:
From @selivan on 2016-06-15T16:15:11Z

ISSUE TYPE

Feature idea

COMPONENT NAME

apt_key

ANSIBLE VERSION

ansible --version:

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

CONFIGURATION

OS / ENVIRONMENT

Management host: Ubuntu 14.04 Trusty

Controlled host: Ubuntu 14.04 Trusty

SUMMARY

If apt_key module takes keyfile from data="{{ lookup('file', 'key.gpg') }}", it always marks task as "changed" in --check mode, despite that this key is present in apt keyring on target host. In normal mode is marks this task as "ok".

STEPS TO REPRODUCE

roles/common-monitoring/tasks/main.yml:

- name: add zabbix repository apt key
apt_key: data="{{ lookup('file', 'zabbix-official-repo.key') }}" state=present

roles/common-monitoring/files/zabbix-official-repo.key : https://gist.github.com/selivan/44d970b55b75a6435d02f2b42dbe44db

playbook.yml:

- hosts: remote_host
become: yes
roles:
- { role: common-monitoring, tags: ['common-monitoring'] }

ansible-playbook playbook.yml --check

EXPECTED RESULTS

Task should be reported as "ok" because key already exists in apt keyring on remote host.

ACTUAL RESULTS

Task is always reported as "changed".

ansible-playbook playbook.yml --check -vvv:

TASK [common-monitoring : add zabbix repository apt key] ***********************
task path: /home/XXXXXX/ansible/roles/common-monitoring/tasks/main.yml:3
File lookup using /home/XXXXXX/ansible/roles/common-monitoring/files/zabbix-official-repo.key as file
<XX.XX.XX.XX> ESTABLISH SSH CONNECTION FOR USER: None
<XX.XX.XX.XX> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=g
ssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/XXXXXX/.ansible/cp/ansib
le-ssh-%h-%p-%r 172.31.113.12 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1466006828.99-150895880810391 `" && e
cho ansible-tmp-1466006828.99-150895880810391="` echo $HOME/.ansible/tmp/ansible-tmp-1466006828.99-150895880810391 `" ) && sleep 0'"'"''
<XX.XX.XX.XX> PUT /tmp/tmpSGoEiZ TO /home/XXXXXX/.ansible/tmp/ansible-tmp-1466006828.99-150895880810391/apt_key
<XX.XX.XX.XX> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthenticat
ions=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/XXXXXX/.ansible/cp
/ansible-ssh-%h-%p-%r '[172.31.113.12]'
<XX.XX.XX.XX> ESTABLISH SSH CONNECTION FOR USER: None
<XX.XX.XX.XX> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=g
ssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/XXXXXX/.ansible/cp/ansib
le-ssh-%h-%p-%r -tt 172.31.113.12 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-rstyhbjupbasyihpfmmshmtlc
qyjhfhu; LANG=C LC_ALL=C LC_MESSAGES=C /usr/bin/python /home/XXXXXX/.ansible/tmp/ansible-tmp-1466006828.99-150895880810391/apt_key; rm -rf "/ho
me/XXXXXX/.ansible/tmp/ansible-tmp-1466006828.99-150895880810391/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
changed: [log01] => {"changed": true, "invocation": {"module_args": {"data": "-----BEGIN PGP PUBLIC KEY BLOCK-----\nVersion: GnuPG v1.4.5 (GNU/Lin
ux)\n\nmQGiBFCNJaYRBAC4nIW8o2NyOIswb82Xn3AYSMUcNZuKB2fMtpu0WxSXIRiX2BwC\nYXx8cIEQVYtLRBL5o0JdmoNCjW6jd5fOVem3EmOcPksvzzRWonIgFHf4EI2n1KJc\nJXX/nDC
+eoh5xW35mRNFN/BEJHxxiRGGbp2MCnApwgrZLhOujaCGAwavGwCgiG4D\nwKMZ4xX6Y2Gv3MSuzMIT0bcEAKYn3WohS+udp0yC3FHDj+oxfuHpklu1xuI3y6ha\n402aEFahNi3wr316ukgdP
AYLbpz76ivoouTJ/U2MqbNLjAspDvlnHXXyqPM5GC6K\njtXPqNrRMUCrwisoAhorGUg/+S5pyXwsWcJ6EKmA80pR9HO+TbsELE5bGe/oc238\nt/2oBAC3zcQ46wPvXpMCNFb+ED71qDOlnDY
aaAPbjgkvnp+WN6nZFFyevjx180Kw\nqWOLnlNP6JOuFW27MP75MDPDpbAAOVENp6qnuW9dxXTN80YpPLKUxrQS8vWPnzkY\nWtUfF75pEOACFVTgXIqEgW0E6oww2HJi9zF5fS8IlFHJztNYt
bQgWmFiYml4IFNJ\nQSA8cGFja2FnZXJAemFiYml4LmNvbT6IYAQTEQIAIAUCUI0lpgIbAwYLCQgHAwIE\nFQIIAwQWAgMBAh4BAheAAAoJENE9WOR56l7UhUwAmgIGZ39U6D2w2oIWDD8m7KV
3\noI06AJ9EnOxMMlxEjTkt9lEvGhEX1bEh7bkBDQRQjSWmEAQAqx+ecOzBbhqMq5hU\nl39cJ6l4aocz6EZ9mSSoF/g+HFz6WYnPAfRaYyfLmZdtF5rGBDD4ysalYG5yD59R\nMv5tNVf/CEx
+JAPMhp6JCBkGRaH+xHws4eBPGkea4rGNVP3L3rA7g+c1YXZICGRI\nOOH7CIzIZ/w6aFGsPp7xM35ogncAAwUD/3s8Nc1OLDy81DC6rGpxfEURd5pvd/j0\nD5Di0WSBEcHXp5nThDz6ro/Vr
0/FVIBtT97tmBHX27yBS3PqxxNRIjZ0GSWQqdws\nQ8o3YT+RHjBugXn8CzTOvIn+2QNMA8EtGIZPpCblJv8q6MFPi9m7avQxguMqufgg\nfAk7377Rt9RqiEkEGBECAAkFAlCNJaYCGwwACgk
Q0T1Y5HnqXtQx4wCfcJZINKVq\nkQIoV3KTQAIzr6IvbZoAn12XXt4GP89xHuzPDZ86YJVAgnfK\n=+200\n-----END PGP PUBLIC KEY BLOCK-----", "file": null, "id": null,
"key": null, "keyring": null, "keyserver": null, "state": "present", "url": null, "validate_certs": true}, "module_name": "apt_key"}}

Copied from original issue: ansible/ansible-modules-core#3958