1 # == Define: elasticsearch::template
3 # This define allows you to insert, update or delete templates that are used within Elasticsearch for the indexes
8 # String. Controls if the managed resources shall be <tt>present</tt> or
9 # <tt>absent</tt>. If set to <tt>absent</tt>:
10 # * The managed software packages are being uninstalled.
11 # * Any traces of the packages will be purged as good as possible. This may
12 # include existing configuration files. The exact behavior is provider
14 # * Puppet type reference: {package, "purgeable"}[http://j.mp/xbxmNP]
15 # * {Puppet's package provider source code}[http://j.mp/wtVCaL]
16 # * System modifications (if any) will be reverted as good as possible
17 # (e.g. removal of created users, services, changed log settings, ...).
18 # * This is thus destructive and should be used with care.
19 # Defaults to <tt>present</tt>.
22 # File path of the template ( json file )
23 # Value type is string
24 # Default value: undef
25 # This variable is optional
28 # Contents of the template ( json )
29 # Value type is string
30 # Default value: undef
31 # This variable is optional
34 # Host name or IP address of the ES instance to connect to
35 # Value type is string
36 # Default value: localhost
37 # This variable is optional
40 # Port number of the ES instance to connect to
41 # Value type is number
43 # This variable is optional
47 # * Richard Pijnenburg <mailto:richard.pijnenburg@elasticsearch.com>
49 define elasticsearch::template(
60 if ! ($ensure in [ 'present', 'absent' ]) {
61 fail("\"${ensure}\" is not a valid ensure parameter value")
64 if ! is_integer($port) {
65 fail("\"${port}\" is not an integer")
69 path => [ '/bin', '/usr/bin', '/usr/local/bin' ],
76 $es_url = "http://${host}:${port}/_template/${name}"
78 # Can't do a replace and delete at the same time
80 if ($ensure == 'present') {
82 # Fail when no file or content is supplied
83 if $file == undef and $content == undef {
84 fail('The variables "file" and "content" cannot be empty when inserting or updating a template.')
85 } elsif $file != undef and $content != undef {
86 fail('The variables "file" and "content" cannot be used together when inserting or updating a template.')
87 } else { # we are good to go. notify to insert in case we deleted
88 $insert_notify = Exec[ "insert_template_${name}" ]
93 $insert_notify = undef
97 # Delete the existing template
98 # First check if it exists of course
99 exec { "delete_template_${name}":
100 command => "curl -s -XDELETE ${es_url}",
101 onlyif => "test $(curl -s '${es_url}?pretty=true' | wc -l) -gt 1",
102 notify => $insert_notify,
106 if ($ensure == 'absent') {
108 # delete the template file on disk and then on the server
109 file { "${elasticsearch::configdir}/templates_import/elasticsearch-template-${name}.json":
111 notify => Exec[ "delete_template_${name}" ],
112 require => Exec[ 'mkdir_templates_elasticsearch' ],
116 if ($ensure == 'present') {
118 if $content == undef {
119 # place the template file using the file source
120 file { "${elasticsearch::configdir}/templates_import/elasticsearch-template-${name}.json":
123 notify => Exec[ "delete_template_${name}" ],
124 require => Exec[ 'mkdir_templates_elasticsearch' ],
127 # place the template file using content
128 file { "${elasticsearch::configdir}/templates_import/elasticsearch-template-${name}.json":
131 notify => Exec[ "delete_template_${name}" ],
132 require => Exec[ 'mkdir_templates_elasticsearch' ],
136 exec { "insert_template_${name}":
137 command => "curl -sL -w \"%{http_code}\\n\" -XPUT ${es_url} -d @${elasticsearch::configdir}/templates_import/elasticsearch-template-${name}.json -o /dev/null | egrep \"(200|201)\" > /dev/null",
138 unless => "test $(curl -s '${es_url}?pretty=true' | wc -l) -gt 1",