0

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?

CEamonn
  • 853
  • 14
  • 37
  • 1
    Sure. For example, you could use the same Jinja templating engine that Ansible uses. It sounds like you've chosen a fairly baroque solution to something, but without knowing either the original problem or what your solution looks like, it's hard to tell. – larsks Jan 04 '19 at 16:15
  • This may have been asked before. Are you looking to accomplish something similar to this https://stackoverflow.com/a/6385805/7548672 –  Jan 04 '19 at 16:16

0 Answers0