import yaml import collections def add_task(): return None def add_vars(): return None def add_handles(): return None def add_project(name=None,gather_facts=False,hosts=None,remote_user=None, tasks=None,handlers=None,varstrings=None): aproject = collections.OrderedDict() aproject['name']=name aproject['hosts'] = hosts aproject['gather_facts'] = gather_facts aproject['remote_user'] = remote_user aproject['tasks'] = tasks aproject['handlers'] = handlers aproject['vars'] = varstrings for k,v in aproject.items(): if v is None: aproject.pop(k) print aproject bprojects={} for key in aproject: print {key: aproject[key]} print bprojects f = open('test.yaml', 'w') result = yaml.dump(aproject, f, default_flow_style=False, allow_unicode=True) print result if __name__=='__main__': tasks=add_task() handlers=add_handles() varstrings=add_vars() add_project(name='Silenthand Olleander',gather_facts=False,hosts=None,remote_user=None, tasks=tasks,handlers=handlers,varstrings=varstrings)