Package: e2j2
Table of contents:What is e2j2?Command line switches and config fileJinja2 filter supportExampleEnvironment variable examplesPlain environment variableTag fileTag jsonTag jsonfileTag base64Tag consulTag listTwo pass renderingTag vaultTag dnsWhat is e2j2?E2J2 (environment to jinja2 variables) is a command line tool which will render text and configuration files bases on jinja2 templates. Template variables can be defined by using plain or tagged environment variables.With tags within the environment variable you can specify different sources, sources include hashicorp consul, hashicorp vault, dns lookups and json.E2J2 can be used within docker containers, or for other simple configuration templating tasks. Within a docker container you can simply add the j2 extention to a configuration file, or to multiple files within one or more folder structures and then run e2j2 as part of your docker entrypoint script.Command line switches and config fileMost flags can be set on either the command line or in a json formatted config file. The following flags / configuration keys are supported:SwitchTypeConfig keyTypeDescription-h, –helpShow help text and exit-v, –versionShow version number-e, –ext, –extensionstringextensionstringJinja2 file extention (default: .j2)-f, –filelistcomma separated listfilelistarrayList of jinja2 templates-s, –searchlistcomma separated listsearchlistarrayList of directories to search for templates (default: current directory)-N, –noopskip writing template to disk-r, –recursiverecursivebooleanTraverse recursively through the search list–no-color, –nocolorno_colorbooleanDisable ANSI color-2, –twopasstwopassbooleanEnable two pass rendering-n, –nested-tagsnested_tagsbooleanEnable support for nested tags-m, –marker-setstringmarker_setstringSelect marker set (default: ‘{{‘)-A, –autodetect-marker-setautodetect_marker_setbooleanAutodetect marker set, fallback to defined marker set–block-startstringblock_startstringBlock marker start (default from marker set)–block-endstringblock_endstringBlock marker start (default: from marker set)–variable-startstringblock_startstringVariable marker start (default: from marker set)–variable-endstringvariable_endstringVariable marker end (default: from marker set)–comment-startstringcomment_startstringComment marker start (default: from marker set)–comment-endstringcomment_endstringComment marker start (default: from marker set)-w, –env-whitelistcomma separated listenv_whitelistarrayList of envars to include-b, –env-blacklistcomma separated listenv_blacklistarrayList of envars to exclude-P, –copy-file_permissionscopy_file_permissionsbooleanCopy file permissions and ownership from template to rendered file-S, –stacktracestacktracebooleanInclude stacktrace in error file–stderrbooleanPrint to stderr instead of to error file-c, –configstringConfig file path–watchlistcomma separated listwatchlistarrayWatch listed envvars for changes and render template(s) on change-R, –runstringrunarrayRun command after rendering templates (command arg1, ..)–splayintsplayintegerRandom delay between 1 and X seconds between watchlist polls–initial-runrender templates before starting watchJinja2 filter supportBy default only the jinja2builtin filtersare supported this can be extended by installing thejinja2-ansible-filtersmodule.Examplelets assume we want to render the following server block in nginx, if we
place the server configuration in a nginx include directory for example
/etc/nginx/conf.dserver{server_name{{NGINX.server_name}};listen80;listen[::]:80;error_page500502503504/50x.html;location/{index{{NGINX.index_page}};root{{NGINX.web_root}};}location~\.php${fastcgi_indexindex.php;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_passunix:{{NGINX.fpm_socket}};fastcgi_split_path_info^(.+\.php)(/.+)$;include{{NGINX.fcgi_params}};root{{NGINX.web_root}};try_files$uri=404;}}if you then set the NGINX environment variable, running e2j2 will render
the jinja2 template and place it in the same folder:~>exportNGINX='json:
{
"server_name": "www.myweb.com",
"index_page": "index.php",
"web_root": "/usr/local/www/myweb",
"fcgi_params": "/usr/local/etc/nginx/myweb-fcgi-params",
"fpm_socket": "/var/run/php-fpm/myweb.socket"
}'~>e2j2In:.rendering:nginx_vhost_config.conf.j2=>done=>writing:nginx_vhost_config.conf=>done~>catnginx_vhost_config.confserver{server_namewww.myweb.com;listen80;listen[::]:80;error_page500502503504/50x.html;location/{indexindex.php;root/usr/local/www/myweb;}location~\.php${fastcgi_indexindex.php;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_passunix:/var/run/php-fpm/myweb.socket;fastcgi_split_path_info^(.+\.php)(/.+)$;include/usr/local/etc/nginx/myweb-fcgi-params;root/usr/local/www/roundcube;try_files$uri=404;}}Environment variable examplesPlain environment variableExample:setting:MYENVVAR='plain environment variable'will render envvar-example.j2 to:This is a plain environment variableTag fileExample:setting:MYFILEVAR=file:./file-example.txtwill render file-example.j2 to:This is a file exampleTag jsonExample:setting:MYJSONVAR='json:{"key": "json-example"}'will render json-example.j2 to:This is a json-exampleTag jsonfileExample:setting:MYJSONFILEVAR='jsonfile:jsonfile-example.json'will render jsonfile-example.j2 to:This is a jsonfile example with subkeyTag base64Example:Setting:export MYBASE64VAR='base64:YmFzZTY0IGV4YW1wbGU='will render base64-example.j2 to:This is a base64 exampleTag consulConfiguration:You can configure the consul tag by setting the CONSUL_CONFIG
environment variable. The following config items are supported:ItemExplanationDefaulturlconsul urlhttp://127.0.0.1:8500schemeconsul url scheme http or httpsscheme from urlhostconsul hosthostname from urlportconsul http(s) portport from urltokenconsul tokennoneGlobal config example:read -d '' CONSUL_CONFIG << EOF
{
   "url": "https://consul.foobar.tld",
   "token": "abcdef01-0123-abcd-1234-0123456789ab"
}
EOFThe ACL token can be configured by either the above configuration or by setting the CONSUL_TOKEN variable.As an alternative for the global configuration it is also possible to configure / adjust the global configuration for each consul tag, by simply include the configuration when using the consul tag.CONSUL_TOKEN and config key token can either contain the actual token or point to a file containing the token, use thefile:tag to point to a file.Tag config examples:export MYCONSULVAR='consul:config={"url": "https://consul2.foobar.tld", "token": "012345678-0123-abcd-1234-0123456789ab"}:consulvar"export MYCONSULVAR='consul:config={"url": "https://consul2.foobar.tld", "token": "file:/path/to/token"}:consulvar"Consul example:Setting:key: consulvar in consul to value: consul exampleandexport MYCONSULVAR='consul:consulvar'will render consul-example.j2 to:This is a consul exampleTag listExample:Setting:export MYLIST='list:"first","second","third","fourth"'will render list-example.j2 to:"first"
"second"
"third"
"fourth"Two pass renderingStarting from version 0.1.12 e2j2 supports embedding jinja2 macros in
environment variables.Example:Setting the following two environment variables:export WORDPRESS='json:{"database": {"name": "mydb", "user": "mydb_user", "password": "{{ DBSECRET }}", "host": "localhost"}}'
export DBSECRET='file:./twopass-secret'will render (by running:e2j2-ftwopass-example.j2-2) to:// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'mydb' );

/** MySQL database username */
define( 'DB_USER', 'mydb_user' );

/** MySQL database password */
define( 'DB_PASSWORD', 'Db$ecr3t' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );In version 0.5.0 support was introduced for nested tag variables, so the above listed example can be simplified.
The DBSECRET variable is no longer needed, if the WORDPRESS variable is changed to:export WORDPRESS='json:{"database": {"name": "mydb", "user": "mydb_user", "password": "file:./twopass-secret", "host": "localhost"}}'If needed you can escape e2j2 tags by using the escape tagTag vaultConfiguration:You can configure the vault tag by setting the VAULT_CONFIG
environment variable. The following config items are supported:ItemExplanationDefaulturlvault urlhttp://127.0.0.1:8200schemevault url scheme http or httpsscheme from urlhostvault hosthostname from urlportvault http(s) portport from urlbackendvault secret backendrawtokenvault tokennonethe following backends are supported:backendDescriptionrawuse plain GET request to secret store APIkv1key/value version 1kv2key/value version 2Global config example:read -d '' VAULT_CONFIG << EOF
 {
   "url": "https://vault.foobar.tld:8200",
   "token": "s.xxxxxxxxxxxxxxxxxxxxxxx",
   "backend: "kv2"
 }
EOFThe Authentication token can be configured by either the above configuration or by setting the VAULT_TOKEN variable.As an alternative for the global configuration it is also possible to configure / adjust the global configuration for each vault tag, by simply include the configuration when using the vault tag.VAULT_TOKEN and config key token can either contain the actual token or point to a file containing the token, use thefile:tag to point to a file.Tag config example:export MYVAULTVAR='vault:config={"backend": "kv2", "token": "s.xxxxxxxxx"}:kv/my-secret"export MYVAULTVAR='vault:config={"backend": "kv2", "token": "file:/path/to/token"}:kv/my-secret"Vault example:Setting:vault kv put secret/my-secret secret=topsecret
export MYVAULTVAR='vault:secret/my-secret'will render vault-kv1-example.j2 (by running:e2j2-fvault-example.j2) to:** topsecret **
This is a vault exampleTag dnsConfiguration:You can configure the dns tag by setting the DNS_CONFIG
environment variable. The following config items are supported:ItemExplanationDefaultnameserversoverwrite nameserversuse system resolversportoverwrite dns port53typerecord type (A, AAAA or SRV)Athe supported record types will return a dict with the following keys:TypeKeysAaddressAAAAaddressSRVtarget, port, weight, priorityDNS example:Assuming a consul node running on localhost with the default dns port 8600.Setting the DNS_CONFIG variable:read -d '' DNS_CONFIG << EOF
 {
   "nameservers": ['127.0.0.1'],
   "port": 8600,
   "type": "SRV"
 }
EOFSetting:export MYDNSVAR='dns:consul.service.consul'will render dns-example.j2 (by running:e2j2-fdns-example.j2) to:My consul node:
node1.node.dc1.consul. listening on port 8300