1

I'm writing Puppet configuration to automate the creation of an Elastic Search repository resource. Unfortunately, as far as I can tell, there's no way to specify this configuration in the Elastic Search YAML configuration file, so I'm stuck with HTTP and curl. I've declared the following as resources:

file { 'curator_repository_config':
    path    => "${elasticsearch::install_dir}/config/s3-repository.json",
    owner   => $elasticsearch::user,
    group   => $elasticsearch::user,
    mode    => '0400',
    content => template('chromeriver/curator/s3-repository.json.erb'),
}

exec { 'create_es_repository':
    command => "curl -is -X PUT 'http://localhost:9200/_snapshot/s3' -d @${elasticsearch::install_dir}/config/s3-repository.json",
    unless  => "curl -is -X GET 'http://localhost:9200/_snapshot/s3'",
    path    => '/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin',
    user    => $elasticsearch::user,
    require => [
        Service['elasticsearch'],
        File['curator_repository_config']
    ]
}

Understanding Puppet configuration isn't necessary to answer this question, but the above essentially creates a file called s3-repository.json which contains configuration details with are ultimately used in the POST to Elastic Search.

The second resource conditionally executes, only running if the return code from the following command is non-zero. It essentially does this:

#!/bin/bash
if ! curl -is -X GET 'http://localhost:9200/_snapshot/s3' &>/dev/null; then
    curl -is -X PUT 'http://localhost:9200/_snapshot/s3' @/path/to/s3-repository.json
fi

The problem I'm having is that curl returns 0 for a 404 on the GET request. I'd like to have curl return 1 if the response is a non-200 response.

Is there an easy way to do this with curl?

1
  • Use the -f option Commented Aug 18, 2015 at 23:14

1 Answer 1

1

I found my answer in the --fail option to curl. By passing this option, curl returns a non-zero exit code for non-200 responses:

curl -i -X GET --fail 'http://localhost:9200/_snapshot/s3'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.