feat: align modules to linter

This commit is contained in:
Dominik Kaminski 2021-08-30 19:03:37 +02:00
parent d6faee5fa6
commit ea9e69d75f
No known key found for this signature in database
GPG Key ID: C337AE35564F75F6
8 changed files with 131 additions and 61 deletions

56
.ansible-lint Normal file
View File

@ -0,0 +1,56 @@
---
# .ansible-lint
exclude_paths: []
# parseable: true
# quiet: true
# verbosity: 1
# Mock modules or roles in order to pass ansible-playbook --syntax-check
mock_modules: []
mock_roles: []
# Enable checking of loop variable prefixes in roles
loop_var_prefix: "{role}_"
# Enforce variable names to follow pattern below, in addition to Ansible own
# requirements, like avoiding python identifiers. To disable add `var-naming`
# to skip_list.
# var_naming_pattern: "^[a-z_][a-z0-9_]*$"
use_default_rules: true
# Load custom rules from this specific folder
# rulesdir:
# - ./rule/directory/
# This makes linter to fully ignore rules/tags listed below
skip_list: []
# Any rule that has the 'opt-in' tag will not be loaded unless its 'id' is
# mentioned in the enable_list:
enable_list:
- "fqcn-builtins" # opt-in
- "no-log-password" # opt-in
- "no-same-owner" # opt-in
# add yaml here if you want to avoid ignoring yaml checks when yamllint
# library is missing. Normally its absence just skips using that rule.
# - yaml
# Report only a subset of tags and fully ignore any others
# tags:
# - var-spacing
# This makes the linter display but not fail for rules/tags listed below:
warn_list: []
# Offline mode disables installation of requirements.yml
offline: false
# Define required Ansible's variables to satisfy syntax check
extra_vars: {}
# Uncomment to enforce action validation with tasks, usually is not
# needed as Ansible syntax check also covers it.
# skip_action_validation: false
# List of additional kind:pattern to be added at the top of the default
# match list, first match determines the file kind.
kinds: []

View File

@ -0,0 +1 @@
../../../..

2
.flake8 Normal file
View File

@ -0,0 +1,2 @@
[flake8]
max-line-length = 120

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.cache

View File

@ -16,6 +16,7 @@ rules:
level: "error"
line-length:
max: 120
level: "warning"
new-line-at-end-of-file:
@ -23,8 +24,7 @@ rules:
quoted-strings:
quote-type: "double"
required: only-when-needed
extra-required: ["^(?:(?!(var_)).)*$"]
required: true
truthy:
level: "error"

View File

@ -1,5 +1,3 @@
# ansible-modules
# ucs_modules
Ansible Modules for UCS
The modules are located in library. An example playbook using the modules is test_play.yml. It can also be used to test the functionality of the modules.

View File

@ -1,44 +1,45 @@
---
### REQUIRED
# The namespace of the collection. This can be a company/brand/organization or product namespace under which all
# content lives. May only contain alphanumeric lowercase characters and underscores. Namespaces cannot start with
# underscores or numbers and cannot contain consecutive underscores
namespace: univention
namespace: "univention"
# The name of the collection. Has the same character restrictions as 'namespace'
name: ucs_modules
name: "ucs_modules"
# The version of the collection. Must be compatible with semantic versioning
version: 0.0.0
version: "0.0.0"
# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
readme: "README.md"
# A list of the collection's content authors. Can be just the name or in the format 'Full Name <email> (url)
# @nicks:irc/im.site#channel'
authors:
- Univention GmbH
- "Univention GmbH"
### OPTIONAL but strongly recommended
# A short summary description of the collection
description: Ansible modules for Univention UCS
description: "Ansible modules for Univention UCS"
# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only
# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file'
license:
- GPL-3.0-or-later
- "GPL-3.0-or-later"
# The path to the license file for the collection. This path is relative to the root of the collection. This key is
# mutually exclusive with 'license'
license_file: ''
license_file: "LICENSE"
# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character
# requirements as 'namespace' and 'name'
tags:
- univention
- ucs
- "univention"
- "ucs"
# Collections that this collection requires to be installed for it to be usable. The key of the dict is the
# collection label 'namespace.name'. The value is a version range
@ -47,21 +48,26 @@ tags:
dependencies: {}
# The URL of the originating SCM repository
repository: https://github.com/univention/ansible-modules
repository: "https://github.com/univention/ansible-modules"
# The URL to any online docs
#documentation: http://docs.example.com
documentation: "https://github.com/univention/ansible-modules"
# The URL to the homepage of the collection/project
homepage: https://www.univention.com
homepage: "https://www.univention.com"
# The URL to the collection issue tracker
issues: https://forge.univention.org
issues: "https://forge.univention.org"
# Ignore irrelevant files
build_ignore:
- .gitlab-ci.yml
- .gitkeep
- .gitignore
- '*.tar.gz'
- ci
- ".ansible-lint"
- ".cache/"
- ".flake8"
- ".gitkeep"
- ".gitignore"
- ".gitlab-ci.yml"
- ".pre-commit-config.yaml"
- ".releaserc"
- ".yamllint"
- "*.tar.gz"

View File

@ -1,4 +1,7 @@
#!/usr/bin/python
import datetime
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = r'''
---
module: univention_config_registry
@ -87,8 +90,6 @@ message:
description: A human-readable information about which keys where changed
'''
import datetime
from ansible.module_utils.basic import AnsibleModule
try:
from univention.config_registry.backend import ConfigRegistry
@ -97,6 +98,7 @@ try:
except ImportError:
have_config_registry = False
def _commit_files(files, result, module):
result['changed'] = len(files) > 0
@ -122,13 +124,13 @@ def _commit_files(files, result, module):
ucr_handlers.commit(ucr, files)
endd = datetime.datetime.now()
result['start'] = str(startd)
result['end'] = str(endd)
result['delta'] = str(endd - startd)
endd = datetime.datetime.now()
result['start'] = str(startd)
result['end'] = str(endd)
result['delta'] = str(endd - startd)
result['meta']['commited_templates'] = files
result['message'] = "These files were be commited: {}".format(" ".join(files))
result['failed'] = 0
result['message'] = "These files were be commited: {}".format(" ".join(files))
result['failed'] = 0
# FIXME: Currently the function cannot fail
# if error != 0:
@ -140,7 +142,7 @@ def _set_keys(keys, result, module):
ucr.load()
def needs_change(key):
if not key in ucr:
if key not in ucr:
return True
if isinstance(keys[key], bool):
if keys[key] and not ucr.is_true(key):
@ -165,30 +167,31 @@ def _set_keys(keys, result, module):
if not result['changed']:
return
args = ["/usr/sbin/univention-config-registry", "set"] + [ "{0}={1}".format(key, keys[key]) for key in to_set ]
startd = datetime.datetime.now()
args = ["/usr/sbin/univention-config-registry", "set"] + ["{0}={1}".format(key, keys[key]) for key in to_set]
startd = datetime.datetime.now()
rc, out, err = module.run_command(args)
rc, out, err = module.run_command(args)
endd = datetime.datetime.now()
result['start'] = str(startd)
result['end'] = str(endd)
result['delta'] = str(endd - startd)
result['out'] = out.rstrip("\r\n")
result['err'] = err.rstrip("\r\n")
result['rc'] = rc
result['message'] = "These keys were set: {}".format(" ".join(to_set))
endd = datetime.datetime.now()
result['start'] = str(startd)
result['end'] = str(endd)
result['delta'] = str(endd - startd)
result['out'] = out.rstrip("\r\n")
result['err'] = err.rstrip("\r\n")
result['rc'] = rc
result['message'] = "These keys were set: {}".format(" ".join(to_set))
result['meta']['changed_keys'] = to_set
result['failed'] = rc != 0 or len(err) > 0
result['failed'] = rc != 0 or len(err) > 0
if rc != 0:
module.fail_json(msg='non-zero return code', **result)
def _unset_keys(keys, result, module):
ucr = ConfigRegistry()
ucr.load()
to_unset = [ key for key in keys if key in ucr ]
to_unset = [key for key in keys if key in ucr]
result['changed'] = len(to_unset) > 0
if not result['changed']:
@ -202,25 +205,26 @@ def _unset_keys(keys, result, module):
if not result['changed']:
return
args = ["/usr/sbin/univention-config-registry", "unset"] + to_unset
startd = datetime.datetime.now()
args = ["/usr/sbin/univention-config-registry", "unset"] + to_unset
startd = datetime.datetime.now()
rc, out, err = module.run_command(args)
rc, out, err = module.run_command(args)
endd = datetime.datetime.now()
result['start'] = str(startd)
result['end'] = str(endd)
result['delta'] = str(endd - startd)
result['out'] = out.rstrip("\r\n")
result['err'] = err.rstrip("\r\n")
result['rc'] = rc
result['message'] = "These keys were unset: {}".format(" ".join(to_unset))
endd = datetime.datetime.now()
result['start'] = str(startd)
result['end'] = str(endd)
result['delta'] = str(endd - startd)
result['out'] = out.rstrip("\r\n")
result['err'] = err.rstrip("\r\n")
result['rc'] = rc
result['message'] = "These keys were unset: {}".format(" ".join(to_unset))
result['meta']['changed_keys'] = to_unset
result['failed'] = rc != 0
result['failed'] = rc != 0
if rc != 0:
module.fail_json(msg='non-zero return code', **result)
def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict(
@ -244,11 +248,13 @@ def run_module():
if not have_config_registry:
module.fail_json(msg='The Python "univention.config_registry.backend" is not available', **result)
if not (('keys' in module.params and module.params['keys']) or ('kvlist' in module.params and module.params['kvlist']) or ('commit' in module.params and module.params['commit'])):
if not (('keys' in module.params and module.params['keys'])
or ('kvlist' in module.params and module.params['kvlist'])
or ('commit' in module.params and module.params['commit'])):
module.fail_json(msg='Either "keys", "kvlist" or "commit" is required.', **result)
state = module.params['state']
keys = module.params['keys'] if 'keys' in module.params and module.params['keys'] else dict()
state = module.params['state']
keys = module.params['keys'] if 'keys' in module.params and module.params['keys'] else dict()
commit = module.params['commit'] if 'commit' in module.params and module.params['commit'] else list()
if 'kvlist' in module.params and module.params['kvlist']: