Shell trick: use template file

If you want to use a POSIX shell for building files from templates, you can use a nifty trick shared on StackOverflow.

$ eval "cat <<EOF >outputfile
$( cat template.in )
EOF
" 2>/dev/null

And template.in can contain shell variables or even command substitution!

$ cat template.in
[domain/$( hostname --domain )]

debug_level = 1
id_provider = ipa
ipa_server = _srv_, $( hostname --domain )
ipa_domain = $( hostname --domain )
ipa_hostname = $( hostname --fqdn )

This is a neat little trick, but should not be a regular substitute for better templating mechanisms like the jinja2 in ansible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.