<s> DOCUMENTATION = '''<STR_LIT>''' <EOL> EXAMPLES = '''<STR_LIT>''' <EOL> import base64 <EOL> import hashlib <EOL> import os <EOL> import zlib <EOL> def copy_from_host ( module ) : <EOL> compress = module . params . get ( '<STR_LIT>' ) <EOL> src = module . params . get ( '<STR_LIT:src>' ) <EOL> if not os . path . exists ( src ) : <EOL> module . fail_json ( msg = "<STR_LIT>" . format ( src ) ) <EOL> if not os . access ( src , os . R_OK ) : <EOL> module . fail_json ( msg = "<STR_LIT>" . format ( src ) ) <EOL> mode = oct ( os . stat ( src ) . st_mode & <NUM_LIT> ) <EOL> with open ( src , '<STR_LIT:rb>' ) as f : <EOL> raw_data = f . read ( ) <EOL> sha1 = hashlib . sha1 ( raw_data ) . hexdigest ( ) <EOL> data = zlib . compress ( raw_data ) if compress else raw_data <EOL> module . exit_json ( content = base64 . b64encode ( data ) , sha1 = sha1 , mode = mode , <EOL> source = src ) <EOL> def copy_to_host ( module ) : <EOL> compress = module . params . get ( '<STR_LIT>' ) <EOL> dest = module . params . get ( '<STR_LIT>' ) <EOL> mode = int ( module . params . get ( '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> sha1 = module . params . get ( '<STR_LIT>' ) <EOL> src = module . params . get ( '<STR_LIT:src>' ) <EOL> data = base64 . b64decode ( src ) <EOL> raw_data = zlib . decompress ( data ) if compress else data <EOL> if sha1 : <EOL> if os . path . exists ( dest ) : <EOL> if os . access ( dest , os . R_OK ) : <EOL> with open ( dest , '<STR_LIT:rb>' ) as f : <EOL> if hashlib . sha1 ( f . read ( ) ) . hexdigest ( ) == sha1 : <EOL> module . exit_json ( changed = False ) <EOL> else : <EOL> module . exit_json ( failed = True , changed = False , <EOL> msg = '<STR_LIT>' . format ( dest ) ) <EOL> if sha1 != hashlib . sha1 ( raw_data ) . hexdigest ( ) : <EOL> module . exit_json ( failed = True , changed = False , <EOL> msg = '<STR_LIT>' ) <EOL> with os . fdopen ( os . open ( dest , os . O_WRONLY | os . O_CREAT , mode ) , '<STR_LIT:wb>' ) as f : <EOL> f . write ( raw_data ) <EOL> module . exit_json ( changed = True ) <EOL> def main ( ) : <EOL> argument_spec = dict ( <EOL> compress = dict ( default = True , type = '<STR_LIT:bool>' ) , <EOL> dest = dict ( type = '<STR_LIT:str>' ) , <EOL> mode = dict ( default = '<STR_LIT>' , type = '<STR_LIT:str>' ) , <EOL> sha1 = dict ( default = None , type = '<STR_LIT:str>' ) , <EOL> src = dict ( required = True , type = '<STR_LIT:str>' ) <EOL> ) <EOL> module = AnsibleModule ( argument_spec ) <EOL> dest = module . params . get ( '<STR_LIT>' ) <EOL> try : <EOL> if dest : <EOL> copy_to_host ( module ) <EOL> else : <EOL> copy_from_host ( module ) <EOL> except Exception as e : <EOL> module . exit_json ( failed = True , changed = True , msg = repr ( e ) ) <EOL> from ansible . module_utils . basic import * <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
