Debian Codegraph @

ansible_collections/community/general/plugins/modules/bootc_manage.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2024, Ryan Cook <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt
5 # 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: bootc_manage
12 version_added: 9.3.0
13 author:
14 - Ryan Cook (@cooktheryan)
15 short_description: Bootc Switch and Upgrade
16 description:
17 - This module manages the switching and upgrading of C(bootc).
18 options:
19 state:
20 description:
21 - Control whether to apply the latest image or switch the image.
22 - B(Note:) This does not reboot the system.
23 - Please use M(ansible.builtin.reboot) to reboot the system.
24 required: true
25 type: str
26 choices: ['switch', 'latest']
27 image:
28 description:
29 - The image to switch to.
30 - This is required when O(state=switch).
31 type: str
32 """
33
34 EXAMPLES = r"""
35 # Switch to a different image
36 - name: Provide image to switch to a different image and retain the current running image
37 community.general.bootc_manage:
38 state: switch
39 image: "example.com/image:latest"
40
41 # Apply updates of the current running image
42 - name: Apply updates of the current running image
43 community.general.bootc_manage:
44 state: latest
45 """
46
47 RETURN = r"""
48 """
49
50
51 from ansible.module_utils.basic import AnsibleModule
52 from ansible.module_utils.common.locale import get_best_parsable_locale
53
54
55 def main():
56 argument_spec = dict(
57 state=dict(type="str", required=True, choices=["switch", "latest"]),
58 image=dict(type="str"),
59 )
60 module = AnsibleModule(
61 argument_spec=argument_spec,
62 required_if=[
63 ("state", "switch", ["image"]),
64 ],
65 )
66
67 state = module.params["state"]
68 image = module.params["image"]
69
70 if state == "switch":
71 command = ["bootc", "switch", image, "--retain"]
72 elif state == "latest":
73 command = ["bootc", "upgrade"]
74
75 locale = get_best_parsable_locale(module)
76 module.run_command_environ_update = dict(
77 LANG=locale, LC_ALL=locale, LC_MESSAGES=locale, LC_CTYPE=locale, LANGUAGE=locale
78 )
79 rc, stdout, err = module.run_command(command, check_rc=True)
80
81 if "Queued for next boot: " in stdout:
82 result = {"changed": True, "stdout": stdout}
83 module.exit_json(**result)
84 elif "No changes in " in stdout or "Image specification is unchanged." in stdout:
85 result = {"changed": False, "stdout": stdout}
86 module.exit_json(**result)
87 else:
88 result = {"changed": False, "stderr": err}
89 module.fail_json(msg="ERROR: Command execution failed.", **result)
90
91
92 if __name__ == "__main__":
93 main()