mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-22 22:11:44 -07:00
Ansible system module: sanity pep8 fixes (#32314)
* Ansible system module: pep8 fixes * Clean up documentation * Blank line change not required by PEP8 * Update legacy-files.txt * Documentation updates * Update documentation * Documentation update
This commit is contained in:
parent
48ab1a1334
commit
d21a6aa147
5 changed files with 143 additions and 195 deletions
|
@ -1,18 +1,16 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# (c) 2014, Nate Coraor <nate@bx.psu.edu>
|
||||
# Copyright: (c) 2014, Nate Coraor <nate@bx.psu.edu>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
module: capabilities
|
||||
|
@ -24,18 +22,16 @@ options:
|
|||
path:
|
||||
description:
|
||||
- Specifies the path to the file to be managed.
|
||||
required: true
|
||||
default: null
|
||||
required: yes
|
||||
capability:
|
||||
description:
|
||||
- Desired capability to set (with operator and flags, if state is C(present)) or remove (if state is C(absent))
|
||||
required: true
|
||||
default: null
|
||||
aliases: [ 'cap' ]
|
||||
required: yes
|
||||
aliases: [ cap ]
|
||||
state:
|
||||
description:
|
||||
- Whether the entry should be present or absent in the file's capabilities.
|
||||
choices: [ "present", "absent" ]
|
||||
choices: [ absent, present ]
|
||||
default: present
|
||||
notes:
|
||||
- The capabilities system will automatically transform operators and flags
|
||||
|
@ -43,19 +39,19 @@ notes:
|
|||
cap_foo+ep). This module does not attempt to determine the final operator
|
||||
and flags to compare, so you will want to ensure that your capabilities
|
||||
argument matches the final capabilities.
|
||||
requirements: []
|
||||
author: "Nate Coraor (@natefoo)"
|
||||
author:
|
||||
- Nate Coraor (@natefoo)
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Set cap_sys_chroot+ep on /foo
|
||||
- capabilities:
|
||||
- name: Set cap_sys_chroot+ep on /foo
|
||||
capabilities:
|
||||
path: /foo
|
||||
capability: cap_sys_chroot+ep
|
||||
state: present
|
||||
|
||||
# Remove cap_net_bind_service from /bar
|
||||
- capabilities:
|
||||
- name: Remove cap_net_bind_service from /bar
|
||||
capabilities:
|
||||
path: /bar
|
||||
capability: cap_net_bind_service
|
||||
state: absent
|
||||
|
@ -63,30 +59,28 @@ EXAMPLES = '''
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
|
||||
OPS = ( '=', '-', '+' )
|
||||
OPS = ('=', '-', '+')
|
||||
|
||||
|
||||
class CapabilitiesModule(object):
|
||||
|
||||
platform = 'Linux'
|
||||
distribution = None
|
||||
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
self.path = module.params['path'].strip()
|
||||
self.capability = module.params['capability'].strip().lower()
|
||||
self.state = module.params['state']
|
||||
self.getcap_cmd = module.get_bin_path('getcap', required=True)
|
||||
self.setcap_cmd = module.get_bin_path('setcap', required=True)
|
||||
self.capability_tup = self._parse_cap(self.capability, op_required=self.state=='present')
|
||||
self.module = module
|
||||
self.path = module.params['path'].strip()
|
||||
self.capability = module.params['capability'].strip().lower()
|
||||
self.state = module.params['state']
|
||||
self.getcap_cmd = module.get_bin_path('getcap', required=True)
|
||||
self.setcap_cmd = module.get_bin_path('setcap', required=True)
|
||||
self.capability_tup = self._parse_cap(self.capability, op_required=self.state == 'present')
|
||||
|
||||
self.run()
|
||||
|
||||
def run(self):
|
||||
|
||||
current = self.getcap(self.path)
|
||||
caps = [ cap[0] for cap in current ]
|
||||
caps = [cap[0] for cap in current]
|
||||
|
||||
if self.state == 'present' and self.capability_tup not in current:
|
||||
# need to add capability
|
||||
|
@ -96,7 +90,7 @@ class CapabilitiesModule(object):
|
|||
# remove from current cap list if it's already set (but op/flags differ)
|
||||
current = list(filter(lambda x: x[0] != self.capability_tup[0], current))
|
||||
# add new cap with correct op/flags
|
||||
current.append( self.capability_tup )
|
||||
current.append(self.capability_tup)
|
||||
self.module.exit_json(changed=True, state=self.state, msg='capabilities changed', stdout=self.setcap(self.path, current))
|
||||
elif self.state == 'absent' and self.capability_tup[0] in caps:
|
||||
# need to remove capability
|
||||
|
@ -130,13 +124,13 @@ class CapabilitiesModule(object):
|
|||
cap_group = cap.split(',')
|
||||
cap_group[-1], op, flags = self._parse_cap(cap_group[-1])
|
||||
for subcap in cap_group:
|
||||
rval.append( ( subcap, op, flags ) )
|
||||
rval.append((subcap, op, flags))
|
||||
else:
|
||||
rval.append(self._parse_cap(cap))
|
||||
return rval
|
||||
|
||||
def setcap(self, path, caps):
|
||||
caps = ' '.join([ ''.join(cap) for cap in caps ])
|
||||
caps = ' '.join([''.join(cap) for cap in caps])
|
||||
cmd = "%s '%s' %s" % (self.setcap_cmd, caps, path)
|
||||
rc, stdout, stderr = self.module.run_command(cmd)
|
||||
if rc != 0:
|
||||
|
@ -160,19 +154,19 @@ class CapabilitiesModule(object):
|
|||
cap, flags = cap.split(op)
|
||||
return (cap, op, flags)
|
||||
|
||||
|
||||
# ==============================================================
|
||||
# main
|
||||
|
||||
def main():
|
||||
|
||||
# defining module
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
path = dict(aliases=['key'], required=True),
|
||||
capability = dict(aliases=['cap'], required=True),
|
||||
state = dict(default='present', choices=['present', 'absent']),
|
||||
argument_spec=dict(
|
||||
path=dict(type='str', required=True, aliases=['key']),
|
||||
capability=dict(type='str', required=True, aliases=['cap']),
|
||||
state=dict(type='str', default='present', choices=['absent', 'present']),
|
||||
),
|
||||
supports_check_mode=True
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
CapabilitiesModule(module)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue