Skip to content

Instantly share code, notes, and snippets.

@jeremypruitt
Created November 11, 2015 09:57
Show Gist options
  • Save jeremypruitt/d85b93359cbe4db6815b to your computer and use it in GitHub Desktop.
Save jeremypruitt/d85b93359cbe4db6815b to your computer and use it in GitHub Desktop.
Render Go template to create an Ansible playbook file
package main
import (
"os"
"text/template"
)
func check(e error) {
if e != nil { panic(e) }
}
const patch_playbook_template = `- name: "Apply patch | {{ .PatchName }}"
hosts: "all"
user: "root"
gather_facts: "{{ .GatherFacts }}"
pre_tasks:
- name: "Ensure /etc/ccp/patch-log exists"
file:
state: "directory"
path: "/etc/ccp/patch-log"
tasks:
- name: "__CHANGE_ME__"
post_tasks:
- name: "Update the patch log"
template:
src: "templates/patch.log.j2"
dest: "/etc/ccp/patch-log/patch-{{ .PatchLogFilename }}.log"
func main() {
f, err := os.Create("/tmp/foo.txt")
check(err)
t := template.Must(template.New("patch_playbook").Parse(patch_playbook_template))
patch_data := map[string]interface{}{
"PatchName": "Fix NFS timeout issue",
"PatchLogFilename": "fix-nfs-timeout-issue",
"GatherFacts": "yes",
}
err = t.Execute(f, patch_data)
check(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment