Skip to content

Instantly share code, notes, and snippets.

@ilumos
Created December 4, 2018 14:12
Show Gist options
  • Save ilumos/7540483b4f82133f7c44f640d1d1fa4c to your computer and use it in GitHub Desktop.
Save ilumos/7540483b4f82133f7c44f640d1d1fa4c to your computer and use it in GitHub Desktop.
Envsubst is a useful tool that saves you from writing complex and brittle seds

envsubst Usage Example

Envsubst is a useful tool that saves you from writing complex and brittle seds

.env File

export LOGSTASH_HOST="logstash.example.com:5044"
export CACHE_LOGS_DIRECTORY="/var/lancache/logs"

filebeat.yml.templ File

name: "lancache"
filebeat.prospectors:
- type: log
  paths:
    - ${CACHE_LOGS_DIRECTORY}/*.log

output.logstash:
  hosts: ["${LOGSTASH_HOST}"]

generate-config.sh File

#!/bin/bash
source .env
# envsubst only substitutes the vars passed to it in single quotes
# any other text starting with a dollar sign in the file will not be changed
envsubst '$LOGSTASH_HOST $CACHE_LOGS_DIRECTORY' < "filebeat.yml.templ" > "/etc/filebeat/filebeat.yml"

Generated config at /etc/filebeat/filebeat.yml

name: "lancache"
filebeat.prospectors:
- type: log
  paths:
    - /var/lancache/logs/*.log

output.logstash:
  hosts: ["/var/lancache/logs"]

How easy is that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment