mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Add BusyBox support to group module (#54689)
* Add BusyBox support to group module * Use bytes when reading/writing to file
This commit is contained in:
@@ -56,6 +56,7 @@ options:
|
||||
non_unique:
|
||||
description:
|
||||
- This option allows to change the group ID to a non-unique value. Requires C(gid).
|
||||
- Not supported on macOS or BusyBox distributions.
|
||||
type: bool
|
||||
default: no
|
||||
version_added: "2.8"
|
||||
@@ -75,6 +76,7 @@ EXAMPLES = '''
|
||||
|
||||
import grp
|
||||
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.basic import AnsibleModule, load_platform_subclass
|
||||
|
||||
|
||||
@@ -463,6 +465,63 @@ class NetBsdGroup(Group):
|
||||
|
||||
# ===========================================
|
||||
|
||||
|
||||
class BusyBoxGroup(Group):
|
||||
"""
|
||||
BusyBox group manipulation class for systems that have addgroup and delgroup.
|
||||
|
||||
It overrides the following methods:
|
||||
- group_add()
|
||||
- group_del()
|
||||
- group_mod()
|
||||
"""
|
||||
|
||||
def group_add(self, **kwargs):
|
||||
cmd = [self.module.get_bin_path('addgroup', True)]
|
||||
if self.gid is not None:
|
||||
cmd.extend(['-g', str(self.gid)])
|
||||
|
||||
if self.system:
|
||||
cmd.append('-S')
|
||||
|
||||
cmd.append(self.name)
|
||||
|
||||
return self.execute_command(cmd)
|
||||
|
||||
def group_del(self):
|
||||
cmd = [self.module.get_bin_path('delgroup', True), self.name]
|
||||
return self.execute_command(cmd)
|
||||
|
||||
def group_mod(self, **kwargs):
|
||||
# Since there is no groupmod command, modify /etc/group directly
|
||||
info = self.group_info()
|
||||
if self.gid is not None and self.gid != info[2]:
|
||||
with open('/etc/group', 'rb') as f:
|
||||
b_groups = f.read()
|
||||
|
||||
b_name = to_bytes(self.name)
|
||||
b_current_group_string = b'%s:x:%d:' % (b_name, info[2])
|
||||
b_new_group_string = b'%s:x:%d:' % (b_name, self.gid)
|
||||
|
||||
if b':%d:' % self.gid in b_groups:
|
||||
self.module.fail_json(msg="gid '{gid}' in use".format(gid=self.gid))
|
||||
|
||||
if self.module.check_mode:
|
||||
return 0, '', ''
|
||||
b_new_groups = b_groups.replace(b_current_group_string, b_new_group_string)
|
||||
with open('/etc/group', 'wb') as f:
|
||||
f.write(b_new_groups)
|
||||
return 0, '', ''
|
||||
|
||||
return None, '', ''
|
||||
|
||||
|
||||
class AlpineGroup(BusyBoxGroup):
|
||||
|
||||
platform = 'Linux'
|
||||
distribution = 'Alpine'
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
|
||||
Reference in New Issue
Block a user