This question already has an answer here:

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 => [

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:

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

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?

marked as duplicate by cuonglm, jimmij, Anthon, Stephen Kitt, chaos Aug 19 '15 at 8:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Use the -f option – jordanm Aug 18 '15 at 23:14

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'

Not the answer you're looking for? Browse other questions tagged or ask your own question.