In work applications, operational automation is based on standardization.
and standardization of the work, is difficult, in the company's relevant departments of cooperation.
Then, after having standardized, the corresponding deployment script, it is better to write.
Contribute to a similar environment can be used in the east.
Of course, can write better, just time is not enough.
#!/usr/bin/python#-*-coding:utf-8-*-ImportSys,os#define a list of all files that need to be replaced by IP, and be sure to use the environment, otherwise it will not work. This script is more versatile if all environments are well-definedrepip_file_list= ["D1_file", "D2_file", "D3_file", "D4_file", "D5_file"]#IP -Integrated Dictionary of the template's IP and new environment for easy replacement and better encapsulationip_dict= {"old_c_ip":"1.1.1.1", "new_c_ip":"2.2.2.2", "old_f_ip":"1.1.1.3", "new_f_ip":"2.2.2.3", "old_k_ip":"1.1.1.4", "new_k_ip":"2.2.2.4", "old_o_ip":"1.1.1.5", "new_o_ip":"2.2.2.5", "old_s_ip":"1.1.1.6", "new_s_ip":"2.2.2.6"}#Replace the contents of the specified file with a function that replaces multiple environment parameters in parallel, so that a script can be used, multiple generic. defRepip_func (File_path, ip_dict): F= Open (File_path,'r+') All_the_lines=f.readlines () f.seek (0) f.truncate () forLineinchAll_the_lines:line= Line.replace (ip_dict['old_c_ip'],ip_dict['new_c_ip']) line= Line.replace (ip_dict['old_t_ip'],ip_dict['new_t_ip']) line= Line.replace (ip_dict['old_s_ip'],ip_dict['new_s_ip']) line= Line.replace (ip_dict['old_f_ip'],ip_dict['new_f_ip']) line= Line.replace (ip_dict['old_k_ip'],ip_dict['new_k_ip']) F.write (line) f.close ()Print "%s has replace IP%s"%(File_path, ip_dict)defMain (): forFile_pathinchRepip_file_list:repip_func (File_path, ip_dict)if __name__=="__main__": Main ()
Python bulk file specifies string substitution