2020-04-02 17:50:13 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2018, Mikhail Gordeev
|
2020-04-02 17:50:13 +02:00
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-04-02 17:50:13 +02:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: apt_repo
|
|
|
|
short_description: Manage APT repositories via apt-repo
|
|
|
|
description:
|
|
|
|
- Manages APT repositories using apt-repo tool.
|
|
|
|
- See U(https://www.altlinux.org/Apt-repo) for details about apt-repo
|
|
|
|
notes:
|
|
|
|
- This module works on ALT based distros.
|
|
|
|
- Does NOT support checkmode, due to a limitation in apt-repo tool.
|
2023-02-24 09:25:20 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: none
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-04-02 17:50:13 +02:00
|
|
|
options:
|
|
|
|
repo:
|
|
|
|
description:
|
|
|
|
- Name of the repository to add or remove.
|
|
|
|
required: true
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-04-02 17:50:13 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicates the desired repository state.
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
2020-06-26 15:17:11 +02:00
|
|
|
type: str
|
2020-04-02 17:50:13 +02:00
|
|
|
remove_others:
|
|
|
|
description:
|
|
|
|
- Remove other then added repositories
|
2023-06-15 15:46:33 +02:00
|
|
|
- Used if O(state=present)
|
2020-04-02 17:50:13 +02:00
|
|
|
type: bool
|
2022-08-24 19:59:13 +02:00
|
|
|
default: false
|
2020-04-02 17:50:13 +02:00
|
|
|
update:
|
|
|
|
description:
|
|
|
|
- Update the package database after changing repositories.
|
|
|
|
type: bool
|
2022-08-24 19:59:13 +02:00
|
|
|
default: false
|
2020-04-02 17:50:13 +02:00
|
|
|
author:
|
|
|
|
- Mikhail Gordeev (@obirvalger)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Remove all repositories
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_repo:
|
2020-04-02 17:50:13 +02:00
|
|
|
repo: all
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Add repository `Sisysphus` and remove other repositories
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_repo:
|
2020-04-02 17:50:13 +02:00
|
|
|
repo: Sisysphus
|
|
|
|
state: present
|
2022-08-24 19:59:13 +02:00
|
|
|
remove_others: true
|
2020-04-02 17:50:13 +02:00
|
|
|
|
|
|
|
- name: Add local repository `/space/ALT/Sisyphus` and update package cache
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.apt_repo:
|
2020-04-02 17:50:13 +02:00
|
|
|
repo: copy:///space/ALT/Sisyphus
|
|
|
|
state: present
|
2022-08-24 19:59:13 +02:00
|
|
|
update: true
|
2020-04-02 17:50:13 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = ''' # '''
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
APT_REPO_PATH = "/usr/bin/apt-repo"
|
|
|
|
|
|
|
|
|
|
|
|
def apt_repo(module, *args):
|
|
|
|
"""run apt-repo with args and return its output"""
|
|
|
|
# make args list to use in concatenation
|
|
|
|
args = list(args)
|
|
|
|
rc, out, err = module.run_command([APT_REPO_PATH] + args)
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg="'%s' failed: %s" % (' '.join(['apt-repo'] + args), err))
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def add_repo(module, repo):
|
|
|
|
"""add a repository"""
|
|
|
|
apt_repo(module, 'add', repo)
|
|
|
|
|
|
|
|
|
|
|
|
def rm_repo(module, repo):
|
|
|
|
"""remove a repository"""
|
|
|
|
apt_repo(module, 'rm', repo)
|
|
|
|
|
|
|
|
|
|
|
|
def set_repo(module, repo):
|
|
|
|
"""add a repository and remove other repositories"""
|
|
|
|
# first add to validate repository
|
|
|
|
apt_repo(module, 'add', repo)
|
|
|
|
apt_repo(module, 'rm', 'all')
|
|
|
|
apt_repo(module, 'add', repo)
|
|
|
|
|
|
|
|
|
|
|
|
def update(module):
|
|
|
|
"""update package cache"""
|
|
|
|
apt_repo(module, 'update')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
repo=dict(type='str', required=True),
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
remove_others=dict(type='bool', default=False),
|
|
|
|
update=dict(type='bool', default=False),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
if not os.path.exists(APT_REPO_PATH):
|
|
|
|
module.fail_json(msg='cannot find /usr/bin/apt-repo')
|
|
|
|
|
|
|
|
params = module.params
|
|
|
|
repo = params['repo']
|
|
|
|
state = params['state']
|
|
|
|
old_repositories = apt_repo(module)
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if params['remove_others']:
|
|
|
|
set_repo(module, repo)
|
|
|
|
else:
|
|
|
|
add_repo(module, repo)
|
|
|
|
elif state == 'absent':
|
|
|
|
rm_repo(module, repo)
|
|
|
|
|
|
|
|
if params['update']:
|
|
|
|
update(module)
|
|
|
|
|
|
|
|
new_repositories = apt_repo(module)
|
|
|
|
changed = old_repositories != new_repositories
|
|
|
|
module.exit_json(changed=changed, repo=repo, state=state)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|