Watch curl command using Puppet variables

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":""

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

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/

Hi @denton64!

You may have a simpler time by using the puppet function template() . 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):

## 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:

...
              "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.

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!