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.

56 lines
1.1 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. DOCUMENTATION = '''
  4. ---
  5. module: container_exists
  6. author: Edoardo Putti
  7. short_description: Check existance for container
  8. description:
  9. - Check if a container with the given name exists
  10. options:
  11. name:
  12. required: true
  13. description:
  14. - Name of the container
  15. '''
  16. from ansible.module_utils.basic import *
  17. def main():
  18. module = AnsibleModule(
  19. argument_spec=dict(
  20. name= dict(
  21. required= True,
  22. type= 'str',
  23. ),
  24. ),
  25. supports_check_mode=True
  26. )
  27. try:
  28. import lxc
  29. except ImportError:
  30. self.module.fail_json(
  31. changed= False,
  32. msg= 'Error importing lxc, is python-lxc installed?',
  33. )
  34. container_name = module.params.get('name')
  35. result = {}
  36. result['name'] = container_name
  37. if container_name in lxc.list_containers():
  38. result['exists'] = True
  39. else:
  40. result['exists'] = False
  41. result['changed'] = True
  42. module.exit_json(**result)
  43. if __name__ == '__main__':
  44. main()