ansible/library/fastd_key

32 lines
817 B
Plaintext
Raw Normal View History

#!/usr/bin/env python3
EXAMPLES = '''
# Generates a fastd key
- fastd_key: path=/etc/fastd/site/secret.conf
'''
from ansible.module_utils.basic import *
import os
if __name__ == '__main__':
module = AnsibleModule(
argument_spec={
'path': {'required': True, 'type': 'str'},
}
)
path = module.params['path']
changed = False
# file does not exist or is empty?
if not os.path.isfile(path) or os.stat(path).st_size == 0:
# create file with restrictive permissions
2018-01-14 18:24:51 +01:00
with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT, 0o600), 'w') as handle:
# generate fastd secret
2021-07-20 16:26:59 +02:00
secret = subprocess.check_output(["fastd", "--machine-readable", "--generate-key"]).strip().decode()
handle.write('secret "%s";\n' % secret)
changed = True
module.exit_json(changed=changed)