TL;DR: Checkout the template engine at github.
When working with pods in Kubernetes I had the need to define environment variables for the pod from a file. This is currently not supported in the currently released version of Kubernetes (1.1). What I wanted to do was to have a template file, for example `pod-template.yaml`, that looked like this:
kind: "ReplicationController"
apiVersion: "v1"
metadata:
name: "my-pod-{{VERSION}}"
labels:
name: "my-pod"
version: "{{VERSION}}"
spec:
replicas: 2
selector:
name: "my-pod"
version: "{{VERSION}}"
template:
metadata:
labels:
name: "my-pod"
version: "{{VERSION}}"
spec:
containers:
- name: "my-pod"
image: "gcr.io/my-project/my-pod:{{VERSION}}"
env:
- name: "TIMEOUT"
value: {{TIMEOUT}}
- name: "URL"
value: "{{URL}}"
ports:
- containerPort: {{API_PORT}}
name: "api"
protocol: "TCP"
- containerPort: {{ADMIN_PORT}}
name: "admin"
protocol: "TCP"
and be able to replace things inside the double brackets (placeholders) with values defined externally to the `pod-template.yaml`. This is of course nothing new and there’s a gazillion of various templating engines out there for various programming languages. But after quite a lot of googling and looking through stackoverflow I couldn’t find anything that worked the way I wanted to in bash.
What made this particular use case a bit tricky was that some of the placeholder values were defined in a file while others had to be passed as parameters to the template engine on usage. For example the value of the `{{VERSION}}` placeholder is generated by our continuous integration server while all others (`{{TIMEOUT}}`, `{{URL}}` etc) are defined in an (encrypted) configuration file checked into our code repository. So I needed a template engine that could handle both of these cases at the same time. The closest thing I found was the bash-templater projected created by Sébastien Lavoie which allowed me to do like this:
$ VERSION=1.2.3 TIMEOUT=1000 URL="http://www.google.com" API_PORT=8080 ADMIN_PORT=8081 \
./templater.sh pod-template.yaml
I.e. it takes environment variables and replaces the placeholders defined in the template file with these values. The outcome looks like this:
kind: "ReplicationController"
apiVersion: "v1"
metadata:
name: "my-pod-1.2.3"
labels:
name: "my-pod"
version: "1.2.3"
spec:
replicas: 2
selector:
name: "my-pod"
version: "1.2.3"
template:
metadata:
labels:
name: "my-pod"
version: "1.2.3"
spec:
containers:
- name: "my-pod"
image: "gcr.io/my-project/my-pod:1.2.3"
env:
- name: "TIMEOUT"
value: 1000
- name: "URL"
value: "http://www.google.se"
ports:
- containerPort: 8080
name: "api"
protocol: "TCP"
- containerPort: 8081
name: "admin"
protocol: "TCP"
The problem (again) was that our configuration was defined in a file (`config.properties`) that looked something like this:
# The timeout in milliseconds
TIMEOUT=1000
# The URL
URL="http://www.google.com"
# External API port
API_PORT=8080
# Internal admin port
ADMIN_PORT=8081
So I ended up forking `bash-templater` to my github repository and added support for passing in values as parameters as well as reading values from a file. Usage:
$ VERSION=1.2.3 ./templater.sh pod-template.yaml -f config.properties > pod.yaml
Volia, all requirements fulfilled! The result of the transformation is stored in the `pod.yaml` file.
I also added some other tweaks, for example suppression of warning messages (that will otherwise be included in the resulting file and mess up the outcome) if you define entries in `config.properties` that are not found as placeholders in template. Just add the `-s` (silent) argument to ignore these warnings:
$ VERSION=1.2.3 ./templater.sh pod-template.yaml -f config.properties -s > pod.yaml
I hope that this will come to use for someone else as well.
31 thoughts on “Simple templating engine in Bash”
Why not just use `envsubst` ?
I’m not familiar with envsubst, could you please elaborate?
A simple solution: http://pempek.net/articles/2013/07/08/bash-sh-as-template-engine
export var1=”test”
envsubst generated.yaml
$var1 in template.yaml, and will substituted as “test” in generated.yaml
But, i cannot find a way to escape if using envsubst.
e.g. i want to keep ${var_to_keep} not substituted by envsubst
Johan, hello. Once again, thanks.
I’ve run into an issue where supplying a template with no variables (ie: {{}}) throws a warning and an error:
Warning: No variable was found in services/portal.yml, syntax is {{VAR}}
sed: 1: “services/portal.yml”: unterminated substitute in regular expression
I meant to open an issue in the repo but they’re disabled. This is just a heads up. I’ll try to submit a PR asap.
Hi Luis,
Thanks for your comment. I actually didn’t know that issues were disabled, I’ll fix this. Also a PR would be great 🙂
Regards,
/Johan
Thanks for your article! Are you going to submit your work upstream to Sébastien’s repo?
I already have provided a PR, see https://github.com/lavoiesl/bash-templater/pull/1, but it has not been accepted yet.
Ah, I missed that. Cool!
I drew some inspiration from your and lavoiesl’s work: https://github.com/relaxdiego/renderest
Just wanted to say that j2cli does the same, as uses jinja syntax for templating.
You can also source properties files as well:
https://github.com/kolypto/j2cli
Thanks for the tip
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Simple templating engine in Bash – Coding all the things
[url=http://www.g23tiz4zm081t1a4t1xc5t3nzy41009fs.org/]uphexjbnk[/url]
aphexjbnk
phexjbnk http://www.g23tiz4zm081t1a4t1xc5t3nzy41009fs.org/
hotpantz.kinnikubaka.com
Furniture Parts
Wheelie Bin Axle
sidel blow molding
bottle blow molding machine
Stamping Parts
affordable gucci tennis shoes cheap YSL ENVELOPE MEDIUM BAG
Garment Logistic Racking Parts
automatic bottle blowing machine
pet mould
cheap gucci tops cheap YSL ENVELOPE SMALL BAG
affordable gucci sweatpants cheap YSL ENVELOPE MEDIUM BAG
spare parts
cheap gucci t shirt cheap YSL ENVELOPE MEDIUM BAG
Stamping
affordable gucci tracksuit cheap YSL ENVELOPE MEDIUM BAG
55% Bronze with 5% MoS2 filled PTFE Tube
Orange Life Jacket
cheap ysl handbags green Saint Laurent Small Monogram East West Wallet In Grained Leather Black-HOT SALE
cheap ysl mens boots Saint Laurent Small Envelope Trifold Wallet In Grained Matelasse Leather Black Gold-HOT SALE
cheap 2nd hand ysl bag Saint Laurent Small Tiny Monogram East West Wallet In Crocodile Embossed Leather Black-HOT SALE
Cabelas Life Jackets
cheap ysl travel bag Saint Laurent Small Envelope Trifold Wallet In Mixed Grained Matelasse Leather Black White-HOT SALE
http://www.baronleba.pl
Nrs Life Jacket
60% Bronze Filled PTFE Rod Bar
Anaconda Life Jackets
40% Bronze Filled PTFE Rod Bar
60% Bronze Powder Filled PTFE Moulded sheet
40% Bronze Powder Filled PTFE Moulded sheet
Float Vest
cheap ysl summer bag Saint Laurent Small Envelope Trifold Wallet In Mixed Grained Matelasse Leather Black Gold-HOT SALE
China Purses Handbags Factory
cheap where are yeezys made Bogs, Botte d'hiver imperméable CLASSIC PEGASUS, filles
Oil Resistant asbestos Sheet
Synthetic Fiber Rubber Gasket
22 Inch Desk Half-Moon Nail Lamp for Beauty Salon Lighting
Fur Muffler Scarf
cheap why is yeezy so cheap Bogs, Botte de pluie PATCH ANKLE, noir, femmes
Substation
Professional Sun Mini Desk Gel Uv Led Table Nail Lamp
Cat Nail New Nail Light USB Lamp Nail Baking Lamp
affordable why is adidas still selling yeezy Bogs, Botte de pluie SKIPPER SOLID, noir, bébés
haedang.vn
cheap adidas emirates mall Bogs, Botte imperméable AMANDA II PLUSH LACE, noir, femmes
Wholesale Extra Large Handbags
Handbag Sets
affordable where is adidas produced Bogs, Botte imperméable CLASSIC UNICORNS noire filles
bilu.com.pl
affordable adidas outlet store in san marcos tx Louis Vuitton Pre-Owned x Comme Des Garçons 2014 Monogram Halls tote bag WOMEN
Diesel Fuel Pump
Main Hydraulic Pump
Luxury Small Portable Luminous USB Charging Cone Desk lamp
Wood Splitter Valve
cheap ye clothing Louis Vuitton Pre-Owned 2004 Vavin PM tote bag WOMEN
affordable adidas outlet store grapevine Louis Vuitton Pre-Owned 2004 Pochette Florentine belt bag WOMEN
Cute Night Light Clip Reading Study Light Adorable Desk Lamp
Triaxial Accelerometer
Study Foldable Rotatable Desk Lamp Touch Switch Table Lamp
cheap adidas outlet store grapevine grapevine tx Louis Vuitton Pre-Owned 2012 Trotteur Beaubourg shoulder bag WOMEN
Ornaments Dimmable USB Charge Mini Phonograph Table Lamp
Marine Hydraulic Pump
Luxury USB Rechargeable Gold Home Hotel Decor Table Lamp
affordable samba online Louis Vuitton Pre-Owned 1998 mini Sarria handbag WOMEN
http://www.czarna4.pl
Red Ripper
reps tbkicks TB-NIKE DUNK LOW PLUM (2020) CU1726-500
Moulding Parts
Ladder Mount
Rear Fog Lamp
Canoe Anchor
Bike Rear Light
reps tkick TB-Travis Scott x Nike SB Dunk Low CT5053-001
Roof Anchor
Injected Plastics
cheap tkicks ru TB-Nike SB Dunk Low Summit White Wolf Grey AR0778-110
Rear Fog Lights
Kayak Anchor
rep tbkicks ru TB-Grateful Dead x Nike SB Dunk Low Orange Bear CJ5378-800
rep tbkicks.ru TB-Grateful Dead x Nike SB Dunk Low Green Bear CJ5378-300
Your article helped me a lot, is there any more related content? Thanks!
Brass Bull Craft
Natural Amethyst Jade Roller And Gua Sha Facial Massager Set
rep louis vuitton who is REP-PRADA Leather tote bag
New Hot Iron Selling Ceramic Automatic Hair Styling Curlers
High Quality Facial Massager Natural Gua Sha Green Jade Roller
affordable louis vuitton is known for REP-PRADA Leather tote bag
Jade Facial Massager Heart Shaped Massage Face Gua Sha
yujyakai.kir.jp
Metal Crafts
Soccer Trophy
cheap louis vuitton founding date REP-Prada Leather shoulder bag
2024 Hot Selling Rose Quartz Vibrating 5 in 1 Electric Jade Roller
rep louis vuitton bags originated from which country REP-PRADA Leather tote bag
cheap home lv REP-Prada Cross leather bag
Car Fan Heater 12v
Car Window Heater
cheap lv artsy LV Bag 19HT0083
cheap lv artsy LV Bags 205fy0002
Processing Product
cheap cheap lv outlet LV Bags 19B5790066
Puresculpt Body Ice
PEEK
ABS Sheet
PTFE ROD
Skin Rejuvenation Instrument
Rf Microneedling Machine At Home
http://www.den100.co.jp
Natural Hair Removal
ABS
Fat Freeze Membranes
cheap lv artsy bag LV Bag 20DJ11055
cheap lv artsy bag LV Bags 19B570015
organic cbd gummies present a expedient and enjoyable disposition to acquaintance the effects of this compound. These gummies check in in various flavors, potencies, and formulations, providing users with controlled dosing and long-lasting effects. Divers consumers cherish them for the purpose relaxation’, anguish relief. In any way, it’s vital to digest them responsibly, as effects may take longer to kick in compared to smoking or vaping. Usually voucher dosage guidelines and effect compliance with nearby laws before purchasing or consuming.
China Sheet Metal Manufacturing
crep protect ingredients adidas FYW 98 “Signal Coral” sneakers
fv5120 104 adidas Originals Superstar “Leather Grid” sneakers
http://www.suplimedics.com
fn9137 141 adidas Bermuda sneakers
Pipeline Metal Detectors
Aluminum X Ray Inspection
K2 Torsion Bar Adjustable Arm
ig6670 adidas AE1 Low “Mural” sneakers
K1 Torsion Bar Adjustable Arm
N3 Adjustable Push Rod
N2 Adjustable Push Rod
Dynamic Checkweigher
hm3711 144 adidas ZX 8000 Superstar Shoes sneakers
Metal Enclosure Fabrication
K3 Torsion Bar Adjustable Arm
Mica Sheets
Rubber Sheets
PVC Plastic Coated Chain Link Fence
Mineral Fiber Sheets
Cork Sheets
U Shape Nail
cheap tkick TB-New Balance x Palace 580 “Shaded Spruce” sneakers
Soft Fibration PTFE Sealing Sheet
cheap tbkicks TB-New Balance 610V1 “Green Punch” sneakers
http://www.remasmedia.com
rep tbkicks.ru TB-New Balance 1700 “Kith Toronto Rococco Red” sneakers
Perforated Hex Grill Mesh Sheet
Rebar Ties Wire
rep tkicks ru TB-New Balance The 580 panelled sneakers
Pig Baby Mesh Fence
reps tbkicks ru TB-New Balance 860v2 “Aime Leon Dore Yellow” sneakers
CHINA NATURAL RUBBER SHEET SUPPLIER
WHITE FOOD QUALITY RUBBER SHEETING
CHINA COMMERCIAL NEOPRENE INSERTION SHEETING – 1 PLY
Sodium Carboxymethyl Cellulose Uses
Carboxymethyl Cellulose Sodium Salt
NATURAL RUBBER SHEET
cheap lv artsy bag LV Shoes 2103SH0066
cheap cheap lv outlet LV Shoes 2104SH0069
lacea.upsa.edu.bo
Hydroxy Methyl Cellulose
cheap lv artsy LV Shoes 20SH010700
COMMERCIAL NEOPRENE INSERTION SHEETING – 1 PLY
Hydroxy Propyl Methyl
Oil Thickener
cheap lv artsy bag LV Shoes 2011SH0001
cheap cheap lv outlet LV Shoes 2103SH0067
cheap good kicks ru Saucony S708081 RUST
cheap goodkick.ru shoes Saucony Shadow 5000 “Aquamarine” sneakers
china BX Ring Joint Gasket manufacture
cheap good kicks Saucony x Czarface Azura sneakers
rep good kicks ru Saucony Shadow 6000 “Salmon” sneakers
BX Ring Joint Gasket
Phenylpropanol
rep goodkick.ru shoes Saucony Shadow 6000 Prem sneakers
dtmx.pl
dj stage lights
Thiolactic Acid
china Expanded PTFE Sealing Tape
interactive projectors
RX Ring Joint Gasket
Propyl Mercaptan
china RX Ring Joint Gasket supplier
The person treating you will make sure you’re on the lowest possible dose to keep your condition under
control. You might also be given a drug called
a proton pump inhibitor or another medicine to protect your stomach.
Steroids are taken in different ways, and the
dosage may vary depending on the condition you have.
The table below gives an idea of how often you might need to take steroids.
It’s also associated with both athletic performance and general endurance.
You might be able to start consuming steroids if you have any infections related to use and skins.
If you have any wound and catch all your body, asteroids might delete these things, and you can feel better
and easily cover-up ups the symptoms.
These include increased nitrogen retention and protein synthesis, increased IGF-1 production, inhibition of glucocorticoids,
increased red blood cell production, increase collagen synthesis, etc.
Much like prohormones, basic steroid use entails a series of bulking and cutting campaigns.
During bulking cycles, people are taking supplements or steroids that enhance their overall muscle building abilities.
Contact your doctor or health care provider right away if any
of these apply to you. Anabolic steroids, together with Anavar, can increase the activity and sensitivity of oral anticoagulants .
The most frequent side effect of all Anabolic Androgenic Steroids
is liver toxicity and cholestatic jaundice, among others.
Such adverse results on the liver are seen in sufferers
utilizing high dosages of Oxandrolone or Anavar for more than one year and concomitant use with other anabolic
agents. No evidence has shown that the use
of Oxandrolone for a short time period had led to the development of liver feature impairment.
The products are safe and legal thus no more worrying
about doping bans and visit from authorities. Crazy bulk is a
well-known company for producing alternative legal steroids.
The appendix bursts or tears if appendicitis is not diagnosed quickly
and goes untreated. Peritonitis is a dangerous infection that happens when bacteria and other
contents of the torn appendix leak into the abdomen. In people with appendicitis, an abscess
usually takes the form of a swollen mass filled with
fluid and bacteria.
Bulking Stack- Costs $179.99 (instead of 229.99),the stack will help boost recovery times,
spur massive growth, and also boost your strength. The stack is a combination of Decaduro, Testo-max,D-Bal, and Trenorol.
HGH-X2 works well with other crucial amino acids in the
body, improving the production and release of HGH hormones.
These products work with the pituitary gland which releases this compound.
Its main ingredients include L-citrulline, L-arginine, acetyl-L-carnitine, all of which
are amino-acids supplements. The product also contains ginseng, wild yam, and Tribulus Terrestris.
Between the high rates of obesity and the amount of sugars and preservatives
in our food, it’s important to be proactive by leading
a lifestyle that promotes heart health. That starts with your
diet—knowing what you’re putting into your body and the effect it has on you is crucial.
There was also no relevant benefit for other endpoints including mortality rate by day 29 and time to recovery.
While NPP is used more for bulking, it can be used in a cutting cycle to protect an existing
lean muscle.
The elderly often experience less fever and less severe abdominal pain than other patients do
with appendicitis. Many older adults do not know that they have a serious problem until
the appendix is close to rupturing. A slight fever and abdominal
pain on one’s right side are reasons to call a doctor right away.
The use of testosterone therapy is increasingly common in the United States, with more
than 2 million men receiving the therapy. Testosterone is
available in different forms, including topicals such
as gels, creams, and patches; injections; and
pellets that are surgically placed directly beneath the skin. Yes absolutely, a crazy
bulk range of products is safer made from natural ingredients
that will boost and enhance your muscles and strength with no side
effects.
Here is my webpage; what are roids – https://learning.quill.com.au/blog/index.php?entryid=4889,
kayak straps
rep good kicks ru New Balance 480 lace-up sneakers
2 inch ratchet tie downs
SBR Rubber Sheet
cheap goodkick.ru shoes New Balance 574 panelled lace-up sneakers
rubber tie down straps
Asbestos Latex Sheet
Non-Asbestos Latex Paper
eye and eye slings
NBR Rubber Sheet
rep good kicks New Balance Rainier Low leather sneakers
cheap good kicks New Balance 670 “Made in England – Grey” sneakers
cheap good kicks ru New Balance M992NT “Tropical” sneakers
Non-Asbestos Jointing Sheets
automol.by
slings with hooks
affordable cheap gucci wallet men's Gucci Bag 2112YA0013
Bent Glass Coffee Table
affordable cheap gucci wear Gucci Bag 2205YA0041
ontocon.sdf-eu.org
Glass Table
Modern Led Outdoor Camping Lanterns Crystal Desk Lamps
New
Automotive Adjustable Torsion Bar
Wireless Restaurant Bar Touch Dimmable Hotel Desk Lamp
China Flip Computer Desk
reps best reps HYPE-adidas Yung-1 sneakers
Sheet Metal Fabrication
Glass Table Manufacturer
replica travis scott reps HYPE-adidas Yeezy Boost 350 V2 “Copper” sneakers
cheap cheap gucci watch Gucci Bag 2204YA0001
Sheetmetal Automotive OEM Parts