def main(): redis_auth_args = redis_auth_argument_spec() module_args = dict(key=dict(type='str', required=True, no_log=False), ) module_args.update(redis_auth_args) module = AnsibleModule( argument_spec=module_args, supports_check_mode=True, ) fail_imports(module) redis = RedisAnsible(module) key = module.params['key'] result = {'changed': False} value = None try: value = redis.connection.get(key) except Exception as e: msg = 'Failed to get value of key "{0}" with exception: {1}'.format( key, str(e)) result['msg'] = msg module.fail_json(**result) if value is None: msg = 'Key "{0}" does not exist in database'.format(key) result['exists'] = False else: msg = 'Got key "{0}"'.format(key) result['value'] = value result['exists'] = True result['msg'] = msg module.exit_json(**result)
def main(): redis_auth_args = redis_auth_argument_spec() module_args = dict( key=dict(type='str', required=True, no_log=False), increment_int=dict(type='int', required=False), increment_float=dict(type='float', required=False), ) module_args.update(redis_auth_args) module = AnsibleModule( argument_spec=module_args, supports_check_mode=True, mutually_exclusive=[['increment_int', 'increment_float']], ) fail_imports(module) redis = RedisAnsible(module) key = module.params['key'] increment_float = module.params['increment_float'] increment_int = module.params['increment_int'] increment = 1 if increment_float is not None: increment = increment_float elif increment_int is not None: increment = increment_int result = {'changed': False} if module.check_mode: value = 0.0 try: res = redis.connection.get(key) if res is not None: value = float(res) except ValueError as e: msg = 'Value: {0} of key: {1} is not incrementable(int or float)'.format( res, key) result['msg'] = msg module.fail_json(**result) except Exception as e: msg = 'Failed to get value of key: {0} with exception: {1}'.format( key, str(e)) result['msg'] = msg module.fail_json(**result) msg = 'Incremented key: {0} by {1} to {2}'.format( key, increment, value + increment) result['msg'] = msg result['value'] = float(value + increment) module.exit_json(**result) if increment_float is not None: try: value = redis.connection.incrbyfloat(key, increment) msg = 'Incremented key: {0} by {1} to {2}'.format( key, increment, value) result['msg'] = msg result['value'] = float(value) result['changed'] = True module.exit_json(**result) except Exception as e: msg = 'Failed to increment key: {0} by {1} with exception: {2}'.format( key, increment, str(e)) result['msg'] = msg module.fail_json(**result) elif increment_int is not None: try: value = redis.connection.incrby(key, increment) msg = 'Incremented key: {0} by {1} to {2}'.format( key, increment, value) result['msg'] = msg result['value'] = float(value) result['changed'] = True module.exit_json(**result) except Exception as e: msg = 'Failed to increment key: {0} by {1} with exception: {2}'.format( key, increment, str(e)) result['msg'] = msg module.fail_json(**result) else: try: value = redis.connection.incr(key) msg = 'Incremented key: {0} to {1}'.format(key, value) result['msg'] = msg result['value'] = float(value) result['changed'] = True module.exit_json(**result) except Exception as e: msg = 'Failed to increment key: {0} with exception: {1}'.format( key, str(e)) result['msg'] = msg module.fail_json(**result)
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'), ) module_args.update(redis_auth_args) module = AnsibleModule( argument_spec=module_args, supports_check_mode=True, ) fail_imports(module, module.params['tls']) redis_params = redis_auth_params(module) 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'] 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') if not master_port: module.fail_json( msg='In replica mode master port must be provided') # Connect and check r = redis.StrictRedis(**redis_params) try: r.ping() except Exception as e: module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) # Check if we are already in the mode that we want info = r.info() 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: status = dict( status=mode, master_host=master_host, master_port=master_port, ) module.exit_json(changed=False, mode=status) else: # Do the stuff # (Check Check_mode before commands so the commands aren't evaluated # if not necessary) if mode == "replica": 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, } module.exit_json(changed=True, mode=status) else: 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') # flush Command section ----------- elif command == "flush": db = module.params['db'] mode = module.params['flush_mode'] # Check if we have all the data if mode == "db": if db is None: module.fail_json( msg="In db mode the db number must be provided") # Connect and check r = redis.StrictRedis(db=db, **redis_params) try: r.ping() except Exception as e: module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) # Do the stuff # (Check Check_mode before commands so the commands aren't evaluated # if not necessary) if mode == "all": if module.check_mode or flush(r): module.exit_json(changed=True, flushed=True) else: # Flush never fails :) module.fail_json(msg="Unable to flush all databases") else: if module.check_mode or flush(r, db): module.exit_json(changed=True, flushed=True, db=db) else: # Flush never fails :) module.fail_json(msg="Unable to flush '%d' database" % db) 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())) else: value = module.params['value'] except ValueError: value = module.params['value'] r = redis.StrictRedis(**redis_params) try: r.ping() except Exception as e: module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc()) try: old_value = r.config_get(name)[name] except Exception as e: module.fail_json(msg="unable to read config: %s" % to_native(e), exception=traceback.format_exc()) changed = old_value != value if module.check_mode or not changed: module.exit_json(changed=changed, name=name, value=value) else: try: r.config_set(name, value) except Exception as e: module.fail_json(msg="unable to write config: %s" % to_native(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')
def main(): redis_auth_args = redis_auth_argument_spec() module_args = dict( key=dict(type='str', required=True, no_log=False), value=dict(type='str', required=False), expiration=dict(type='int', required=False), non_existing=dict(type='bool', required=False), existing=dict(type='bool', required=False), keep_ttl=dict(type='bool', required=False), state=dict(type='str', default='present', choices=['present', 'absent']), ) module_args.update(redis_auth_args) module = AnsibleModule( argument_spec=module_args, supports_check_mode=True, required_if=[('state', 'present', ('value', ))], mutually_exclusive=[['non_existing', 'existing'], ['keep_ttl', 'expiration']], ) fail_imports(module) redis = RedisAnsible(module) key = module.params['key'] value = module.params['value'] px = module.params['expiration'] nx = module.params['non_existing'] xx = module.params['existing'] keepttl = module.params['keep_ttl'] state = module.params['state'] set_args = { 'name': key, 'value': value, 'px': px, 'nx': nx, 'xx': xx, 'keepttl': keepttl } result = {'changed': False} old_value = None try: old_value = redis.connection.get(key) except Exception as e: msg = 'Failed to get value of key: {0} with exception: {1}'.format( key, str(e)) result['msg'] = msg module.fail_json(**result) if state == 'absent': if module.check_mode: if old_value is None: msg = 'Key: {0} not present'.format(key) result['msg'] = msg module.exit_json(**result) else: msg = 'Deleted key: {0}'.format(key) result['msg'] = msg module.exit_json(**result) try: ret = redis.connection.delete(key) if ret == 0: msg = 'Key: {0} not present'.format(key) result['msg'] = msg module.exit_json(**result) else: msg = 'Deleted key: {0}'.format(key) result['msg'] = msg result['changed'] = True module.exit_json(**result) except Exception as e: msg = 'Failed to delete key: {0} with exception: {1}'.format( key, str(e)) result['msg'] = msg module.fail_json(**result) old_value = None try: old_value = redis.connection.get(key) except Exception as e: msg = 'Failed to get value of key: {0} with exception: {1}'.format( key, str(e)) result['msg'] = msg module.fail_json(**result) result['old_value'] = old_value if old_value == value and keepttl is not False and px is None: msg = 'Key {0} already has desired value'.format(key) result['msg'] = msg result['value'] = value module.exit_json(**result) if module.check_mode: result['msg'] = 'Set key: {0}'.format(key) result['value'] = value module.exit_json(**result) try: ret = redis.connection.set(**set_args) if ret is None: if nx: msg = 'Could not set key: {0}. Key already present.'.format( key) else: msg = 'Could not set key: {0}. Key not present.'.format(key) result['msg'] = msg module.fail_json(**result) msg = 'Set key: {0}'.format(key) result['msg'] = msg result['changed'] = True result['value'] = value module.exit_json(**result) except Exception as e: msg = 'Failed to set key: {0} with exception: {2}'.format(key, str(e)) result['msg'] = msg module.fail_json(**result)