Guest User

Untitled

a guest
Oct 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. DOCUMENTATION = '''
  4. ---
  5. module: remote_copy
  6. short_description: Remote recursive copy
  7. '''
  8.  
  9. from ansible.module_utils.basic import AnsibleModule
  10.  
  11. import shutil
  12. import os
  13.  
  14. def copyfile(src, dst, changed=False, result=[]):
  15. if not os.path.exists(dst) or os.stat(src).st_mtime - os.stat(dst).st_mtime > 1:
  16. shutil.copy2(src, dst)
  17. result.append(dst)
  18. changed = True
  19. return changed, result
  20.  
  21. def copytree(src, dst, changed=False, result=[]):
  22. if os.path.isdir(src):
  23. if not os.path.exists(dst):
  24. os.makedirs(dst)
  25. result.append(dst)
  26. changed = True
  27. for item in os.listdir(src):
  28. s = os.path.join(src, item)
  29. d = os.path.join(dst, item)
  30. if os.path.isdir(s):
  31. changed, result = copytree(s, d, changed, result)
  32. else:
  33. changed, result = copyfile(s, d, changed, result)
  34. else:
  35. changed, result = copyfile(src, dst, changed, result)
  36. return changed, result
  37.  
  38. def main():
  39. module = AnsibleModule(
  40. argument_spec=dict(
  41. dest=dict(required=True, type='str'),
  42. src=dict(required=True, type='str')
  43. ),
  44. supports_check_mode=False,
  45. )
  46.  
  47. dest = module.params['dest']
  48. src = module.params['src']
  49.  
  50. changed, result = copytree(src, dest)
  51.  
  52. if changed:
  53. module.exit_json(changed=True, msg="files copied successfully", meta=result)
  54. else:
  55. module.exit_json(changed=False, msg="no files changed", meta=result)
  56.  
  57. if __name__ == '__main__':
  58. main()
Add Comment
Please, Sign In to add comment