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.

90 lines
2.0 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from __future__ import print_function
  4. from ansible.module_utils.basic import *
  5. DOCUMENTATION = '''
  6. ---
  7. module: container_file_exists
  8. author: Edoardo Putti
  9. short_description: Return whether a file is present in the container
  10. description:
  11. - Check if the given path exists on the given container
  12. options:
  13. name:
  14. required: true
  15. description:
  16. - Name of the container
  17. path:
  18. required: true
  19. description:
  20. - path of the file to check
  21. '''
  22. def check_file_in_container(path):
  23. import os
  24. import json
  25. result = dict(
  26. exists = False,
  27. failed = False,
  28. path = path,
  29. )
  30. if os.path.exists(path):
  31. result['exists'] = True
  32. else:
  33. result['exists'] = True
  34. print(json.dumps(result))
  35. return 0
  36. def main():
  37. module = AnsibleModule(
  38. argument_spec = dict(
  39. name = dict(
  40. required = True,
  41. type = 'str',
  42. ),
  43. path = dict(
  44. required = True,
  45. type = 'str',
  46. ),
  47. ),
  48. )
  49. try:
  50. import lxc
  51. except ImportError:
  52. module.fail_json(
  53. changed = False,
  54. failed = True,
  55. msg = 'Error importing lxc, is python-lxc installed?',
  56. )
  57. container_name = module.params.get('name')
  58. file_path = module.params.get('path')
  59. result = {}
  60. result['name'] = container_name
  61. result['path'] = file_path
  62. if container_name in lxc.list_containers():
  63. container = lxc.Container(container_name)
  64. file_exists = container.attach_wait(
  65. check_file_in_container,
  66. file_path,
  67. env_policy = lxc.LXC_ATTACH_CLEAR_ENV,
  68. )
  69. else:
  70. result['changed'] = False
  71. result['failed'] = True
  72. result['msg'] = "Target container does not exists"
  73. module.exit_json(**result)
  74. if __name__ == '__main__':
  75. main()