This code is a little bit crappy. Look into puppet concat module or file_line resource to do such things. Or create separate python file and import it.