I've a python script that interacts with a website's api and returns a number of variables. These variables in turn make up parts of an ansible playbook.
Currently I'm just creating the playbooks from the script like this
file = open(f'playbook_dir/{var1}-{var2}.yml', 'w')
file.write("######################################\n")
file.write("\n")
file.write(f"---\n")
file.write(f"- name: Update site with new info\n")
file.write(f" hosts: {device_1}\n")
file.write(f" gather_facts: no\n")
file.write(f" connection: local\n")
etc, which does give me the desired output. However it does make the code more prone to errors, and because the playbooks created are fairly big (more than the example) makes the code quite ugly.
Is there a better way to generate new files from a template than this?