| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: (c) 2012, Michael DeHaan <[email protected]> |
| 4 |
# Copyright: (c) 2012, Jayson Vantuyl <[email protected]> |
| 5 |
|
| 6 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
--- |
| 13 |
module: apt_key |
| 14 |
author: |
| 15 |
- Jayson Vantuyl (@jvantuyl) |
| 16 |
version_added: "1.0" |
| 17 |
short_description: Add or remove an apt key |
| 18 |
description: |
| 19 |
- Add or remove an I(apt) key, optionally downloading it. |
| 20 |
extends_documentation_fragment: action_common_attributes |
| 21 |
deprecated: |
| 22 |
alternative: ansible.builtin.deb822_repository |
| 23 |
why: The M(ansible.builtin.apt_key) module is deprecated in favor of the M(ansible.builtin.deb822_repository) module. |
| 24 |
removed_in: "2.25" |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
diff_mode: |
| 29 |
support: none |
| 30 |
platform: |
| 31 |
platforms: debian |
| 32 |
notes: |
| 33 |
- The C(apt-key) command used by this module has been deprecated. See the L(Debian wiki,https://wiki.debian.org/DebianRepository/UseThirdParty) for details. |
| 34 |
This module is kept for backwards compatibility for systems that still use C(apt-key) as the main way to manage apt repository keys. |
| 35 |
- As a sanity check, downloaded key id must match the one specified. |
| 36 |
- "Use full fingerprint (40 characters) key ids to avoid key collisions. |
| 37 |
To generate a full-fingerprint imported key: C(apt-key adv --list-public-keys --with-fingerprint --with-colons)." |
| 38 |
- If you specify both the key O(id) and the O(url) with O(state=present), the task can verify or add the key as needed. |
| 39 |
- Adding a new key requires an apt cache update (e.g. using the M(ansible.builtin.apt) module's C(update_cache) option). |
| 40 |
- The C(apt-key) utility has been deprecated and removed in modern debian versions, use M(ansible.builtin.deb822_repository) as an alternative |
| 41 |
to M(ansible.builtin.apt_repository) + apt_key combinations. |
| 42 |
requirements: |
| 43 |
- gpg |
| 44 |
seealso: |
| 45 |
- module: ansible.builtin.deb822_repository |
| 46 |
options: |
| 47 |
id: |
| 48 |
description: |
| 49 |
- The identifier of the key. |
| 50 |
- Including this allows check mode to correctly report the changed state. |
| 51 |
- If specifying a subkey's id be aware that apt-key does not understand how to remove keys via a subkey id. Specify the primary key's id instead. |
| 52 |
- This parameter is required when O(state) is set to V(absent). |
| 53 |
type: str |
| 54 |
data: |
| 55 |
description: |
| 56 |
- The keyfile contents to add to the keyring. |
| 57 |
type: str |
| 58 |
file: |
| 59 |
description: |
| 60 |
- The path to a keyfile on the remote server to add to the keyring. |
| 61 |
type: path |
| 62 |
keyring: |
| 63 |
description: |
| 64 |
- The full path to specific keyring file in C(/etc/apt/trusted.gpg.d/). |
| 65 |
type: path |
| 66 |
version_added: "1.3" |
| 67 |
url: |
| 68 |
description: |
| 69 |
- The URL to retrieve key from. |
| 70 |
type: str |
| 71 |
keyserver: |
| 72 |
description: |
| 73 |
- The keyserver to retrieve key from. |
| 74 |
type: str |
| 75 |
version_added: "1.6" |
| 76 |
state: |
| 77 |
description: |
| 78 |
- Ensures that the key is present (added) or absent (revoked). |
| 79 |
type: str |
| 80 |
choices: [ absent, present ] |
| 81 |
default: present |
| 82 |
validate_certs: |
| 83 |
description: |
| 84 |
- If V(false), SSL certificates for the target url will not be validated. This should only be used |
| 85 |
on personally controlled sites using self-signed certificates. |
| 86 |
type: bool |
| 87 |
default: 'yes' |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = """ |
| 91 |
- name: One way to avoid apt_key once it is removed from your distro, armored keys should use .asc extension, binary should use .gpg |
| 92 |
block: |
| 93 |
- name: somerepo | no apt key |
| 94 |
ansible.builtin.get_url: |
| 95 |
url: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x36a1d7869245c8950f966e92d8576a8ba88d21e9 |
| 96 |
dest: /etc/apt/keyrings/myrepo.asc |
| 97 |
checksum: sha256:bb42f0db45d46bab5f9ec619e1a47360b94c27142e57aa71f7050d08672309e0 |
| 98 |
|
| 99 |
- name: somerepo | apt source |
| 100 |
ansible.builtin.apt_repository: |
| 101 |
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/myrepo.asc] https://download.example.com/linux/ubuntu {{ ansible_distribution_release }} stable" |
| 102 |
state: present |
| 103 |
|
| 104 |
- name: Add an apt key by id from a keyserver |
| 105 |
ansible.builtin.apt_key: |
| 106 |
keyserver: keyserver.ubuntu.com |
| 107 |
id: 36A1D7869245C8950F966E92D8576A8BA88D21E9 |
| 108 |
|
| 109 |
- name: Add an Apt signing key, uses whichever key is at the URL |
| 110 |
ansible.builtin.apt_key: |
| 111 |
url: https://ftp-master.debian.org/keys/archive-key-6.0.asc |
| 112 |
state: present |
| 113 |
|
| 114 |
- name: Add an Apt signing key, will not download if present |
| 115 |
ansible.builtin.apt_key: |
| 116 |
id: 9FED2BCBDCD29CDF762678CBAED4B06F473041FA |
| 117 |
url: https://ftp-master.debian.org/keys/archive-key-6.0.asc |
| 118 |
state: present |
| 119 |
|
| 120 |
- name: Remove a Apt specific signing key, leading 0x is valid |
| 121 |
ansible.builtin.apt_key: |
| 122 |
id: 0x9FED2BCBDCD29CDF762678CBAED4B06F473041FA |
| 123 |
state: absent |
| 124 |
|
| 125 |
# Use armored file since utf-8 string is expected. Must be of "PGP PUBLIC KEY BLOCK" type. |
| 126 |
- name: Add a key from a file on the Ansible server |
| 127 |
ansible.builtin.apt_key: |
| 128 |
data: "{{ lookup('ansible.builtin.file', 'apt.asc') }}" |
| 129 |
state: present |
| 130 |
|
| 131 |
- name: Add an Apt signing key to a specific keyring file |
| 132 |
ansible.builtin.apt_key: |
| 133 |
id: 9FED2BCBDCD29CDF762678CBAED4B06F473041FA |
| 134 |
url: https://ftp-master.debian.org/keys/archive-key-6.0.asc |
| 135 |
keyring: /etc/apt/trusted.gpg.d/debian.gpg |
| 136 |
|
| 137 |
- name: Add Apt signing key on remote server to keyring |
| 138 |
ansible.builtin.apt_key: |
| 139 |
id: 9FED2BCBDCD29CDF762678CBAED4B06F473041FA |
| 140 |
file: /tmp/apt.gpg |
| 141 |
state: present |
| 142 |
""" |
| 143 |
|
| 144 |
RETURN = """ |
| 145 |
after: |
| 146 |
description: List of apt key ids or fingerprints after any modification |
| 147 |
returned: on change |
| 148 |
type: list |
| 149 |
sample: ["D8576A8BA88D21E9", "3B4FE6ACC0B21F32", "D94AA3F0EFE21092", "871920D1991BC93C"] |
| 150 |
before: |
| 151 |
description: List of apt key ids or fingerprints before any modifications |
| 152 |
returned: always |
| 153 |
type: list |
| 154 |
sample: ["3B4FE6ACC0B21F32", "D94AA3F0EFE21092", "871920D1991BC93C"] |
| 155 |
fp: |
| 156 |
description: Fingerprint of the key to import |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
sample: "D8576A8BA88D21E9" |
| 160 |
id: |
| 161 |
description: key id from source |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: "36A1D7869245C8950F966E92D8576A8BA88D21E9" |
| 165 |
key_id: |
| 166 |
description: calculated key id, it should be same as 'id', but can be different |
| 167 |
returned: always |
| 168 |
type: str |
| 169 |
sample: "36A1D7869245C8950F966E92D8576A8BA88D21E9" |
| 170 |
short_id: |
| 171 |
description: calculated short key id |
| 172 |
returned: always |
| 173 |
type: str |
| 174 |
sample: "A88D21E9" |
| 175 |
""" |
| 176 |
|
| 177 |
import os |
| 178 |
|
| 179 |
from ansible.module_utils.common.text.converters import to_native |
| 180 |
from ansible.module_utils.basic import AnsibleModule |
| 181 |
from ansible.module_utils.common.locale import get_best_parsable_locale |
| 182 |
from ansible.module_utils.urls import fetch_url, is_fetch_success, mask_url |
| 183 |
|
| 184 |
|
| 185 |
apt_key_bin = None |
| 186 |
gpg_bin = None |
| 187 |
locale = None |
| 188 |
|
| 189 |
|
| 190 |
def lang_env(module): |
| 191 |
|
| 192 |
if not hasattr(lang_env, 'result'): |
| 193 |
locale = get_best_parsable_locale(module) |
| 194 |
lang_env.result = dict(LANG=locale, LC_ALL=locale, LC_MESSAGES=locale, LANGUAGE=locale) |
| 195 |
|
| 196 |
return lang_env.result |
| 197 |
|
| 198 |
|
| 199 |
def find_needed_binaries(module): |
| 200 |
global apt_key_bin |
| 201 |
global gpg_bin |
| 202 |
|
| 203 |
try: |
| 204 |
apt_key_bin = module.get_bin_path('apt-key', required=True) |
| 205 |
except ValueError as e: |
| 206 |
module.exit_json(f'{to_native(e)}. Apt-key has been deprecated. See the deb822_repository as an alternative.') |
| 207 |
|
| 208 |
try: |
| 209 |
gpg_bin = module.get_bin_path('gpg', required=True) |
| 210 |
except ValueError as e: |
| 211 |
module.exit_json(msg=to_native(e)) |
| 212 |
|
| 213 |
|
| 214 |
def add_http_proxy(cmd): |
| 215 |
|
| 216 |
for envvar in ('HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy'): |
| 217 |
proxy = os.environ.get(envvar) |
| 218 |
if proxy: |
| 219 |
break |
| 220 |
|
| 221 |
if proxy: |
| 222 |
cmd += ' --keyserver-options http-proxy=%s' % proxy |
| 223 |
|
| 224 |
return cmd |
| 225 |
|
| 226 |
|
| 227 |
def parse_key_id(key_id): |
| 228 |
"""validate the key_id and break it into segments |
| 229 |
|
| 230 |
:arg key_id: The key_id as supplied by the user. A valid key_id will be |
| 231 |
8, 16, or more hexadecimal chars with an optional leading ``0x``. |
| 232 |
:returns: The portion of key_id suitable for apt-key del, the portion |
| 233 |
suitable for comparisons with --list-public-keys, and the portion that |
| 234 |
can be used with --recv-key. If key_id is long enough, these will be |
| 235 |
the last 8 characters of key_id, the last 16 characters, and all of |
| 236 |
key_id. If key_id is not long enough, some of the values will be the |
| 237 |
same. |
| 238 |
|
| 239 |
* apt-key del <= 1.10 has a bug with key_id != 8 chars |
| 240 |
* apt-key adv --list-public-keys prints 16 chars |
| 241 |
* apt-key adv --recv-key can take more chars |
| 242 |
|
| 243 |
""" |
| 244 |
# Make sure the key_id is valid hexadecimal |
| 245 |
int(to_native(key_id), 16) |
| 246 |
|
| 247 |
key_id = key_id.upper() |
| 248 |
if key_id.startswith('0X'): |
| 249 |
key_id = key_id[2:] |
| 250 |
|
| 251 |
key_id_len = len(key_id) |
| 252 |
if (key_id_len != 8 and key_id_len != 16) and key_id_len <= 16: |
| 253 |
raise ValueError('key_id must be 8, 16, or 16+ hexadecimal characters in length') |
| 254 |
|
| 255 |
short_key_id = key_id[-8:] |
| 256 |
|
| 257 |
fingerprint = key_id |
| 258 |
if key_id_len > 16: |
| 259 |
fingerprint = key_id[-16:] |
| 260 |
|
| 261 |
return short_key_id, fingerprint, key_id |
| 262 |
|
| 263 |
|
| 264 |
def parse_output_for_keys(output, short_format=False): |
| 265 |
|
| 266 |
found = [] |
| 267 |
lines = to_native(output).split('\n') |
| 268 |
for line in lines: |
| 269 |
if (line.startswith("pub") or line.startswith("sub")) and "expired" not in line: |
| 270 |
try: |
| 271 |
# apt key format |
| 272 |
tokens = line.split() |
| 273 |
code = tokens[1] |
| 274 |
(len_type, real_code) = code.split("/") |
| 275 |
except (IndexError, ValueError): |
| 276 |
# gpg format |
| 277 |
try: |
| 278 |
tokens = line.split(':') |
| 279 |
real_code = tokens[4] |
| 280 |
except (IndexError, ValueError): |
| 281 |
# invalid line, skip |
| 282 |
continue |
| 283 |
found.append(real_code) |
| 284 |
|
| 285 |
if found and short_format: |
| 286 |
found = shorten_key_ids(found) |
| 287 |
|
| 288 |
return found |
| 289 |
|
| 290 |
|
| 291 |
def all_keys(module, keyring, short_format): |
| 292 |
if keyring is not None: |
| 293 |
cmd = "%s --keyring %s adv --list-public-keys --keyid-format=long" % (apt_key_bin, keyring) |
| 294 |
else: |
| 295 |
cmd = "%s adv --list-public-keys --keyid-format=long" % apt_key_bin |
| 296 |
(rc, out, err) = module.run_command(cmd) |
| 297 |
if rc != 0: |
| 298 |
module.fail_json(msg="Unable to list public keys", cmd=cmd, rc=rc, stdout=out, stderr=err) |
| 299 |
|
| 300 |
return parse_output_for_keys(out, short_format) |
| 301 |
|
| 302 |
|
| 303 |
def shorten_key_ids(key_id_list): |
| 304 |
""" |
| 305 |
Takes a list of key ids, and converts them to the 'short' format, |
| 306 |
by reducing them to their last 8 characters. |
| 307 |
""" |
| 308 |
short = [] |
| 309 |
for key in key_id_list: |
| 310 |
short.append(key[-8:]) |
| 311 |
return short |
| 312 |
|
| 313 |
|
| 314 |
def download_key(module, url): |
| 315 |
|
| 316 |
try: |
| 317 |
# note: validate_certs and other args are pulled from module directly |
| 318 |
rsp, info = fetch_url(module, url, use_proxy=True) |
| 319 |
if not is_fetch_success(info): |
| 320 |
module.fail_json(msg="Failed to download key at %s: %s" % (mask_url(url), info['msg'])) |
| 321 |
|
| 322 |
return rsp.read() |
| 323 |
except Exception: |
| 324 |
module.fail_json(msg=f"Error getting key id from url: {mask_url(url)}") |
| 325 |
|
| 326 |
|
| 327 |
def get_key_id_from_file(module, filename, data=None): |
| 328 |
|
| 329 |
native_data = to_native(data) |
| 330 |
is_armored = native_data.find("-----BEGIN PGP PUBLIC KEY BLOCK-----") >= 0 |
| 331 |
|
| 332 |
key = None |
| 333 |
|
| 334 |
cmd = [gpg_bin, '--with-colons', filename] |
| 335 |
|
| 336 |
(rc, out, err) = module.run_command(cmd, environ_update=lang_env(module), data=(native_data if is_armored else data), binary_data=not is_armored) |
| 337 |
if rc != 0: |
| 338 |
module.fail_json(msg="Unable to extract key from '%s'" % ('inline data' if data is not None else filename), stdout=out, stderr=err) |
| 339 |
|
| 340 |
keys = parse_output_for_keys(out) |
| 341 |
# assume we only want first key? |
| 342 |
if keys: |
| 343 |
key = keys[0] |
| 344 |
|
| 345 |
return key |
| 346 |
|
| 347 |
|
| 348 |
def get_key_id_from_data(module, data): |
| 349 |
return get_key_id_from_file(module, '-', data) |
| 350 |
|
| 351 |
|
| 352 |
def import_key(module, keyring, keyserver, key_id): |
| 353 |
|
| 354 |
if keyring: |
| 355 |
cmd = "%s --keyring %s adv --no-tty --keyserver %s" % (apt_key_bin, keyring, keyserver) |
| 356 |
else: |
| 357 |
cmd = "%s adv --no-tty --keyserver %s" % (apt_key_bin, keyserver) |
| 358 |
|
| 359 |
# check for proxy |
| 360 |
cmd = add_http_proxy(cmd) |
| 361 |
|
| 362 |
# add recv argument as last one |
| 363 |
cmd = "%s --recv %s" % (cmd, key_id) |
| 364 |
|
| 365 |
for retry in range(5): |
| 366 |
(rc, out, err) = module.run_command(cmd, environ_update=lang_env(module)) |
| 367 |
if rc == 0: |
| 368 |
break |
| 369 |
else: |
| 370 |
# Out of retries |
| 371 |
if rc == 2 and 'not found on keyserver' in out: |
| 372 |
msg = 'Key %s not found on keyserver %s' % (key_id, keyserver) |
| 373 |
module.fail_json(cmd=cmd, msg=msg, forced_environment=lang_env(module)) |
| 374 |
else: |
| 375 |
msg = "Error fetching key %s from keyserver: %s" % (key_id, keyserver) |
| 376 |
module.fail_json(cmd=cmd, msg=msg, forced_environment=lang_env(module), rc=rc, stdout=out, stderr=err) |
| 377 |
return True |
| 378 |
|
| 379 |
|
| 380 |
def add_key(module, keyfile, keyring, data=None): |
| 381 |
if data is not None: |
| 382 |
if keyring: |
| 383 |
cmd = "%s --keyring %s add -" % (apt_key_bin, keyring) |
| 384 |
else: |
| 385 |
cmd = "%s add -" % apt_key_bin |
| 386 |
(rc, out, err) = module.run_command(cmd, data=data, binary_data=True) |
| 387 |
if rc != 0: |
| 388 |
module.fail_json( |
| 389 |
msg="Unable to add a key from binary data", |
| 390 |
cmd=cmd, |
| 391 |
rc=rc, |
| 392 |
stdout=out, |
| 393 |
stderr=err, |
| 394 |
) |
| 395 |
else: |
| 396 |
if keyring: |
| 397 |
cmd = "%s --keyring %s add %s" % (apt_key_bin, keyring, keyfile) |
| 398 |
else: |
| 399 |
cmd = "%s add %s" % (apt_key_bin, keyfile) |
| 400 |
(rc, out, err) = module.run_command(cmd) |
| 401 |
if rc != 0: |
| 402 |
module.fail_json( |
| 403 |
msg="Unable to add a key from file %s" % (keyfile), |
| 404 |
cmd=cmd, |
| 405 |
rc=rc, |
| 406 |
keyfile=keyfile, |
| 407 |
stdout=out, |
| 408 |
stderr=err, |
| 409 |
) |
| 410 |
return True |
| 411 |
|
| 412 |
|
| 413 |
def remove_key(module, key_id, keyring): |
| 414 |
if keyring: |
| 415 |
cmd = '%s --keyring %s del %s' % (apt_key_bin, keyring, key_id) |
| 416 |
else: |
| 417 |
cmd = '%s del %s' % (apt_key_bin, key_id) |
| 418 |
(rc, out, err) = module.run_command(cmd) |
| 419 |
if rc != 0: |
| 420 |
module.fail_json( |
| 421 |
msg="Unable to remove a key with id %s" % (key_id), |
| 422 |
cmd=cmd, |
| 423 |
rc=rc, |
| 424 |
key_id=key_id, |
| 425 |
stdout=out, |
| 426 |
stderr=err, |
| 427 |
) |
| 428 |
return True |
| 429 |
|
| 430 |
|
| 431 |
def main(): |
| 432 |
module = AnsibleModule( |
| 433 |
argument_spec=dict( |
| 434 |
id=dict(type='str'), |
| 435 |
url=dict(type='str'), |
| 436 |
data=dict(type='str'), |
| 437 |
file=dict(type='path'), |
| 438 |
keyring=dict(type='path'), |
| 439 |
validate_certs=dict(type='bool', default=True), |
| 440 |
keyserver=dict(type='str'), |
| 441 |
state=dict(type='str', default='present', choices=['absent', 'present']), |
| 442 |
), |
| 443 |
supports_check_mode=True, |
| 444 |
mutually_exclusive=(('data', 'file', 'keyserver', 'url'),), |
| 445 |
) |
| 446 |
|
| 447 |
# parameters |
| 448 |
key_id = module.params['id'] |
| 449 |
url = module.params['url'] |
| 450 |
data = module.params['data'] |
| 451 |
filename = module.params['file'] |
| 452 |
keyring = module.params['keyring'] |
| 453 |
state = module.params['state'] |
| 454 |
keyserver = module.params['keyserver'] |
| 455 |
|
| 456 |
# internal vars |
| 457 |
short_format = False |
| 458 |
short_key_id = None |
| 459 |
fingerprint = None |
| 460 |
error_no_error = "apt-key did not return an error, but %s (check that the id is correct and *not* a subkey)" |
| 461 |
|
| 462 |
# ensure we have requirements met |
| 463 |
find_needed_binaries(module) |
| 464 |
|
| 465 |
# initialize result dict |
| 466 |
r = {'changed': False} |
| 467 |
|
| 468 |
if not key_id: |
| 469 |
|
| 470 |
if keyserver: |
| 471 |
module.fail_json(msg="Missing key_id, required with keyserver.") |
| 472 |
|
| 473 |
if url: |
| 474 |
data = download_key(module, url) |
| 475 |
|
| 476 |
if filename: |
| 477 |
key_id = get_key_id_from_file(module, filename) |
| 478 |
elif data: |
| 479 |
key_id = get_key_id_from_data(module, data) |
| 480 |
|
| 481 |
r['id'] = key_id |
| 482 |
try: |
| 483 |
short_key_id, fingerprint, key_id = parse_key_id(key_id) |
| 484 |
r['short_id'] = short_key_id |
| 485 |
r['fp'] = fingerprint |
| 486 |
r['key_id'] = key_id |
| 487 |
except ValueError: |
| 488 |
module.fail_json(msg='Invalid key_id', **r) |
| 489 |
|
| 490 |
if not fingerprint: |
| 491 |
# invalid key should fail well before this point, but JIC ... |
| 492 |
module.fail_json(msg="Unable to continue as we could not extract a valid fingerprint to compare against existing keys.", **r) |
| 493 |
|
| 494 |
if len(key_id) == 8: |
| 495 |
short_format = True |
| 496 |
|
| 497 |
# get existing keys to verify if we need to change |
| 498 |
r['before'] = keys = all_keys(module, keyring, short_format) |
| 499 |
keys2 = [] |
| 500 |
|
| 501 |
if state == 'present': |
| 502 |
if (short_format and short_key_id not in keys) or (not short_format and fingerprint not in keys): |
| 503 |
r['changed'] = True |
| 504 |
if not module.check_mode: |
| 505 |
if filename: |
| 506 |
add_key(module, filename, keyring) |
| 507 |
elif keyserver: |
| 508 |
import_key(module, keyring, keyserver, key_id) |
| 509 |
elif data: |
| 510 |
# this also takes care of url if key_id was not provided |
| 511 |
add_key(module, "-", keyring, data) |
| 512 |
elif url: |
| 513 |
# we hit this branch only if key_id is supplied with url |
| 514 |
data = download_key(module, url) |
| 515 |
add_key(module, "-", keyring, data) |
| 516 |
else: |
| 517 |
module.fail_json(msg="No key to add ... how did i get here?!?!", **r) |
| 518 |
|
| 519 |
# verify it got added |
| 520 |
r['after'] = keys2 = all_keys(module, keyring, short_format) |
| 521 |
if (short_format and short_key_id not in keys2) or (not short_format and fingerprint not in keys2): |
| 522 |
module.fail_json(msg=error_no_error % 'failed to add the key', **r) |
| 523 |
|
| 524 |
elif state == 'absent': |
| 525 |
if not key_id: |
| 526 |
module.fail_json(msg="key is required to remove a key", **r) |
| 527 |
if fingerprint in keys: |
| 528 |
r['changed'] = True |
| 529 |
if not module.check_mode: |
| 530 |
# we use the "short" id: key_id[-8:], short_format=True |
| 531 |
# it's a workaround for https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1481871 |
| 532 |
if short_key_id is not None and remove_key(module, short_key_id, keyring): |
| 533 |
r['after'] = keys2 = all_keys(module, keyring, short_format) |
| 534 |
if fingerprint in keys2: |
| 535 |
module.fail_json(msg=error_no_error % 'the key was not removed', **r) |
| 536 |
else: |
| 537 |
module.fail_json(msg="error removing key_id", **r) |
| 538 |
|
| 539 |
module.exit_json(**r) |
| 540 |
|
| 541 |
|
| 542 |
if __name__ == '__main__': |
| 543 |
main() |