mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Consolidate boolean/mk_boolean conversion functions into a single location
Consolidate the module_utils, constants, and config functions that convert values into booleans into a single function in module_utils. Port code to use the module_utils.validate.convert_bool.boolean function isntead of mk_boolean.
This commit is contained in:
@@ -25,9 +25,9 @@ import os.path
|
||||
import re
|
||||
import tempfile
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_native, to_text
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.utils.hashing import checksum_s
|
||||
|
||||
@@ -101,7 +101,7 @@ class ActionModule(ActionBase):
|
||||
result['msg'] = "src and dest are required"
|
||||
return result
|
||||
|
||||
if boolean(remote_src):
|
||||
if boolean(remote_src, strict=False):
|
||||
result.update(self._execute_module(tmp=tmp, task_vars=task_vars))
|
||||
return result
|
||||
else:
|
||||
|
||||
@@ -24,9 +24,9 @@ import os
|
||||
import stat
|
||||
import tempfile
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.errors import AnsibleError, AnsibleFileNotFound
|
||||
from ansible.module_utils._text import to_bytes, to_native, to_text
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.utils.hashing import checksum
|
||||
|
||||
@@ -43,11 +43,11 @@ class ActionModule(ActionBase):
|
||||
source = self._task.args.get('src', None)
|
||||
content = self._task.args.get('content', None)
|
||||
dest = self._task.args.get('dest', None)
|
||||
raw = boolean(self._task.args.get('raw', 'no'))
|
||||
force = boolean(self._task.args.get('force', 'yes'))
|
||||
remote_src = boolean(self._task.args.get('remote_src', False))
|
||||
follow = boolean(self._task.args.get('follow', False))
|
||||
decrypt = boolean(self._task.args.get('decrypt', True))
|
||||
raw = boolean(self._task.args.get('raw', 'no'), strict=False)
|
||||
force = boolean(self._task.args.get('force', 'yes'), strict=False)
|
||||
remote_src = boolean(self._task.args.get('remote_src', False), strict=False)
|
||||
follow = boolean(self._task.args.get('follow', False), strict=False)
|
||||
decrypt = boolean(self._task.args.get('decrypt', True), strict=False)
|
||||
|
||||
result['failed'] = True
|
||||
if (source is None and content is None) or dest is None:
|
||||
|
||||
@@ -20,10 +20,10 @@ __metaclass__ = type
|
||||
import os
|
||||
import base64
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_bytes
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.utils.hashing import checksum, checksum_s, md5, secure_hash
|
||||
from ansible.utils.path import makedirs_safe
|
||||
@@ -51,9 +51,11 @@ class ActionModule(ActionBase):
|
||||
|
||||
source = self._task.args.get('src', None)
|
||||
dest = self._task.args.get('dest', None)
|
||||
flat = boolean(self._task.args.get('flat'))
|
||||
fail_on_missing = boolean(self._task.args.get('fail_on_missing'))
|
||||
validate_checksum = boolean(self._task.args.get('validate_checksum', self._task.args.get('validate_md5', True)))
|
||||
flat = boolean(self._task.args.get('flat'), strict=False)
|
||||
fail_on_missing = boolean(self._task.args.get('fail_on_missing'), strict=False)
|
||||
validate_checksum = boolean(self._task.args.get('validate_checksum',
|
||||
self._task.args.get('validate_md5', True)),
|
||||
strict=False)
|
||||
|
||||
# validate source and dest are strings FIXME: use basic.py and module specs
|
||||
if not isinstance(source, string_types):
|
||||
|
||||
@@ -20,9 +20,9 @@ __metaclass__ = type
|
||||
|
||||
import os
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ class ActionModule(ActionBase):
|
||||
result = super(ActionModule, self).run(tmp, task_vars)
|
||||
|
||||
src = self._task.args.get('src', None)
|
||||
remote_src = boolean(self._task.args.get('remote_src', 'no'))
|
||||
remote_src = boolean(self._task.args.get('remote_src', 'no'), strict=False)
|
||||
|
||||
if src is None:
|
||||
result['failed'] = True
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.module_utils.six import iteritems, string_types
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.utils.vars import isidentifier
|
||||
|
||||
@@ -46,7 +46,7 @@ class ActionModule(ActionBase):
|
||||
return result
|
||||
|
||||
if isinstance(v, string_types) and v.lower() in ('true', 'false', 'yes', 'no'):
|
||||
v = boolean(v)
|
||||
v = boolean(v, strict=False)
|
||||
facts[k] = v
|
||||
|
||||
result['changed'] = False
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.module_utils.six import iteritems, string_types
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.utils.vars import isidentifier
|
||||
|
||||
@@ -53,7 +53,7 @@ class ActionModule(ActionBase):
|
||||
val = self._task.args.get(opt, None)
|
||||
if val is not None:
|
||||
if not isinstance(val, bool):
|
||||
stats[opt] = boolean(self._templar.template(val))
|
||||
stats[opt] = boolean(self._templar.template(val), strict=False)
|
||||
else:
|
||||
stats[opt] = val
|
||||
|
||||
|
||||
@@ -23,12 +23,11 @@ from collections import MutableSequence
|
||||
from ansible import constants as C
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.playbook.play_context import MAGIC_VARIABLE_MAPPING
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.plugins import connection_loader
|
||||
|
||||
boolean = C.mk_boolean
|
||||
|
||||
|
||||
class ActionModule(ActionBase):
|
||||
|
||||
@@ -314,7 +313,7 @@ class ActionModule(ActionBase):
|
||||
user = None
|
||||
if not dest_is_local:
|
||||
# Src and dest rsync "path" handling
|
||||
if boolean(_tmp_args.get('set_remote_user', 'yes')):
|
||||
if boolean(_tmp_args.get('set_remote_user', 'yes'), strict=False):
|
||||
if use_delegate:
|
||||
user = task_vars.get('ansible_delegated_vars', dict()).get('ansible_ssh_user', None)
|
||||
if not user:
|
||||
|
||||
@@ -22,12 +22,11 @@ import os
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleError, AnsibleFileNotFound
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.template import generate_ansible_template_vars
|
||||
from ansible.utils.hashing import checksum_s
|
||||
|
||||
boolean = C.mk_boolean
|
||||
|
||||
|
||||
class ActionModule(ActionBase):
|
||||
|
||||
@@ -58,7 +57,7 @@ class ActionModule(ActionBase):
|
||||
|
||||
source = self._task.args.get('src', None)
|
||||
dest = self._task.args.get('dest', None)
|
||||
force = boolean(self._task.args.get('force', True))
|
||||
force = boolean(self._task.args.get('force', True), strict=False)
|
||||
state = self._task.args.get('state', None)
|
||||
newline_sequence = self._task.args.get('newline_sequence', self.DEFAULT_NEWLINE_SEQUENCE)
|
||||
variable_start_string = self._task.args.get('variable_start_string', None)
|
||||
|
||||
@@ -21,10 +21,9 @@ __metaclass__ = type
|
||||
import os
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.action import ActionBase
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
|
||||
|
||||
class ActionModule(ActionBase):
|
||||
@@ -40,7 +39,7 @@ class ActionModule(ActionBase):
|
||||
|
||||
source = self._task.args.get('src', None)
|
||||
dest = self._task.args.get('dest', None)
|
||||
remote_src = boolean(self._task.args.get('remote_src', False))
|
||||
remote_src = boolean(self._task.args.get('remote_src', False), strict=False)
|
||||
creates = self._task.args.get('creates', None)
|
||||
decrypt = self._task.args.get('decrypt', True)
|
||||
|
||||
@@ -53,7 +52,7 @@ class ActionModule(ActionBase):
|
||||
return result
|
||||
# We will take the information from copy and store it in
|
||||
# the remote_src var to use later in this file.
|
||||
self._task.args['remote_src'] = remote_src = not boolean(self._task.args.pop('copy'))
|
||||
self._task.args['remote_src'] = remote_src = not boolean(self._task.args.pop('copy'), strict=False)
|
||||
|
||||
if source is None or dest is None:
|
||||
result['failed'] = True
|
||||
@@ -79,17 +78,17 @@ class ActionModule(ActionBase):
|
||||
if not remote_src:
|
||||
try:
|
||||
source = self._loader.get_real_file(self._find_needle('files', source), decrypt=decrypt)
|
||||
except AnsibleError:
|
||||
except AnsibleError as e:
|
||||
result['failed'] = True
|
||||
result['msg'] = to_native(get_exception())
|
||||
result['msg'] = to_text(e)
|
||||
self._remove_tmp_path(tmp)
|
||||
return result
|
||||
|
||||
try:
|
||||
remote_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=True)
|
||||
except AnsibleError:
|
||||
except AnsibleError as e:
|
||||
result['failed'] = True
|
||||
result['msg'] = to_native(get_exception())
|
||||
result['msg'] = to_text(e)
|
||||
self._remove_tmp_path(tmp)
|
||||
return result
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ try:
|
||||
except ImportError:
|
||||
cli = None
|
||||
|
||||
from ansible.constants import mk_boolean
|
||||
from ansible.module_utils.urls import open_url
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.callback import CallbackBase
|
||||
|
||||
try:
|
||||
@@ -79,8 +79,9 @@ class CallbackModule(CallbackBase):
|
||||
self.webhook_url = os.getenv('SLACK_WEBHOOK_URL')
|
||||
self.channel = os.getenv('SLACK_CHANNEL', '#ansible')
|
||||
self.username = os.getenv('SLACK_USERNAME', 'ansible')
|
||||
self.show_invocation = mk_boolean(
|
||||
os.getenv('SLACK_INVOCATION', self._display.verbosity > 1)
|
||||
self.show_invocation = boolean(
|
||||
os.getenv('SLACK_INVOCATION', self._display.verbosity > 1),
|
||||
strict=False
|
||||
)
|
||||
|
||||
if self.webhook_url is None:
|
||||
|
||||
@@ -111,16 +111,14 @@ from functools import wraps
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNotFound
|
||||
from ansible.errors import AnsibleOptionsError
|
||||
from ansible.module_utils.basic import BOOLEANS
|
||||
from ansible.compat import selectors
|
||||
from ansible.module_utils.six import PY3, text_type, binary_type
|
||||
from ansible.module_utils.six.moves import shlex_quote
|
||||
from ansible.module_utils._text import to_bytes, to_native, to_text
|
||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS, boolean
|
||||
from ansible.plugins.connection import ConnectionBase, BUFSIZE
|
||||
from ansible.utils.path import unfrackpath, makedirs_safe
|
||||
|
||||
boolean = C.mk_boolean
|
||||
|
||||
try:
|
||||
from __main__ import display
|
||||
except ImportError:
|
||||
@@ -791,7 +789,7 @@ class Connection(ConnectionBase):
|
||||
if not isinstance(scp_if_ssh, bool):
|
||||
scp_if_ssh = scp_if_ssh.lower()
|
||||
if scp_if_ssh in BOOLEANS:
|
||||
scp_if_ssh = boolean(scp_if_ssh)
|
||||
scp_if_ssh = boolean(scp_if_ssh, strict=False)
|
||||
elif scp_if_ssh != 'smart':
|
||||
raise AnsibleOptionsError('scp_if_ssh needs to be one of [smart|True|False]')
|
||||
if scp_if_ssh == 'smart':
|
||||
|
||||
@@ -122,9 +122,9 @@ import os
|
||||
|
||||
from jinja2.exceptions import UndefinedError
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.errors import AnsibleFileNotFound, AnsibleLookupError, AnsibleUndefinedVariable
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@ class LookupModule(LookupBase):
|
||||
if isinstance(term, dict):
|
||||
files = term.get('files', [])
|
||||
paths = term.get('paths', [])
|
||||
skip = boolean(term.get('skip', False))
|
||||
skip = boolean(term.get('skip', False), strict=False)
|
||||
|
||||
filelist = files
|
||||
if isinstance(files, string_types):
|
||||
|
||||
@@ -46,8 +46,8 @@ __metaclass__ = type
|
||||
import os
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
|
||||
HAS_HVAC = False
|
||||
try:
|
||||
@@ -149,7 +149,7 @@ class HashiVault:
|
||||
self.client.auth_ldap(username, password, mount_point)
|
||||
|
||||
def boolean_or_cacert(self, validate_certs, cacert):
|
||||
validate_certs = boolean(validate_certs)
|
||||
validate_certs = boolean(validate_certs, strict=False)
|
||||
'''' return a bool or cacert '''
|
||||
if validate_certs is True:
|
||||
if cacert != '':
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.constants import mk_boolean as boolean
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible.module_utils.parsing.convert_bool import boolean
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.utils.listify import listify_lookup_plugin_terms
|
||||
|
||||
@@ -72,7 +72,7 @@ class LookupModule(LookupBase):
|
||||
# this particular item is to be skipped
|
||||
continue
|
||||
|
||||
skip_missing = boolean(flags.get('skip_missing', False))
|
||||
skip_missing = boolean(flags.get('skip_missing', False), strict=False)
|
||||
subvalue = item0
|
||||
lastsubkey = False
|
||||
sublist = []
|
||||
|
||||
Reference in New Issue
Block a user