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.

78 lines
1.8 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(args):
  23. (path, module) = args
  24. import os
  25. module.exit_json(
  26. exists = os.path.exists(path),
  27. path = path,
  28. )
  29. def main():
  30. module = AnsibleModule(
  31. argument_spec = dict(
  32. name = dict(
  33. required = True,
  34. type = 'str',
  35. ),
  36. path = dict(
  37. required = True,
  38. type = 'str',
  39. ),
  40. ),
  41. )
  42. try:
  43. import lxc
  44. except ImportError:
  45. module.fail_json(
  46. changed = False,
  47. failed = True,
  48. msg = 'Error importing lxc, is python-lxc installed?',
  49. )
  50. container_name = module.params.get('name')
  51. file_path = module.params.get('path')
  52. if container_name in lxc.list_containers():
  53. container = lxc.Container(container_name)
  54. file_exists = container.attach_wait(
  55. check_file_in_container,
  56. (file_path, module),
  57. env_policy = lxc.LXC_ATTACH_CLEAR_ENV,
  58. )
  59. else:
  60. module.fail_json(
  61. changed = False,
  62. msg = 'Target container does not exists',
  63. )
  64. if __name__ == '__main__':
  65. main()