Created
March 29, 2017 09:22
-
-
Save eranchetz/d861043fba274922638244ca19d3173a to your computer and use it in GitHub Desktop.
Example Chef recipe to install Consul Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ | |
# /opt/my-app/consul.ctmpl | |
# | |
# This file is read by Consul Template and rendered onto disk using | |
# the configuration placed in /etc/consul-template.d. | |
}} | |
{{ with vault "postgresql/creds/readonly" }} | |
[config] | |
username = "{{ .Data.username }}" | |
password = "{{ .Data.password }}" | |
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: This recipe is designed to be informational and is not a copy-paste | |
# implementation. Please see the following blog post for more information: | |
# | |
# https://www.hashicorp.com/blog/using-hashicorp-vault-with-chef.html | |
# | |
# Install the unzip package because Consul and Consul Template are | |
# published as ZIP files. | |
package "unzip" | |
# Download the latest version of Consul Template using the remote_file | |
# resource in Chef and trigger an extraction. | |
remote_file "/tmp/consul-template.zip" do | |
source "https://releases.hashicorp.com/consul-template/0.12.1/consul-template_0.12.1_linux_amd64.zip" | |
owner "root" | |
group "root" | |
mode "0755" | |
backup false | |
action :create_if_missing | |
notifies :run, "execute[extract_consul_template]", :immediately | |
end | |
# Unzips the binary. | |
execute "extract_consul_template" do | |
command <<-EOH | |
unzip /tmp/consul-template.zip | |
mv consul-template /usr/local/bin/consul-template | |
chmod +x /usr/local/bin/consul-template | |
EOH | |
action :nothing | |
end | |
# Create the configuration directory where the template configurations | |
# will reside. | |
directory "/etc/consul-template.d" do | |
owner "root" | |
group "root" | |
action :create | |
end | |
# Create an upstart script - this could also be systemd or some other | |
# init system of your preference. | |
template "/etc/init/consul-template.conf" do | |
source "upstart-consul-template.conf" | |
owner "root" | |
group "root" | |
mode "0644" | |
end | |
# Start the service and register it with Chef. | |
service "consul-template" do | |
provider Chef::Provider::Service::Upstart | |
action :enable | |
end | |
# This writes the Consul Template template that Consul Template will | |
# read, parse, communicate with Vault, and render as the application | |
# configuration. Since Consul Template is running as a process, it | |
# will read all files in /etc/consul-template.d as configured in the | |
# upstart script above. | |
template "/etc/consul-template.d/my-app.hcl" do | |
source "my-app-ct.hcl" | |
owner "root" | |
group "root" | |
mode "0644" | |
notifies :reload, "service[consul-template]", :delayed | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# templates/my-app.hcl | |
# | |
# This file is used to configure an instance of the Consul | |
# Template process. This tells Consul Template to ingress | |
# the file at /etc/my-app/config.ctmpl, communicate with Vault, | |
# and then write the resulting contents to /opt/my-app/config. | |
# If the template changes, Consul Template will restart the | |
# application. | |
template { | |
source = "/opt/my-app/config.ctmpl" | |
destination = "/opt/my-app/config" | |
command = "service my-app reload" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# templates/upstart-consul-template.conf | |
# | |
# This is a sample upstart configuration template for Consul | |
# Template that tells Consul Template to read all configuration | |
# from /etc/consul-template.d. | |
description "consul-template" | |
start on runlevel [2345] | |
stop on runlevel [!2345] | |
respawn | |
script | |
/usr/local/bin/consul-template \ | |
-config="/etc/consul-template.d/" \ | |
>> /var/log/consul-template.log 2>&1 | |
end script | |
post-stop exec sleep 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment