Defined Type: consul::service

Defined in:
manifests/service.pp

Summary

Sets up a Consul service definition

Overview

Examples:

simple MySQL service

consul::service { 'my_db':
  port                => 3306,
  tags                => ['db','mysql'],
  address             => '1.2.3.4',
  token               => 'xxxxxxxxxx',
  service_config_hash =>  {
    'connect' => {
      'sidecar_service' => {},
    },
  },
  checks              => [
    {
      name     => 'MySQL Port',
      tcp      => 'localhost:3306',
      interval => '10s',
    },
  ],
}

simple HTTPS service

consul::service { 'my_https_app':
  port                => 443,
  tags                => ['web','rails'],
  address             => '1.2.3.5',
  token               => 'xxxxxxxxxx',
  service_config_hash =>  {
    'connect' => {
      'sidecar_service' => {},
    },
  },
  checks              => [
    {
      name            => 'HTTPS Request',
      http            => 'https://localhost:443',
      tls_skip_verify => true,
      method          => "GET",
      headers         => { "Host" => ["test.example.com"] },
    },
  ],
}

Parameters:

  • address (Optional[String[1]]) (defaults to: undef)

    IP address the service is running at.

  • checks (Array[Hash]) (defaults to: [])

    If provided an array of checks that will be added to this service

  • enable_tag_override (Boolean) (defaults to: false)

    enable_tag_override support for service. Defaults to False.

  • ensure (String[1]) (defaults to: 'present')

    Define availability of service. Use ‘absent’ to remove existing services. Defaults to ‘present’

  • id (String[1]) (defaults to: $title)

    The unique ID of the service on the node. Defaults to title.

  • port (Optional[Integer[0, 65535]]) (defaults to: undef)

    TCP port the service runs on.

  • service_name (String[1]) (defaults to: $title)

    Name of the service. Defaults to title.

  • service_config_hash (Hash) (defaults to: {})

    Use this to populate the basic service params for each of the services

  • tags (Array[String[1]]) (defaults to: [])

    Array of strings.

  • token (Optional[String[1]]) (defaults to: undef)

    ACL token for interacting with the catalog (must be ‘management’ type)

  • meta (Optional[Hash[ String[1], Variant[ String[1], Numeric, Boolean, ]]]) (defaults to: undef)

    Service meta key/value pairs as hash.

See Also:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'manifests/service.pp', line 59

define consul::service (
  Optional[String[1]]         $address              = undef,
  Array[Hash]                 $checks               = [],
  Boolean                     $enable_tag_override  = false,
  String[1]                   $ensure               = 'present',
  String[1]                   $id                   = $title,
  Optional[Integer[0, 65535]] $port                 = undef,
  String[1]                   $service_name         = $title,
  Hash                        $service_config_hash  = {},
  Array[String[1]]            $tags                 = [],
  Optional[String[1]]         $token                = undef,
  Optional[Hash[
      String[1],
      Variant[
        String[1],
        Numeric,
        Boolean,
  ]]]                         $meta                 = undef,
) {
  include consul

  consul::validate_checks($checks)

  if versioncmp($consul::version, '1.0.0') >= 0 {
    $override_key = 'enable_tag_override'
  } else {
    $override_key = 'enableTagOverride'
  }

  $default_config_hash = {
    'id'                => $id,
    'name'              => $service_name,
    'address'           => $address,
    'port'              => $port,
    'tags'              => $tags,
    'checks'            => $checks,
    'token'             => $token,
    'meta'              => $meta,
    $override_key       => $enable_tag_override,
  }

  $basic_hash = $default_config_hash + $service_config_hash

  $service_hash = {
    service => $basic_hash.filter |$key, $val| { $val =~ NotUndef },
  }

  $escaped_id = regsubst($id,'\/','_','G')
  file { "${consul::config_dir}/service_${escaped_id}.json":
    ensure  => $ensure,
    owner   => $consul::user_real,
    group   => $consul::group_real,
    mode    => $consul::config_mode,
    content => consul::sorted_json($service_hash, $consul::pretty_config, $consul::pretty_config_indent),
    notify  => Class['consul::reload_service'],
  }
}