diff --git a/library/container_certificate_exists.py b/library/container_certificate_exists.py deleted file mode 100644 index 23a5069..0000000 --- a/library/container_certificate_exists.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -DOCUMENTATION = ''' ---- -module: container_certificate_exists -author: Edoardo Putti -short_description: Return wheter a certificate is present in the container -description: - - Look for the /etc/ssh/ssh_host_ed25519_key-cert.pub file -options: - name: - required: true - description: - - Name of the container -''' - -from ansible.module_utils.basic import * - - -def main(): - module = AnsibleModule( - argument_spec = dict( - name = dict( - required = True, - type = 'str', - ), - ), - supports_check_mode=True - ) - - try: - import lxc - except ImportError: - self.module.fail_json( - changed= False, - msg= 'Error importing lxc, is python-lxc installed?', - ) - - container_name = module.params.get('name') - - result = {} - result['name'] = container_name - - if container_name in lxc.list_containers(): - - container_certificate = container.attach_wait( - lxc.attach_run_command, - ['cat', '/etc/ssh/ssh_host_ed25519_key-cert.pub',], - ) - result['changed'] = True - result['msg'] = container_certificate - - else: - result['changed'] = False - result['failure'] = True - result['msg'] = "Target container does not exists" - - module.exit_json(**result) - -if __name__ == '__main__': - main() diff --git a/library/container_file_exists.py b/library/container_file_exists.py new file mode 100644 index 0000000..0238c6c --- /dev/null +++ b/library/container_file_exists.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import print_function +from ansible.module_utils.basic import * + +DOCUMENTATION = ''' +--- +module: container_file_exists +author: Edoardo Putti +short_description: Return whether a file is present in the container +description: + - Check if the given path exists on the given container +options: + name: + required: true + description: + - Name of the container + path: + required: true + description: + - path of the file to check +''' + +def read_file_in_container(path): + with open(path, 'r') as lines: + print(lines.read()) + return 0 + +def write_file_in_container(path): + with open(path, 'w') as out: + out.write("pippo") + return 0 + +def check_file_in_container(path): + import os + import json + result = { + changed: True, + exists: False, + failure: False, + path: path, + } + if os.path.exists(path): + result.exists = True + else: + result.failure = True + + print(json.dump(result)) + return 0 + +def main(): + module = AnsibleModule( + argument_spec = dict( + name = dict( + required = True, + type = 'str', + ), + path = dict( + required = True, + type = 'str', + ), + ), + ) + + try: + import lxc + + except ImportError: + module.fail_json( + changed = False, + failed = True, + msg = 'Error importing lxc, is python-lxc installed?', + ) + + container_name = module.params.get('name') + file_path = module.params.get('path') + + result = {} + result['name'] = container_name + result['path'] = file_path + + if container_name in lxc.list_containers(): + + container = lxc.Container(container_name) + + file_exists = container.attach_wait( + check_file_in_container, + file_path, + env_policy = lxc.LXC_ATTACH_CLEAR_ENV, + ) + + else: + result['changed'] = False + result['failed'] = True + result['msg'] = "Target container does not exists" + + module.exit_json(**result) + +if __name__ == '__main__': + main()