consul_data

pdk
ploperations-consul_data provides a simple way to get or set key/value pairs and query for the nodes backing a service in Consul.
Puppet Operations

Puppet Operations

ploperations

6,587 downloads

6,587 latest version

4.6 quality score

Version information

  • 0.1.0 (latest)
released Jan 30th 2019
This version is compatible with:
  • Puppet Enterprise 2019.8.x, 2019.7.x, 2019.5.x, 2019.4.x, 2019.3.x, 2019.2.x, 2019.1.x, 2019.0.x
  • Puppet >= 6.0.0 < 7.0.0
  • CentOS
    ,
    OracleLinux
    ,
    RedHat
    ,
    Scientific
    ,
    Debian
    ,
    Ubuntu
    ,
    windows

Start using this module

Documentation

ploperations/consul_data — version 0.1.0 Jan 30th 2019

Use data from Consul in Puppet code

Table of Contents

  1. Description
  2. Setup
  3. Usage & Reference
  4. Development

Description

consul_data provides a simple way to interact with Consul. You can get or set key/value pairs and query for the nodes backing a service. You can also wrap function calls in Deferred() to query the local Consul agent on a node instead of reaching out to your Consul cluster from the Puppet master.

Setup

All that's required to use this module is it being added to your Puppetfile and pluginsync being enabled.

Usage & Reference

consul_data::get_key($consul_url, $key, $key_return_format)

Get the value of a key from Consul

  • consul_url: The full url including port for querying Consul
  • key: The key you wish to query for
  • key_return_format: The format in which to return the value of the key key.
    • Defaults to string but may also be hash, json, or json_pretty.
    • All options other than string assume the data is stored in JSON format.

This will return the value of the key in the specified format.

consul_data::get_service_nodes($consul_url, $service)

Querys for nodes providing a given service

  • consul_url: The full url including port for querying Consul
  • service: The service you want to get a list of nodes for

This will return a Hash representing the JSON response from Consul.

Example: Get a list of nodes running Consul and their IP addresses

$data = consul_data::get_service_nodes('https://consul-app.example.com:8500', 'consul')
$data.each |$consul_node| {
  notify { "${consul_node['Node']} is at ${consul_node['Address']}": }
}

consul_data::set_key($consul_url, $key, $value)

Set, update, or delete a key in Consul

  • consul_url: The full url including port for querying Consul
  • key: The key you wish to set, update, or delete
  • value:
    • if set to undef the specified key will be deleted
    • if set to a string the key will be updated to contain that string
    • if set to a hash or and array of hashes the key will be updated to contain the JSON representation of that value passed in.

Query the Consul agent on a node directly

You can take advantage of Deferred() to interact with the Consul agent on a node directly. For example:

$some_value_from_consul = Deferred('consul_data::get_key', [$consul_url, 'foo', 'json_pretty'])

file { $some_config_file:
  ensure  => file,
  content => $some_value_from_consul,
}

You can also use a templated file to benefit from querying the node's agent:

$variables = {
  'nodes_hash' => Deferred('consul_data::get_service_nodes',[
    'https://consul-app.example.com:8500',
    'my-service'
  ]),
}

# use inline_epp(), and file() to compile the template source into the catalog
file { '/etc/my-service.conf':
  ensure  => file,
  content => Deferred('inline_epp', [
    file('mymodule/my-service.conf.epp'),
    $variables
  ]),
}

Note: the tempalte resides at mymodule/files/my-service.conf.epp as opposed to in the templates directory.

In this example, you could have a template that used values found in the hash nodes_hash such as the node names and their IP addresses.

Development

Pull requests are welcome!