Forge Home

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

ploperations

9,317 downloads

243 latest version

3.1 quality score

Version information

  • 1.0.0 (latest)
  • 0.1.0
released Sep 17th 2021
This version is compatible with:
  • Puppet Enterprise 2021.3.x, 2021.2.x, 2021.1.x, 2021.0.x, 2019.8.x, 2019.7.x, 2019.5.x, 2019.4.x, 2019.3.x, 2019.2.x, 2019.1.x
  • Puppet >= 6.1.0 < 8.0.0
  • CentOS
    ,
    OracleLinux
    ,
    RedHat
    ,
    Scientific
    ,
    Debian
    ,
    Ubuntu
    ,
    windows

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'ploperations-consul_data', '1.0.0'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add ploperations-consul_data
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install ploperations-consul_data --version 1.0.0

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

ploperations/consul_data — version 1.0.0 Sep 17th 2021

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 template 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!