| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, Dusty Mabe <[email protected]> |
| 3 |
# Copyright (c) 2018, Ansible Project |
| 4 |
# Copyright (c) 2021, Abhijeet Kasurde <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: rpm_ostree_pkg |
| 12 |
short_description: Install or uninstall overlay additional packages |
| 13 |
version_added: "2.0.0" |
| 14 |
description: |
| 15 |
- Install or uninstall overlay additional packages using C(rpm-ostree) command. |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
name: |
| 25 |
description: |
| 26 |
- Name of overlay package to install or remove. |
| 27 |
required: true |
| 28 |
type: list |
| 29 |
elements: str |
| 30 |
aliases: [pkg] |
| 31 |
state: |
| 32 |
description: |
| 33 |
- State of the overlay package. |
| 34 |
- V(present) simply ensures that a desired package is installed. |
| 35 |
- V(absent) removes the specified package. |
| 36 |
choices: ['absent', 'present'] |
| 37 |
default: 'present' |
| 38 |
type: str |
| 39 |
apply_live: |
| 40 |
description: |
| 41 |
- Adds the options C(--apply-live) when O(state=present). |
| 42 |
- Option is ignored when O(state=absent). |
| 43 |
- For more information, please see U(https://coreos.github.io/rpm-ostree/apply-live/). |
| 44 |
type: bool |
| 45 |
default: false |
| 46 |
version_added: 10.1.0 |
| 47 |
author: |
| 48 |
- Dusty Mabe (@dustymabe) |
| 49 |
- Abhijeet Kasurde (@Akasurde) |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
- name: Install overlay package |
| 54 |
community.general.rpm_ostree_pkg: |
| 55 |
name: nfs-utils |
| 56 |
state: present |
| 57 |
|
| 58 |
- name: Remove overlay package |
| 59 |
community.general.rpm_ostree_pkg: |
| 60 |
name: nfs-utils |
| 61 |
state: absent |
| 62 |
|
| 63 |
- name: Apply the overlay package live |
| 64 |
community.general.rpm_ostree_pkg: |
| 65 |
name: nfs-utils |
| 66 |
state: present |
| 67 |
apply_live: true |
| 68 |
|
| 69 |
# In case a different transaction is currently running the module would fail. |
| 70 |
# Adding a delay can help mitigate this problem: |
| 71 |
- name: Install overlay package |
| 72 |
community.general.rpm_ostree_pkg: |
| 73 |
name: nfs-utils |
| 74 |
state: present |
| 75 |
register: rpm_ostree_pkg |
| 76 |
until: rpm_ostree_pkg is not failed |
| 77 |
retries: 10 |
| 78 |
delay: 30 |
| 79 |
""" |
| 80 |
|
| 81 |
RETURN = r""" |
| 82 |
action: |
| 83 |
description: Action performed. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: 'install' |
| 87 |
packages: |
| 88 |
description: A list of packages specified. |
| 89 |
returned: always |
| 90 |
type: list |
| 91 |
sample: ["nfs-utils"] |
| 92 |
cmd: |
| 93 |
description: Full command used for performed action. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: 'rpm-ostree uninstall --allow-inactive --idempotent --unchanged-exit-77 nfs-utils' |
| 97 |
needs_reboot: |
| 98 |
description: Determine if machine needs a reboot to apply current changes. |
| 99 |
returned: success |
| 100 |
type: bool |
| 101 |
sample: true |
| 102 |
version_added: 10.1.0 |
| 103 |
""" |
| 104 |
|
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
|
| 108 |
class RpmOstreePkg: |
| 109 |
def __init__(self, module): |
| 110 |
self.module = module |
| 111 |
self.params = module.params |
| 112 |
self.state = module.params["state"] |
| 113 |
|
| 114 |
def ensure(self): |
| 115 |
results = dict( |
| 116 |
rc=0, |
| 117 |
changed=False, |
| 118 |
action="", |
| 119 |
packages=[], |
| 120 |
stdout="", |
| 121 |
stderr="", |
| 122 |
cmd="", |
| 123 |
needs_reboot=False, |
| 124 |
) |
| 125 |
|
| 126 |
# Ensure rpm-ostree command exists |
| 127 |
cmd = [self.module.get_bin_path("rpm-ostree", required=True)] |
| 128 |
|
| 129 |
# Decide action to perform |
| 130 |
if self.state == "present": |
| 131 |
results["action"] = "install" |
| 132 |
cmd.append("install") |
| 133 |
elif self.state == "absent": |
| 134 |
results["action"] = "uninstall" |
| 135 |
cmd.append("uninstall") |
| 136 |
|
| 137 |
# Add the options to the command line |
| 138 |
if self.params["apply_live"] and self.state == "present": |
| 139 |
cmd.extend(["--apply-live", "--assumeyes"]) |
| 140 |
|
| 141 |
# Additional parameters |
| 142 |
cmd.extend(["--allow-inactive", "--idempotent", "--unchanged-exit-77"]) |
| 143 |
for pkg in self.params["name"]: |
| 144 |
cmd.append(pkg) |
| 145 |
results["packages"].append(pkg) |
| 146 |
|
| 147 |
rc, out, err = self.module.run_command(cmd) |
| 148 |
|
| 149 |
# Determine if system needs a reboot to apply change |
| 150 |
if 'Changes queued for next boot. Run "systemctl reboot" to start a reboot' in out: |
| 151 |
results["needs_reboot"] = True |
| 152 |
|
| 153 |
results.update( |
| 154 |
dict( |
| 155 |
rc=rc, |
| 156 |
cmd=" ".join(cmd), |
| 157 |
stdout=out, |
| 158 |
stderr=err, |
| 159 |
) |
| 160 |
) |
| 161 |
|
| 162 |
# A few possible options: |
| 163 |
# - rc=0 - succeeded in making a change |
| 164 |
# - rc=77 - no change was needed |
| 165 |
# - rc=? - error |
| 166 |
if rc == 0: |
| 167 |
results["changed"] = True |
| 168 |
elif rc == 77: |
| 169 |
results["changed"] = False |
| 170 |
results["rc"] = 0 |
| 171 |
else: |
| 172 |
self.module.fail_json(msg="non-zero return code", **results) |
| 173 |
|
| 174 |
self.module.exit_json(**results) |
| 175 |
|
| 176 |
|
| 177 |
def main(): |
| 178 |
module = AnsibleModule( |
| 179 |
argument_spec=dict( |
| 180 |
state=dict(default="present", choices=["absent", "present"]), |
| 181 |
name=dict( |
| 182 |
aliases=["pkg"], |
| 183 |
required=True, |
| 184 |
type="list", |
| 185 |
elements="str", |
| 186 |
), |
| 187 |
apply_live=dict( |
| 188 |
type="bool", |
| 189 |
default=False, |
| 190 |
), |
| 191 |
), |
| 192 |
) |
| 193 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 194 |
|
| 195 |
rpm_ostree_pkg = RpmOstreePkg(module) |
| 196 |
rpm_ostree_pkg.ensure() |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
main() |