| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2018, Mikhail Gordeev |
| 4 |
|
| 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: apt_repo |
| 12 |
short_description: Manage APT repositories using C(apt-repo) |
| 13 |
description: |
| 14 |
- Manages APT repositories using C(apt-repo) tool. |
| 15 |
- See U(https://www.altlinux.org/Apt-repo) for details about C(apt-repo). |
| 16 |
notes: |
| 17 |
- This module works on ALT based distros. |
| 18 |
- Does NOT support checkmode, due to a limitation in C(apt-repo) tool. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: none |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
repo: |
| 28 |
description: |
| 29 |
- Name of the repository to add or remove. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Indicates the desired repository state. |
| 35 |
choices: [absent, present] |
| 36 |
default: present |
| 37 |
type: str |
| 38 |
remove_others: |
| 39 |
description: |
| 40 |
- Remove other then added repositories. |
| 41 |
- Used if O(state=present). |
| 42 |
type: bool |
| 43 |
default: false |
| 44 |
update: |
| 45 |
description: |
| 46 |
- Update the package database after changing repositories. |
| 47 |
type: bool |
| 48 |
default: false |
| 49 |
author: |
| 50 |
- Mikhail Gordeev (@obirvalger) |
| 51 |
""" |
| 52 |
|
| 53 |
EXAMPLES = r""" |
| 54 |
- name: Remove all repositories |
| 55 |
community.general.apt_repo: |
| 56 |
repo: all |
| 57 |
state: absent |
| 58 |
|
| 59 |
- name: Add repository `Sisysphus` and remove other repositories |
| 60 |
community.general.apt_repo: |
| 61 |
repo: Sisysphus |
| 62 |
state: present |
| 63 |
remove_others: true |
| 64 |
|
| 65 |
- name: Add local repository `/space/ALT/Sisyphus` and update package cache |
| 66 |
community.general.apt_repo: |
| 67 |
repo: copy:///space/ALT/Sisyphus |
| 68 |
state: present |
| 69 |
update: true |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = """ # """ |
| 73 |
|
| 74 |
import os |
| 75 |
|
| 76 |
from ansible.module_utils.basic import AnsibleModule |
| 77 |
|
| 78 |
APT_REPO_PATH = "/usr/bin/apt-repo" |
| 79 |
|
| 80 |
|
| 81 |
def apt_repo(module, *args): |
| 82 |
"""run apt-repo with args and return its output""" |
| 83 |
# make args list to use in concatenation |
| 84 |
args = list(args) |
| 85 |
rc, out, err = module.run_command([APT_REPO_PATH] + args) |
| 86 |
|
| 87 |
if rc != 0: |
| 88 |
module.fail_json(msg=f"'{' '.join(['apt-repo'] + args)}' failed: {err}") |
| 89 |
|
| 90 |
return out |
| 91 |
|
| 92 |
|
| 93 |
def add_repo(module, repo): |
| 94 |
"""add a repository""" |
| 95 |
apt_repo(module, "add", repo) |
| 96 |
|
| 97 |
|
| 98 |
def rm_repo(module, repo): |
| 99 |
"""remove a repository""" |
| 100 |
apt_repo(module, "rm", repo) |
| 101 |
|
| 102 |
|
| 103 |
def set_repo(module, repo): |
| 104 |
"""add a repository and remove other repositories""" |
| 105 |
# first add to validate repository |
| 106 |
apt_repo(module, "add", repo) |
| 107 |
apt_repo(module, "rm", "all") |
| 108 |
apt_repo(module, "add", repo) |
| 109 |
|
| 110 |
|
| 111 |
def update(module): |
| 112 |
"""update package cache""" |
| 113 |
apt_repo(module, "update") |
| 114 |
|
| 115 |
|
| 116 |
def main(): |
| 117 |
module = AnsibleModule( |
| 118 |
argument_spec=dict( |
| 119 |
repo=dict(type="str", required=True), |
| 120 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 121 |
remove_others=dict(type="bool", default=False), |
| 122 |
update=dict(type="bool", default=False), |
| 123 |
), |
| 124 |
) |
| 125 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 126 |
|
| 127 |
if not os.path.exists(APT_REPO_PATH): |
| 128 |
module.fail_json(msg="cannot find /usr/bin/apt-repo") |
| 129 |
|
| 130 |
params = module.params |
| 131 |
repo = params["repo"] |
| 132 |
state = params["state"] |
| 133 |
old_repositories = apt_repo(module) |
| 134 |
|
| 135 |
if state == "present": |
| 136 |
if params["remove_others"]: |
| 137 |
set_repo(module, repo) |
| 138 |
else: |
| 139 |
add_repo(module, repo) |
| 140 |
elif state == "absent": |
| 141 |
rm_repo(module, repo) |
| 142 |
|
| 143 |
if params["update"]: |
| 144 |
update(module) |
| 145 |
|
| 146 |
new_repositories = apt_repo(module) |
| 147 |
changed = old_repositories != new_repositories |
| 148 |
module.exit_json(changed=changed, repo=repo, state=state) |
| 149 |
|
| 150 |
|
| 151 |
if __name__ == "__main__": |
| 152 |
main() |