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.

89 lines
2.1 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_content
  8. author: Edoardo Putti
  9. short_description: Return the content of a file
  10. description:
  11. - Retrieve the content for the given path 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 read_file_in_container(args):
  23. (path, module) = args
  24. try:
  25. with open(path, 'r') as lines:
  26. module.exit_json(
  27. path = path,
  28. text = lines.read().strip('\n'),
  29. )
  30. except IOError, e:
  31. module.exit_json(
  32. msg = e,
  33. path = path,
  34. )
  35. def main():
  36. module = AnsibleModule(
  37. argument_spec = dict(
  38. name = dict(
  39. required = True,
  40. type = 'str',
  41. ),
  42. path = dict(
  43. required = True,
  44. type = 'str',
  45. ),
  46. ),
  47. )
  48. try:
  49. import lxc
  50. except ImportError:
  51. module.fail_json(
  52. changed = False,
  53. msg = 'Error importing lxc, is python-lxc installed?',
  54. )
  55. container_name = module.params.get('name')
  56. file_path = module.params.get('path')
  57. result = {}
  58. result['name'] = container_name
  59. result['path'] = file_path
  60. if container_name in lxc.list_containers():
  61. container = lxc.Container(container_name)
  62. file_exists = container.attach_wait(
  63. read_file_in_container,
  64. (file_path, module),
  65. env_policy = lxc.LXC_ATTACH_CLEAR_ENV,
  66. )
  67. else:
  68. module.fail_json(
  69. available_container = lxc.list_containers(),
  70. msg = 'Target container does not exists',
  71. name = container_name,
  72. path = file_path,
  73. )
  74. if __name__ == '__main__':
  75. main()