Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1017 B

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. DOCUMENTATION = '''
  4. ---
  5. module: gen_passwd
  6. author: Daniele Baracchi
  7. short_description: Generate a random password
  8. description:
  9. - Generate a random password
  10. options:
  11. length:
  12. required: true
  13. description:
  14. - Length of the generated password
  15. '''
  16. from random import SystemRandom
  17. import string
  18. from ansible.module_utils.basic import *
  19. def main():
  20. module = AnsibleModule(
  21. argument_spec=dict(
  22. length=dict(required=True, type='int')
  23. ),
  24. supports_check_mode=True
  25. )
  26. length = module.params.get('length')
  27. result = {}
  28. result['length'] = length
  29. rng = SystemRandom()
  30. valid_chars = string.ascii_uppercase + string.ascii_lowercase + \
  31. string.digits
  32. passwd = [rng.choice(valid_chars) for _ in xrange(length)]
  33. result['passwd'] = ''.join(passwd)
  34. result['changed'] = True
  35. module.exit_json(**result)
  36. if __name__ == '__main__':
  37. main()