mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-25 05:02:46 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -180,97 +180,106 @@ def main():
|
|||
module = AnsibleModule(
|
||||
supports_check_mode=True,
|
||||
argument_spec=dict(
|
||||
state=dict(type='str', choices=['present', 'absent'], default='present'),
|
||||
name=dict(type='str', ),
|
||||
address=dict(type='str', ),
|
||||
subscriptions=dict(type='list', elements="str"),
|
||||
safe_mode=dict(type='bool', default=False),
|
||||
redact=dict(type='list', elements="str"),
|
||||
socket=dict(type='dict'),
|
||||
keepalives=dict(type='bool', default=True),
|
||||
keepalive=dict(type='dict'),
|
||||
registration=dict(type='dict'),
|
||||
deregister=dict(type='bool'),
|
||||
deregistration=dict(type='dict'),
|
||||
ec2=dict(type='dict'),
|
||||
chef=dict(type='dict'),
|
||||
puppet=dict(type='dict'),
|
||||
servicenow=dict(type='dict')
|
||||
state=dict(type="str", choices=["present", "absent"], default="present"),
|
||||
name=dict(
|
||||
type="str",
|
||||
),
|
||||
address=dict(
|
||||
type="str",
|
||||
),
|
||||
subscriptions=dict(type="list", elements="str"),
|
||||
safe_mode=dict(type="bool", default=False),
|
||||
redact=dict(type="list", elements="str"),
|
||||
socket=dict(type="dict"),
|
||||
keepalives=dict(type="bool", default=True),
|
||||
keepalive=dict(type="dict"),
|
||||
registration=dict(type="dict"),
|
||||
deregister=dict(type="bool"),
|
||||
deregistration=dict(type="dict"),
|
||||
ec2=dict(type="dict"),
|
||||
chef=dict(type="dict"),
|
||||
puppet=dict(type="dict"),
|
||||
servicenow=dict(type="dict"),
|
||||
),
|
||||
required_if=[
|
||||
['state', 'present', ['subscriptions']]
|
||||
]
|
||||
required_if=[["state", "present", ["subscriptions"]]],
|
||||
)
|
||||
|
||||
state = module.params['state']
|
||||
state = module.params["state"]
|
||||
path = "/etc/sensu/conf.d/client.json"
|
||||
|
||||
if state == 'absent':
|
||||
if state == "absent":
|
||||
if os.path.exists(path):
|
||||
if module.check_mode:
|
||||
msg = f'{path} would have been deleted'
|
||||
msg = f"{path} would have been deleted"
|
||||
module.exit_json(msg=msg, changed=True)
|
||||
else:
|
||||
try:
|
||||
os.remove(path)
|
||||
msg = f'{path} deleted successfully'
|
||||
msg = f"{path} deleted successfully"
|
||||
module.exit_json(msg=msg, changed=True)
|
||||
except OSError as e:
|
||||
msg = 'Exception when trying to delete {path}: {exception}'
|
||||
module.fail_json(
|
||||
msg=msg.format(path=path, exception=str(e)))
|
||||
msg = "Exception when trying to delete {path}: {exception}"
|
||||
module.fail_json(msg=msg.format(path=path, exception=str(e)))
|
||||
else:
|
||||
# Idempotency: it is okay if the file doesn't exist
|
||||
msg = f'{path} already does not exist'
|
||||
msg = f"{path} already does not exist"
|
||||
module.exit_json(msg=msg)
|
||||
|
||||
# Build client configuration from module arguments
|
||||
config = {'client': {}}
|
||||
args = ['name', 'address', 'subscriptions', 'safe_mode', 'redact',
|
||||
'socket', 'keepalives', 'keepalive', 'registration', 'deregister',
|
||||
'deregistration', 'ec2', 'chef', 'puppet', 'servicenow']
|
||||
config = {"client": {}}
|
||||
args = [
|
||||
"name",
|
||||
"address",
|
||||
"subscriptions",
|
||||
"safe_mode",
|
||||
"redact",
|
||||
"socket",
|
||||
"keepalives",
|
||||
"keepalive",
|
||||
"registration",
|
||||
"deregister",
|
||||
"deregistration",
|
||||
"ec2",
|
||||
"chef",
|
||||
"puppet",
|
||||
"servicenow",
|
||||
]
|
||||
|
||||
for arg in args:
|
||||
if arg in module.params and module.params[arg] is not None:
|
||||
config['client'][arg] = module.params[arg]
|
||||
config["client"][arg] = module.params[arg]
|
||||
|
||||
# Load the current config, if there is one, so we can compare
|
||||
current_config = None
|
||||
try:
|
||||
current_config = json.load(open(path, 'r'))
|
||||
current_config = json.load(open(path, "r"))
|
||||
except (IOError, ValueError):
|
||||
# File either doesn't exist or it is invalid JSON
|
||||
pass
|
||||
|
||||
if current_config is not None and current_config == config:
|
||||
# Config is the same, let's not change anything
|
||||
module.exit_json(msg='Client configuration is already up to date',
|
||||
config=config['client'],
|
||||
file=path)
|
||||
module.exit_json(msg="Client configuration is already up to date", config=config["client"], file=path)
|
||||
|
||||
# Validate that directory exists before trying to write to it
|
||||
if not module.check_mode and not os.path.exists(os.path.dirname(path)):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(path))
|
||||
except OSError as e:
|
||||
module.fail_json(msg=f'Unable to create {os.path.dirname(path)}: {e}')
|
||||
module.fail_json(msg=f"Unable to create {os.path.dirname(path)}: {e}")
|
||||
|
||||
if module.check_mode:
|
||||
module.exit_json(msg='Client configuration would have been updated',
|
||||
changed=True,
|
||||
config=config['client'],
|
||||
file=path)
|
||||
module.exit_json(
|
||||
msg="Client configuration would have been updated", changed=True, config=config["client"], file=path
|
||||
)
|
||||
|
||||
try:
|
||||
with open(path, 'w') as client:
|
||||
with open(path, "w") as client:
|
||||
client.write(json.dumps(config, indent=4))
|
||||
module.exit_json(msg='Client configuration updated',
|
||||
changed=True,
|
||||
config=config['client'],
|
||||
file=path)
|
||||
module.exit_json(msg="Client configuration updated", changed=True, config=config["client"], file=path)
|
||||
except (OSError, IOError) as e:
|
||||
module.fail_json(msg=f'Unable to write file {path}: {e}')
|
||||
module.fail_json(msg=f"Unable to write file {path}: {e}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue