# Watch curl command using Puppet variables

**URL:** https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [September 2, 2016, 8:42pm UTC](https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711 "2016-09-02T20:42:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![denton64](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@denton64](https://discuss.elastic.co/u/denton64)
#### Post date: [September 2, 2016, 8:42pm UTC](https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711/1 "2016-09-02T20:42:35Z")

</div>

Hey y'all. Running into another issue. Instead of writing huge curls over and over in puppet, I can variablize it depending on the environment. However, it is not like my variable in the JSON. I have tried numerous corrections but the color is not going through. When I check the watches on my instance, it just shows **"color":""**

```auto
    ## Set-up variables 
    if ($tier_level == 'prod'){
      $color = 'red'
    }else{
      $color = 'yellow'
    }

      exec { 'api curl':
        command => "sudo curl -XPUT 'localhost:9200/_watcher/watch/api-error' -d '{
      \"trigger\" : {
          \"schedule\" : { \"interval\" : \"10s\" } 
        },
        \"input\" : {
          \"search\" : {
            \"request\" : {
              \"indices\" : [\"filebeat\"],
              \"body\" : {
                \"query\" : {
                  \"filtered\" : {
                    \"query\" : {
                  \"match_phrase\" : { \"source\": \"/var/www/html/logs/error_log\" }
                  },
                  \"filter\" : {
                    \"bool\": {
                    \"must\": [
                    {
                      \"range\": {
                        \"@timestamp\" : {
                        \"from\" : \"now-5m\",
                        \"to\" : \"now\"
                        }
                      }
                    }
                    ]
                  }
                }
              }
            }
          }
          }
          }
        },
        \"actions\" : {
        \"notify-hipchat\" : {
          \"throttle_period\" : \"1m\",
          \"hipchat\" : {
            \"account\" : \"notify-dev-monitoring\",
            \"message\" : {
              \"body\": \"{{#ctx.payload.hits.hits}}New error seen in api apache error_log! \n\nHost: {{_source.beat.hostname}}\nMessage: {{_source.message}} \n------------------------------------------------------------------------------\n{{/ctx.payload.hits.hits}}\",
              \"format\" : \"text\",
              \"color\" : \"$color\",
              \"notify\" : true
            }
          }
        }
      }
      }'
",
        before => Exec['web curl']
      }

```

Have tried ${color}, $color, {$color}, with single quotes/double quotes/alone.. Nothing seems to work. Please send help!!

---

<div class="post-metadata">

### Author: ![skearns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skearns/32/125945_2.png) [@skearns](https://discuss.elastic.co/u/skearns)
#### Post date: [September 2, 2016, 9:23pm UTC](https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711/2 "2016-09-02T21:23:19Z")

</div>

I love the idea of using puppet to automate the creation of Watches! Unfortunately, I don't speak puppet.. but a quick google suggests you could define a string to hold the command, then use the puppet `regsubst` to replace the word color with the actual variable, and pass that string as the command to exec?

[https://ask.puppet.com/question/13872/change-replace-character-in-string-and-add-strings/](https://ask.puppet.com/question/13872/change-replace-character-in-string-and-add-strings/)

---

<div class="post-metadata">

### Author: ![tylerjl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylerjl/32/44965_2.png) [@tylerjl](https://discuss.elastic.co/u/tylerjl)
#### Post date: [September 7, 2016, 8:23pm UTC](https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711/3 "2016-09-07T20:23:05Z")

</div>

Hi @denton64!

You may have a simpler time by using the puppet function [`template()`](https://docs.puppet.com/puppet/latest/reference/lang_template.html#with-a-template-file-template-and-epp). All the `template()` function does is pull in a file to use as a string in puppet, with the functionality to interpolate variables into your template content.

For example, in your case, you could use something like the following in your manifest (not exact, I'm just coming up with this as I go):

```auto
## Set-up variables 
if ($tier_level == 'prod'){
  $color = 'red'
}else{
  $color = 'yellow'
}

exec { 'api curl':
  command => template('mymodule/watch.json.erb'),
  before => Exec['web curl'],
}

```

And then in `mymodule/templates/watch.json.erb`:

```auto
...
              "format" : "text",
              "color" : "<%= @color %>",
              "notify" : true
...

```

The paths referenced here (like `mymodule`) need to be changed for your environment, but this is one way you could do it.

---

<div class="post-metadata">

### Author: ![denton64](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@denton64](https://discuss.elastic.co/u/denton64)
#### Post date: [September 8, 2016, 2:18pm UTC](https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711/4 "2016-09-08T14:18:18Z")

</div>

ahhh great thought! I got around it for the time being by just hard coding it in my template for elasticsearch.yml but thought of it as a bandaid. I will definitely play around with this at the end of my sprint. Thank you Tyler!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 1:43pm UTC](https://discuss.elastic.co/t/watch-curl-command-using-puppet-variables/59711/5 "2017-07-06T13:43:00Z")

</div>


