• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Stupid Simple Bash Templating

sempl

Circle CI

Stupid simple bash templating.

Uses environment variables, inherited or sourced from a file, to render templates using common bash utilities.

Given the following template (examples/config.yaml.tmpl):

user: $USER
password: ${password:-defaultpass}
files:
  ### begin
  # for f in $(ls); do
  - $f
  # done
  ### end

We could expect the following output when running sempl:

$ USER=myuser sempl -o examples/config.yaml.tmpl
user: myuser
password: defaultpass
files:
  - README.md
  - examples
  - sempl

Installation

curl -L -o sempl https://github.com/nextrevision/sempl/raw/master/sempl
chmod +x sempl

Requirements

  • bash
  • sed (GNU)
  • mktemp
  • grep (egrep)
  • openssl (if using encryption)

Usage

usage: ./sempl [args] template [outfile]

args:
-s [varsfile]   vars file (can be repeated)
-p [password]   decryption password
-k [passfile]   decryption password file
-v              verbose
-o              print template to stdout
-f              fail if a variable is unset with no default
-c              check that the template will render, but do not write the file
-h              help
--version       print version and exit
--update        update script to latest version

Loops

It is possible to use inline bash loops for more complex logic.

In order to designate where the loop should start, you must have in text ### begin followed at some point by ### end signaling the end of a loop. Any code you wish to execute must be preceded with a # and a space. Anything without a preceding # will be rendered as output by the template.

Caveats

  • A backslash must be doubly escaped (i.e. \\)
  • Redirection in command substitution does not work (i.e. $(cat blah 2>&1))
  • Quotes (single and double) must be closed or escaped

Examples

Template Expansion w/ Environment Vars

source examples/vars.sh
./sempl -v examples/config.json.tmpl

Template Expansion w/ Vars File

./sempl -v -s examples/vars.sh examples/config.json.tmpl

Template Expansion w/ Outfile

./sempl -v -s examples/vars.sh examples/config.json.tmpl \
  examples/outfile.json

Template Expansion w/ Decryption Key

./sempl -v -p mypassword -s examples/vars.sh.enc \
  examples/config.json.tmpl examples/outfile.json

Template Expansion w/ Decryption File

./sempl -v -k examples/passfile.txt -s examples/vars.sh.enc \
  examples/outfile.json examples/config.json.tmpl

Template Expansion w/ Multiple Varsfiles

Multiple vars files can be specified by repeating the -s flag. Vars files can either be passed either encrypted or decrypted.

./sempl -v -p mypassword -s examples/vars.sh.enc \
  -s examples/vars.sh -s examples/vars.sh \
  examples/config.json.tmpl examples/outfile.json

Looping over a list of files

Given the template test.txt.tmpl below:

This is a text file. Siblings include:
### begin
# for i in $(ls); do
# if [[ $i == "sibling1.txt" ]]; then
  $i (favorite)
# else
  $i
# fi
# done
### end

Could be rendered as:

This is a text file. Siblings include:
  test.txt.tmpl
  sibling1.txt (favorite)
  sibling2.txt

Encryption

crypttool is a very simple wrapper around the openssl command that can encrypt, decrypt, or edit a file. sempl can take an encrypted file and decrypt it at runtime with a password/passfile specified as an argument. This allows storing of secrets in variable files and decryption at the point of rendering a template file.

Encrypting a Varsfile

./crypttool -p mypassword encrypt examples/vars.sh

Decrypting a Varsfile

./crypttool -p mypassword decrypt examples/vars.sh.enc

Editing an Encrypted Varsfile

./crypttool -p mypassword edit examples/vars.sh.enc

More Repositories

1

ansible-swarm-playbook

Playbook for creating/managing a Docker Swarm cluster
Python
167
star
2

terraform-rancher-ha-example

Terraform files for deploying a Rancher HA cluster in AWS
HCL
61
star
3

rancher-vagrant

Extensible and automated local Rancher environments using Vagrant
Ruby
18
star
4

ansible-docker-registry

Ansible role for installing docker registry
Ruby
14
star
5

fauxcli

Mocks a command line client from a given YAML file
Go
14
star
6

docker-freeradius

FreeRadius Docker Files
8
star
7

puppet-automysqlbackup

Puppet module for deploying automysqlbackup
Ruby
7
star
8

env2ini

Creates and updates INI files from environment variables
Go
6
star
9

docker-registry-ui

A simple web UI for browsing private docker registries
Python
6
star
10

consul-rancher

Docker and Rancher files for easily deploying Consul, Consul Agent, and Registror
Shell
5
star
11

kubernetes-docset-generator

Scripts for generating the Kubernetes Dash docset
Shell
5
star
12

vagrant-rancher

Vagrant plugin to install a Rancher server and agents on all Vagrant guests.
Ruby
4
star
13

brackets-puppet-syntax

Puppet syntax highlighter for the Brackets editor
JavaScript
3
star
14

go-runscope

Go Library for interacting with the Runscope API
Go
2
star
15

sapt

s3 apt repo management utility
Go
2
star
16

ops-packer

Packer template and files for creating a Vagrant box ready to go with OpenSwitch
Shell
2
star
17

gotmpl

CLI tool for rendering templates that supports environment variables, variable files, and inline encryption
Go
2
star
18

jjb-docset-generator

Script to generate a dash docset for Jenkins Job Builder
Shell
2
star
19

electron-react-reflux-photon-example

Simple app example that incorporates electron, react, react-router, reflux, and photon.
CSS
1
star
20

ops-playground

A set of examples for playing with OpenSwitch
1
star
21

puppet-flowtools

Puppet module to install flow-tools and manage flow-capture
Ruby
1
star
22

rsc

Runscope Command Line Client
Go
1
star
23

jenkins-gitflow-example

Reference for a gitflow job workflow
Shell
1
star
24

hydromonitor

A web UI for managing and visualizing your Tilt devices
CSS
1
star