mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-26 21:33:12 +00:00
Reformat everything.
This commit is contained in:
@@ -151,7 +151,10 @@ else:
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.common.text.formatters import human_to_bytes
|
||||
from ansible_collections.community.general.plugins.module_utils.redis import (
|
||||
fail_imports, redis_auth_argument_spec, redis_auth_params)
|
||||
fail_imports,
|
||||
redis_auth_argument_spec,
|
||||
redis_auth_params,
|
||||
)
|
||||
import re
|
||||
|
||||
|
||||
@@ -185,15 +188,16 @@ def flush(client, db=None):
|
||||
def main():
|
||||
redis_auth_args = redis_auth_argument_spec(tls_default=False)
|
||||
module_args = dict(
|
||||
command=dict(type='str', choices=['config', 'flush', 'replica', 'slave']),
|
||||
master_host=dict(type='str'),
|
||||
master_port=dict(type='int'),
|
||||
replica_mode=dict(type='str', default='replica', choices=['master', 'replica', 'slave'],
|
||||
aliases=["slave_mode"]),
|
||||
db=dict(type='int'),
|
||||
flush_mode=dict(type='str', default='all', choices=['all', 'db']),
|
||||
name=dict(type='str'),
|
||||
value=dict(type='str'),
|
||||
command=dict(type="str", choices=["config", "flush", "replica", "slave"]),
|
||||
master_host=dict(type="str"),
|
||||
master_port=dict(type="int"),
|
||||
replica_mode=dict(
|
||||
type="str", default="replica", choices=["master", "replica", "slave"], aliases=["slave_mode"]
|
||||
),
|
||||
db=dict(type="int"),
|
||||
flush_mode=dict(type="str", default="all", choices=["all", "db"]),
|
||||
name=dict(type="str"),
|
||||
value=dict(type="str"),
|
||||
)
|
||||
module_args.update(redis_auth_args)
|
||||
module = AnsibleModule(
|
||||
@@ -201,29 +205,29 @@ def main():
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
fail_imports(module, module.params['tls'])
|
||||
fail_imports(module, module.params["tls"])
|
||||
|
||||
redis_params = redis_auth_params(module)
|
||||
|
||||
command = module.params['command']
|
||||
command = module.params["command"]
|
||||
if command == "slave":
|
||||
command = "replica"
|
||||
|
||||
# Replica Command section -----------
|
||||
if command == "replica":
|
||||
master_host = module.params['master_host']
|
||||
master_port = module.params['master_port']
|
||||
mode = module.params['replica_mode']
|
||||
master_host = module.params["master_host"]
|
||||
master_port = module.params["master_port"]
|
||||
mode = module.params["replica_mode"]
|
||||
if mode == "slave":
|
||||
mode = "replica"
|
||||
|
||||
# Check if we have all the data
|
||||
if mode == "replica": # Only need data if we want to be replica
|
||||
if not master_host:
|
||||
module.fail_json(msg='In replica mode master host must be provided')
|
||||
module.fail_json(msg="In replica mode master host must be provided")
|
||||
|
||||
if not master_port:
|
||||
module.fail_json(msg='In replica mode master port must be provided')
|
||||
module.fail_json(msg="In replica mode master port must be provided")
|
||||
|
||||
# Connect and check
|
||||
r = redis.StrictRedis(**redis_params)
|
||||
@@ -237,7 +241,12 @@ def main():
|
||||
if mode == "master" and info["role"] == "master":
|
||||
module.exit_json(changed=False, mode=mode)
|
||||
|
||||
elif mode == "replica" and info["role"] == "slave" and info["master_host"] == master_host and info["master_port"] == master_port:
|
||||
elif (
|
||||
mode == "replica"
|
||||
and info["role"] == "slave"
|
||||
and info["master_host"] == master_host
|
||||
and info["master_port"] == master_port
|
||||
):
|
||||
status = dict(
|
||||
status=mode,
|
||||
master_host=master_host,
|
||||
@@ -252,24 +261,24 @@ def main():
|
||||
if module.check_mode or set_replica_mode(r, master_host, master_port):
|
||||
info = r.info()
|
||||
status = {
|
||||
'status': mode,
|
||||
'master_host': master_host,
|
||||
'master_port': master_port,
|
||||
"status": mode,
|
||||
"master_host": master_host,
|
||||
"master_port": master_port,
|
||||
}
|
||||
module.exit_json(changed=True, mode=status)
|
||||
else:
|
||||
module.fail_json(msg='Unable to set replica mode')
|
||||
module.fail_json(msg="Unable to set replica mode")
|
||||
|
||||
else:
|
||||
if module.check_mode or set_master_mode(r):
|
||||
module.exit_json(changed=True, mode=mode)
|
||||
else:
|
||||
module.fail_json(msg='Unable to set master mode')
|
||||
module.fail_json(msg="Unable to set master mode")
|
||||
|
||||
# flush Command section -----------
|
||||
elif command == "flush":
|
||||
db = module.params['db']
|
||||
mode = module.params['flush_mode']
|
||||
db = module.params["db"]
|
||||
mode = module.params["flush_mode"]
|
||||
|
||||
# Check if we have all the data
|
||||
if mode == "db":
|
||||
@@ -297,16 +306,16 @@ def main():
|
||||
module.exit_json(changed=True, flushed=True, db=db)
|
||||
else: # Flush never fails :)
|
||||
module.fail_json(msg=f"Unable to flush '{db}' database")
|
||||
elif command == 'config':
|
||||
name = module.params['name']
|
||||
elif command == "config":
|
||||
name = module.params["name"]
|
||||
|
||||
try: # try to parse the value as if it were the memory size
|
||||
if re.match(r'^\s*(\d*\.?\d*)\s*([A-Za-z]+)?\s*$', module.params['value'].upper()):
|
||||
value = str(human_to_bytes(module.params['value'].upper()))
|
||||
if re.match(r"^\s*(\d*\.?\d*)\s*([A-Za-z]+)?\s*$", module.params["value"].upper()):
|
||||
value = str(human_to_bytes(module.params["value"].upper()))
|
||||
else:
|
||||
value = module.params['value']
|
||||
value = module.params["value"]
|
||||
except ValueError:
|
||||
value = module.params['value']
|
||||
value = module.params["value"]
|
||||
|
||||
r = redis.StrictRedis(**redis_params)
|
||||
|
||||
@@ -330,8 +339,8 @@ def main():
|
||||
module.fail_json(msg=f"unable to write config: {e}", exception=traceback.format_exc())
|
||||
module.exit_json(changed=changed, name=name, value=value)
|
||||
else:
|
||||
module.fail_json(msg='A valid command must be provided')
|
||||
module.fail_json(msg="A valid command must be provided")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user