Debian Codegraph @

lib/ansible/modules/dpkg_selections.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright: Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8
9 DOCUMENTATION = """
10 ---
11 module: dpkg_selections
12 short_description: Dpkg package selection selections
13 description:
14 - Change dpkg package selection state via C(--get-selections) and C(--set-selections).
15 version_added: "2.0"
16 author:
17 - Brian Brazil (@brian-brazil) <[email protected]>
18 options:
19 name:
20 description:
21 - Name of the package.
22 required: true
23 type: str
24 selection:
25 description:
26 - The selection state to set the package to.
27 choices: [ 'install', 'hold', 'deinstall', 'purge' ]
28 required: true
29 type: str
30 extends_documentation_fragment:
31 - action_common_attributes
32 attributes:
33 check_mode:
34 support: full
35 diff_mode:
36 support: full
37 platform:
38 support: full
39 platforms: debian
40 notes:
41 - This module will not cause any packages to be installed/removed/purged, use the M(ansible.builtin.apt) module for that.
42 """
43 EXAMPLES = """
44 - name: Prevent python from being upgraded
45 ansible.builtin.dpkg_selections:
46 name: python
47 selection: hold
48
49 - name: Allow python to be upgraded
50 ansible.builtin.dpkg_selections:
51 name: python
52 selection: install
53 """
54
55 from ansible.module_utils.basic import AnsibleModule
56 from ansible.module_utils.common.locale import get_best_parsable_locale
57
58
59 def main():
60 module = AnsibleModule(
61 argument_spec=dict(
62 name=dict(required=True),
63 selection=dict(choices=['install', 'hold', 'deinstall', 'purge'], required=True)
64 ),
65 supports_check_mode=True,
66 )
67
68 dpkg = module.get_bin_path('dpkg', True)
69
70 locale = get_best_parsable_locale(module)
71 DPKG_ENV = dict(LANG=locale, LC_ALL=locale, LC_MESSAGES=locale, LC_CTYPE=locale, LANGUAGE=locale)
72 module.run_command_environ_update = DPKG_ENV
73
74 name = module.params['name']
75 selection = module.params['selection']
76
77 # Get current settings.
78 rc, out, err = module.run_command([dpkg, '--get-selections', name], check_rc=True)
79 if 'no packages found matching' in err:
80 module.fail_json(msg="Failed to find package '%s' to perform selection '%s'." % (name, selection))
81 elif not out:
82 current = 'not present'
83 else:
84 current = out.split()[1]
85
86 changed = current != selection
87
88 if module.check_mode or not changed:
89 module.exit_json(changed=changed, before=current, after=selection)
90
91 module.run_command([dpkg, '--set-selections'], data="%s %s" % (name, selection), check_rc=True)
92 module.exit_json(changed=changed, before=current, after=selection)
93
94
95 if __name__ == '__main__':
96 main()