| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2015 Cristian van Ee <cristian at cvee.org> |
| 4 |
# Copyright 2015 Igor Gnatenko <[email protected]> |
| 5 |
# Copyright 2018 Adam Miller <[email protected]> |
| 6 |
# |
| 7 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = """ |
| 13 |
--- |
| 14 |
module: dnf |
| 15 |
version_added: 1.9 |
| 16 |
short_description: Manages packages with the I(dnf) package manager |
| 17 |
description: |
| 18 |
- Installs, upgrade, removes, and lists packages and groups with the I(dnf) package manager. |
| 19 |
options: |
| 20 |
use_backend: |
| 21 |
description: |
| 22 |
- Backend module to use. |
| 23 |
default: "auto" |
| 24 |
choices: |
| 25 |
auto: Automatically select the backend based on the C(ansible_facts.pkg_mgr) fact. |
| 26 |
yum: Alias for V(auto) (see Notes) |
| 27 |
dnf: M(ansible.builtin.dnf) |
| 28 |
yum4: Alias for V(dnf) |
| 29 |
dnf4: Alias for V(dnf) |
| 30 |
dnf5: M(ansible.builtin.dnf5) |
| 31 |
type: str |
| 32 |
version_added: 2.15 |
| 33 |
name: |
| 34 |
description: |
| 35 |
- "A package name or package specifier with version, like C(name-1.0). |
| 36 |
When using state=latest, this can be '*' which means run: dnf -y update. |
| 37 |
You can also pass a url or a local path to an rpm file. |
| 38 |
To operate on several packages this can accept a comma separated string of packages or a list of packages." |
| 39 |
- Comparison operators for package version are valid here C(>), C(<), C(>=), C(<=). Example - C(name >= 1.0). |
| 40 |
Spaces around the operator are required. |
| 41 |
- You can also pass an absolute path for a binary which is provided by the package to install. |
| 42 |
See examples for more information. |
| 43 |
aliases: |
| 44 |
- pkg |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
default: [] |
| 48 |
|
| 49 |
list: |
| 50 |
description: |
| 51 |
- Various (non-idempotent) commands for usage with C(/usr/bin/ansible) and I(not) playbooks. |
| 52 |
Use M(ansible.builtin.package_facts) instead of the O(list) argument as a best practice. |
| 53 |
type: str |
| 54 |
|
| 55 |
state: |
| 56 |
description: |
| 57 |
- Whether to install (V(present), V(latest)), or remove (V(absent)) a package. |
| 58 |
- Default is V(None), however in effect the default action is V(present) unless the O(autoremove=true), |
| 59 |
then V(absent) is inferred. |
| 60 |
choices: ['absent', 'present', 'installed', 'removed', 'latest'] |
| 61 |
type: str |
| 62 |
|
| 63 |
enablerepo: |
| 64 |
description: |
| 65 |
- C(Repoid) of repositories to enable for the install/update operation. |
| 66 |
These repos will not persist beyond the transaction. |
| 67 |
When specifying multiple repos, separate them with a ",". |
| 68 |
type: list |
| 69 |
elements: str |
| 70 |
default: [] |
| 71 |
|
| 72 |
disablerepo: |
| 73 |
description: |
| 74 |
- C(Repoid) of repositories to disable for the install/update operation. |
| 75 |
These repos will not persist beyond the transaction. |
| 76 |
When specifying multiple repos, separate them with a C(,). |
| 77 |
type: list |
| 78 |
elements: str |
| 79 |
default: [] |
| 80 |
|
| 81 |
conf_file: |
| 82 |
description: |
| 83 |
- The remote dnf configuration file to use for the transaction. |
| 84 |
type: str |
| 85 |
|
| 86 |
disable_gpg_check: |
| 87 |
description: |
| 88 |
- Whether to disable the GPG checking of signatures of packages being |
| 89 |
installed. Has an effect only if O(state=present) or O(state=latest). |
| 90 |
- This setting affects packages installed from a repository as well as |
| 91 |
"local" packages installed from the filesystem or a URL. |
| 92 |
type: bool |
| 93 |
default: 'no' |
| 94 |
|
| 95 |
installroot: |
| 96 |
description: |
| 97 |
- Specifies an alternative installroot, relative to which all packages |
| 98 |
will be installed. |
| 99 |
version_added: "2.3" |
| 100 |
default: "/" |
| 101 |
type: str |
| 102 |
|
| 103 |
releasever: |
| 104 |
description: |
| 105 |
- Specifies an alternative release from which all packages will be |
| 106 |
installed. |
| 107 |
version_added: "2.6" |
| 108 |
type: str |
| 109 |
|
| 110 |
autoremove: |
| 111 |
description: |
| 112 |
- If V(true), removes all "leaf" packages from the system that were originally |
| 113 |
installed as dependencies of user-installed packages but which are no longer |
| 114 |
required by any such package. Should be used alone or when O(state=absent). |
| 115 |
type: bool |
| 116 |
default: "no" |
| 117 |
version_added: "2.4" |
| 118 |
exclude: |
| 119 |
description: |
| 120 |
- Package name(s) to exclude from the operation. This can be a list or a comma separated string. |
| 121 |
- Works with all O(state) values, including V(absent), V(present), and V(latest). |
| 122 |
version_added: "2.7" |
| 123 |
type: list |
| 124 |
elements: str |
| 125 |
default: [] |
| 126 |
skip_broken: |
| 127 |
description: |
| 128 |
- Skip all unavailable packages or packages with broken dependencies |
| 129 |
without raising an error. Equivalent to passing the C(--skip-broken) option. |
| 130 |
type: bool |
| 131 |
default: "no" |
| 132 |
version_added: "2.7" |
| 133 |
update_cache: |
| 134 |
description: |
| 135 |
- Force dnf to check if cache is out of date and redownload if needed. |
| 136 |
Has an effect only if O(state=present) or O(state=latest). |
| 137 |
type: bool |
| 138 |
default: "no" |
| 139 |
aliases: [ expire-cache ] |
| 140 |
version_added: "2.7" |
| 141 |
update_only: |
| 142 |
description: |
| 143 |
- When using latest, only update installed packages. Do not install packages. |
| 144 |
- Has an effect only if O(state=present) or O(state=latest). |
| 145 |
default: "no" |
| 146 |
type: bool |
| 147 |
version_added: "2.7" |
| 148 |
security: |
| 149 |
description: |
| 150 |
- If set to V(true), and O(state=latest) then only installs updates that have been marked security related. |
| 151 |
- Note that, similar to C(dnf upgrade-minimal), this filter applies to dependencies as well. |
| 152 |
type: bool |
| 153 |
default: "no" |
| 154 |
version_added: "2.7" |
| 155 |
bugfix: |
| 156 |
description: |
| 157 |
- If set to V(true), and O(state=latest) then only installs updates that have been marked bugfix related. |
| 158 |
- Note that, similar to C(dnf upgrade-minimal), this filter applies to dependencies as well. |
| 159 |
default: "no" |
| 160 |
type: bool |
| 161 |
version_added: "2.7" |
| 162 |
enable_plugin: |
| 163 |
description: |
| 164 |
- C(Plugin) name to enable for the install/update operation. |
| 165 |
The enabled plugin will not persist beyond the transaction. |
| 166 |
version_added: "2.7" |
| 167 |
type: list |
| 168 |
elements: str |
| 169 |
default: [] |
| 170 |
disable_plugin: |
| 171 |
description: |
| 172 |
- C(Plugin) name to disable for the install/update operation. |
| 173 |
The disabled plugins will not persist beyond the transaction. |
| 174 |
version_added: "2.7" |
| 175 |
type: list |
| 176 |
default: [] |
| 177 |
elements: str |
| 178 |
disable_excludes: |
| 179 |
description: |
| 180 |
- Disable the excludes defined in DNF config files. |
| 181 |
- If set to V(all), disables all excludes. |
| 182 |
- If set to V(main), disable excludes defined in C([main]) in C(dnf.conf). |
| 183 |
- If set to V(repoid), disable excludes defined for given repo id. |
| 184 |
version_added: "2.7" |
| 185 |
type: str |
| 186 |
validate_certs: |
| 187 |
description: |
| 188 |
- This only applies if using a https url as the source of the rpm. For example, for localinstall. |
| 189 |
If set to V(false), the SSL certificates will not be validated. |
| 190 |
- This should only set to V(false) used on personally controlled sites using self-signed certificates as it avoids verifying the source site. |
| 191 |
type: bool |
| 192 |
default: "yes" |
| 193 |
version_added: "2.7" |
| 194 |
sslverify: |
| 195 |
description: |
| 196 |
- Disables SSL validation of the repository server for this transaction. |
| 197 |
- This should be set to V(false) if one of the configured repositories is using an untrusted or self-signed certificate. |
| 198 |
type: bool |
| 199 |
default: "yes" |
| 200 |
version_added: "2.13" |
| 201 |
allow_downgrade: |
| 202 |
description: |
| 203 |
- Specify if the named package and version is allowed to downgrade |
| 204 |
a maybe already installed higher version of that package. |
| 205 |
Note that setting O(allow_downgrade=true) can make this module |
| 206 |
behave in a non-idempotent way. The task could end up with a set |
| 207 |
of packages that does not match the complete list of specified |
| 208 |
packages to install (because dependencies between the downgraded |
| 209 |
package and others can cause changes to the packages which were |
| 210 |
in the earlier transaction). |
| 211 |
- Since this feature is not provided by C(dnf) itself but by M(ansible.builtin.dnf) module, |
| 212 |
using this in combination with wildcard characters in O(name) may result in an unexpected results. |
| 213 |
type: bool |
| 214 |
default: "no" |
| 215 |
version_added: "2.7" |
| 216 |
download_only: |
| 217 |
description: |
| 218 |
- Only download the packages, do not install them. |
| 219 |
default: "no" |
| 220 |
type: bool |
| 221 |
version_added: "2.7" |
| 222 |
lock_timeout: |
| 223 |
description: |
| 224 |
- Amount of time to wait for the dnf lockfile to be freed. |
| 225 |
required: false |
| 226 |
default: 30 |
| 227 |
type: int |
| 228 |
version_added: "2.8" |
| 229 |
install_weak_deps: |
| 230 |
description: |
| 231 |
- Will also install all packages linked by a weak dependency relation. |
| 232 |
type: bool |
| 233 |
default: "yes" |
| 234 |
version_added: "2.8" |
| 235 |
download_dir: |
| 236 |
description: |
| 237 |
- Specifies an alternate directory to store packages. |
| 238 |
- Has an effect only if O(download_only) is specified. |
| 239 |
type: str |
| 240 |
version_added: "2.8" |
| 241 |
allowerasing: |
| 242 |
description: |
| 243 |
- If V(true) it allows erasing of installed packages to resolve dependencies. |
| 244 |
required: false |
| 245 |
type: bool |
| 246 |
default: "no" |
| 247 |
version_added: "2.10" |
| 248 |
nobest: |
| 249 |
description: |
| 250 |
- This is the opposite of the O(best) option kept for backwards compatibility. |
| 251 |
- Since ansible-core 2.17 the default value is set by the operating system distribution. |
| 252 |
required: false |
| 253 |
type: bool |
| 254 |
version_added: "2.11" |
| 255 |
best: |
| 256 |
description: |
| 257 |
- When set to V(true), either use a package with the highest version available or fail. |
| 258 |
- When set to V(false), if the latest version cannot be installed go with the lower version. |
| 259 |
- Default is set by the operating system distribution. |
| 260 |
required: false |
| 261 |
type: bool |
| 262 |
version_added: "2.17" |
| 263 |
cacheonly: |
| 264 |
description: |
| 265 |
- Tells dnf to run entirely from system cache; does not download or update metadata. |
| 266 |
type: bool |
| 267 |
default: "no" |
| 268 |
version_added: "2.12" |
| 269 |
extends_documentation_fragment: |
| 270 |
- action_common_attributes |
| 271 |
- action_common_attributes.flow |
| 272 |
attributes: |
| 273 |
action: |
| 274 |
details: dnf has 2 action plugins that use it under the hood, M(ansible.builtin.dnf) and M(ansible.builtin.package). |
| 275 |
support: partial |
| 276 |
async: |
| 277 |
support: none |
| 278 |
bypass_host_loop: |
| 279 |
support: none |
| 280 |
check_mode: |
| 281 |
support: full |
| 282 |
diff_mode: |
| 283 |
support: full |
| 284 |
platform: |
| 285 |
platforms: rhel |
| 286 |
notes: |
| 287 |
- When used with a C(loop:) each package will be processed individually, it is much more efficient to pass the list directly to the O(name) option. |
| 288 |
- Group removal doesn't work if the group was installed with Ansible because |
| 289 |
upstream dnf's API doesn't properly mark groups as installed, therefore upon |
| 290 |
removal the module is unable to detect that the group is installed |
| 291 |
U(https://bugzilla.redhat.com/show_bug.cgi?id=1620324). |
| 292 |
- While O(use_backend=yum) and the ability to call the action plugin as |
| 293 |
M(ansible.builtin.yum) are provided for syntax compatibility, the YUM |
| 294 |
backend was removed in ansible-core 2.17 because the required libraries are |
| 295 |
not available for any supported version of Python. If you rely on this |
| 296 |
functionality, use an older version of Ansible. |
| 297 |
requirements: |
| 298 |
- python3-dnf |
| 299 |
author: |
| 300 |
- Igor Gnatenko (@ignatenkobrain) <[email protected]> |
| 301 |
- Cristian van Ee (@DJMuggs) <cristian at cvee.org> |
| 302 |
- Berend De Schouwer (@berenddeschouwer) |
| 303 |
- Adam Miller (@maxamillion) <[email protected]> |
| 304 |
""" |
| 305 |
|
| 306 |
EXAMPLES = """ |
| 307 |
- name: Install the latest version of Apache |
| 308 |
ansible.builtin.dnf: |
| 309 |
name: httpd |
| 310 |
state: latest |
| 311 |
|
| 312 |
- name: Install Apache >= 2.4 |
| 313 |
ansible.builtin.dnf: |
| 314 |
name: httpd >= 2.4 |
| 315 |
state: present |
| 316 |
|
| 317 |
- name: Install the latest version of Apache and MariaDB |
| 318 |
ansible.builtin.dnf: |
| 319 |
name: |
| 320 |
- httpd |
| 321 |
- mariadb-server |
| 322 |
state: latest |
| 323 |
|
| 324 |
- name: Remove the Apache package |
| 325 |
ansible.builtin.dnf: |
| 326 |
name: httpd |
| 327 |
state: absent |
| 328 |
|
| 329 |
- name: Install the latest version of Apache from the testing repo |
| 330 |
ansible.builtin.dnf: |
| 331 |
name: httpd |
| 332 |
enablerepo: testing |
| 333 |
state: present |
| 334 |
|
| 335 |
- name: Upgrade all packages |
| 336 |
ansible.builtin.dnf: |
| 337 |
name: "*" |
| 338 |
state: latest |
| 339 |
|
| 340 |
- name: Update the webserver, depending on which is installed on the system. Do not install the other one |
| 341 |
ansible.builtin.dnf: |
| 342 |
name: |
| 343 |
- httpd |
| 344 |
- nginx |
| 345 |
state: latest |
| 346 |
update_only: yes |
| 347 |
|
| 348 |
- name: Install the nginx rpm from a remote repo |
| 349 |
ansible.builtin.dnf: |
| 350 |
name: 'http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm' |
| 351 |
state: present |
| 352 |
|
| 353 |
- name: Install nginx rpm from a local file |
| 354 |
ansible.builtin.dnf: |
| 355 |
name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm |
| 356 |
state: present |
| 357 |
|
| 358 |
- name: Install Package based upon the file it provides |
| 359 |
ansible.builtin.dnf: |
| 360 |
name: /usr/bin/cowsay |
| 361 |
state: present |
| 362 |
|
| 363 |
- name: Install the 'Development tools' package group |
| 364 |
ansible.builtin.dnf: |
| 365 |
name: '@Development tools' |
| 366 |
state: present |
| 367 |
|
| 368 |
- name: Autoremove unneeded packages installed as dependencies |
| 369 |
ansible.builtin.dnf: |
| 370 |
autoremove: yes |
| 371 |
|
| 372 |
- name: Uninstall httpd but keep its dependencies |
| 373 |
ansible.builtin.dnf: |
| 374 |
name: httpd |
| 375 |
state: absent |
| 376 |
autoremove: no |
| 377 |
|
| 378 |
- name: Install a modularity appstream with defined stream and profile |
| 379 |
ansible.builtin.dnf: |
| 380 |
name: '@postgresql:9.6/client' |
| 381 |
state: present |
| 382 |
|
| 383 |
- name: Install a modularity appstream with defined stream |
| 384 |
ansible.builtin.dnf: |
| 385 |
name: '@postgresql:9.6' |
| 386 |
state: present |
| 387 |
|
| 388 |
- name: Install a modularity appstream with defined profile |
| 389 |
ansible.builtin.dnf: |
| 390 |
name: '@postgresql/client' |
| 391 |
state: present |
| 392 |
""" |
| 393 |
|
| 394 |
import json |
| 395 |
import sys |
| 396 |
|
| 397 |
from ansible.module_utils.urls import fetch_file |
| 398 |
from ansible.module_utils.basic import AnsibleModule |
| 399 |
from ansible.module_utils.common.respawn import get_env_with_pythonpath, probe_interpreters_for_module |
| 400 |
from ansible.module_utils.embed import EmbedManager |
| 401 |
from ansible.module_utils.yumdnf import YumDnf, yumdnf_argument_spec |
| 402 |
|
| 403 |
|
| 404 |
dnfscript = EmbedManager.embed('..module_utils._embed', 'dnf.py') |
| 405 |
|
| 406 |
|
| 407 |
class DnfModule(YumDnf): |
| 408 |
""" |
| 409 |
DNF Ansible module back-end implementation |
| 410 |
""" |
| 411 |
|
| 412 |
def __init__(self, module): |
| 413 |
# This populates instance vars for all argument spec params |
| 414 |
super(DnfModule, self).__init__(module) |
| 415 |
|
| 416 |
self._interpreter = self._probe_interpreters() |
| 417 |
self.pkg_mgr_name = "dnf" |
| 418 |
|
| 419 |
def _build_config(self): |
| 420 |
"""Build configuration dictionary for module_utils functions.""" |
| 421 |
return { |
| 422 |
'conf_file': self.conf_file, |
| 423 |
'disable_gpg_check': self.disable_gpg_check, |
| 424 |
'sslverify': self.sslverify, |
| 425 |
'installroot': self.installroot, |
| 426 |
'exclude': self.exclude, |
| 427 |
'disable_excludes': self.disable_excludes, |
| 428 |
'releasever': self.releasever, |
| 429 |
'skip_broken': self.skip_broken, |
| 430 |
'nobest': self.nobest, |
| 431 |
'best': self.best, |
| 432 |
'download_only': self.download_only, |
| 433 |
'download_dir': self.download_dir, |
| 434 |
'cacheonly': self.cacheonly, |
| 435 |
'autoremove': self.autoremove, |
| 436 |
'install_weak_deps': self.install_weak_deps, |
| 437 |
'disablerepo': self.disablerepo, |
| 438 |
'enablerepo': self.enablerepo, |
| 439 |
'disable_plugin': self.disable_plugin, |
| 440 |
'enable_plugin': self.enable_plugin, |
| 441 |
'update_cache': self.update_cache, |
| 442 |
'bugfix': self.bugfix, |
| 443 |
'security': self.security, |
| 444 |
} |
| 445 |
|
| 446 |
def _probe_interpreters(self): |
| 447 |
interpreters = [ |
| 448 |
sys.executable, |
| 449 |
'/usr/libexec/platform-python', |
| 450 |
'/usr/bin/python3', |
| 451 |
'/usr/bin/python' |
| 452 |
] |
| 453 |
|
| 454 |
# probe well-known system Python locations for accessible bindings, favoring py3 |
| 455 |
interpreter = probe_interpreters_for_module(interpreters, module_names=['dnf']) |
| 456 |
|
| 457 |
if not interpreter: |
| 458 |
# done all we can do, something is just broken (auto-install isn't useful anymore with respawn, so it was removed) |
| 459 |
self.module.fail_json( |
| 460 |
msg='Could not import the dnf python module. ' |
| 461 |
f'Please install `python3-dnf` package. (attempted {interpreters})', |
| 462 |
results=[] |
| 463 |
) |
| 464 |
|
| 465 |
return interpreter |
| 466 |
|
| 467 |
def _execute_dnf_script(self, command, config, params=None): |
| 468 |
"""Execute the dnf module_utils script via subprocess with JSON RPC.""" |
| 469 |
request = {'command': command, 'config': config} |
| 470 |
if params: |
| 471 |
request['params'] = params |
| 472 |
|
| 473 |
request_json = json.dumps(request) |
| 474 |
python_executable = self._interpreter or sys.executable |
| 475 |
env = get_env_with_pythonpath() |
| 476 |
|
| 477 |
try: |
| 478 |
rc, stdout, stderr = self.module.run_command( |
| 479 |
[python_executable, '-m', dnfscript.python_module_ref], |
| 480 |
data=request_json, |
| 481 |
check_rc=False, |
| 482 |
handle_exceptions=False, |
| 483 |
environ_update=env, |
| 484 |
) |
| 485 |
|
| 486 |
if stdout: |
| 487 |
return json.loads(stdout) |
| 488 |
else: |
| 489 |
return { |
| 490 |
'failed': True, |
| 491 |
'msg': f'No output from dnf script. stderr: {stderr}', |
| 492 |
'results': [], |
| 493 |
'rc': rc |
| 494 |
} |
| 495 |
except json.JSONDecodeError as e: |
| 496 |
return { |
| 497 |
'failed': True, |
| 498 |
'msg': f'Failed to parse JSON from dnf script: {e}. stdout: {stdout}', |
| 499 |
'results': [], |
| 500 |
'rc': 1 |
| 501 |
} |
| 502 |
except Exception as e: |
| 503 |
return { |
| 504 |
'failed': True, |
| 505 |
'msg': f'Failed to execute dnf script: {e}', |
| 506 |
'results': [], |
| 507 |
'rc': 1 |
| 508 |
} |
| 509 |
|
| 510 |
def list_items(self, command): |
| 511 |
"""List package info based on the command.""" |
| 512 |
config = self._build_config() |
| 513 |
|
| 514 |
result = self._execute_dnf_script( |
| 515 |
command='list', |
| 516 |
config=config, |
| 517 |
params={'list_command': command}, |
| 518 |
) |
| 519 |
|
| 520 |
if result.get('failed'): |
| 521 |
self.module.fail_json(msg=result['msg'], results=[], rc=1) |
| 522 |
|
| 523 |
for warning in result.get('warnings', []): |
| 524 |
self.module.warn(warning) |
| 525 |
|
| 526 |
self.module.exit_json(msg='', results=result['results']) |
| 527 |
|
| 528 |
def ensure(self): |
| 529 |
names = [fetch_file(self.module, name) if '://' in name else name for name in self.names] |
| 530 |
|
| 531 |
config = self._build_config() |
| 532 |
params = { |
| 533 |
'names': names, |
| 534 |
'state': self.state, |
| 535 |
'autoremove': self.autoremove, |
| 536 |
'update_only': self.update_only, |
| 537 |
'allow_downgrade': self.allow_downgrade, |
| 538 |
'download_only': self.download_only, |
| 539 |
'disable_gpg_check': self.disable_gpg_check, |
| 540 |
'check_mode': self.module.check_mode, |
| 541 |
'download_dir': self.download_dir, |
| 542 |
'allowerasing': self.allowerasing, |
| 543 |
} |
| 544 |
|
| 545 |
result = self._execute_dnf_script( |
| 546 |
command='ensure', |
| 547 |
config=config, |
| 548 |
params=params, |
| 549 |
) |
| 550 |
|
| 551 |
for warning in result.get('warnings', []): |
| 552 |
self.module.warn(warning) |
| 553 |
|
| 554 |
if result.get('failed'): |
| 555 |
error_msg = result.get('msg', 'Unknown error occurred') |
| 556 |
if result.get('failures'): |
| 557 |
self.module.fail_json( |
| 558 |
msg=error_msg, |
| 559 |
failures=result['failures'], |
| 560 |
results=result.get('results', []), |
| 561 |
rc=result.get('rc', 1) |
| 562 |
) |
| 563 |
else: |
| 564 |
self.module.fail_json( |
| 565 |
msg=error_msg, |
| 566 |
results=result.get('results', []), |
| 567 |
rc=result.get('rc', 1) |
| 568 |
) |
| 569 |
|
| 570 |
if result.get('changed', False): |
| 571 |
self.module.exit_json( |
| 572 |
msg=result.get('msg', ''), |
| 573 |
changed=True, |
| 574 |
results=result.get('results', []), |
| 575 |
rc=result.get('rc', 0) |
| 576 |
) |
| 577 |
else: |
| 578 |
self.module.exit_json( |
| 579 |
msg=result.get('msg', 'Nothing to do'), |
| 580 |
changed=False, |
| 581 |
results=result.get('results', []), |
| 582 |
rc=result.get('rc', 0) |
| 583 |
) |
| 584 |
|
| 585 |
def update_cache_only(self): |
| 586 |
config = self._build_config() |
| 587 |
|
| 588 |
result = self._execute_dnf_script( |
| 589 |
command='update-cache', |
| 590 |
config=config, |
| 591 |
) |
| 592 |
|
| 593 |
if result.get('failed'): |
| 594 |
self.module.fail_json( |
| 595 |
msg=result['msg'], |
| 596 |
results=[], |
| 597 |
rc=1 |
| 598 |
) |
| 599 |
|
| 600 |
for warning in result.get('warnings', []): |
| 601 |
self.module.warn(warning) |
| 602 |
|
| 603 |
self.module.exit_json( |
| 604 |
msg='Cache updated', |
| 605 |
changed=result.get('changed', False), |
| 606 |
results=[], |
| 607 |
rc=0 |
| 608 |
) |
| 609 |
|
| 610 |
def run(self): |
| 611 |
if self.update_cache and not self.names and not self.list: |
| 612 |
self.update_cache_only() |
| 613 |
return |
| 614 |
|
| 615 |
# Set state as installed by default |
| 616 |
# This is not set in AnsibleModule() because the following shouldn't happen |
| 617 |
# - dnf: autoremove=yes state=installed |
| 618 |
if self.state is None: |
| 619 |
self.state = 'installed' |
| 620 |
|
| 621 |
if self.list: |
| 622 |
self.list_items(self.list) |
| 623 |
else: |
| 624 |
self.ensure() |
| 625 |
|
| 626 |
|
| 627 |
def main(): |
| 628 |
# state=installed name=pkgspec |
| 629 |
# state=removed name=pkgspec |
| 630 |
# state=latest name=pkgspec |
| 631 |
# |
| 632 |
# informational commands: |
| 633 |
# list=installed |
| 634 |
# list=updates |
| 635 |
# list=available |
| 636 |
# list=repos |
| 637 |
# list=pkgspec |
| 638 |
|
| 639 |
yumdnf_argument_spec['argument_spec']['use_backend'] = dict(default='auto', choices=['auto', 'dnf', 'yum', 'yum4', 'dnf4', 'dnf5']) |
| 640 |
|
| 641 |
module = AnsibleModule( |
| 642 |
**yumdnf_argument_spec |
| 643 |
) |
| 644 |
|
| 645 |
module_implementation = DnfModule(module) |
| 646 |
module_implementation.run() |
| 647 |
|
| 648 |
|
| 649 |
if __name__ == '__main__': |
| 650 |
main() |