3
votes

I have a curl command which is soap request and with headers like basic auth. I need to implement this curl in Chef recipe for automation.So that every time i execute the chef script ,i see this curl being executed too. Please advice me with a syntax for adding curl command in the chef script.

4
What's your ultimate goal? This contradicts with the idempodency thinking. Do you want that for some kind of reporting/monitoring of your chef runs? Instead of executing command, you can also use Ruby's http client. - StephenKing
Correct Stephen, considering calling the external REST API using ruby and wrapping this as Light weight resource. If on the other hand your just calling the external service to set some node data and really good approach is to call the REST service as a ohai plugin. Lots of options more detail would enable better advice. - Mark O'Connor

4 Answers

6
votes

Couldn't you do this using the http_request resource?

Something like:

http_request 'SOAP request' do
  url "http://www.example.com/example"
  message <xml request>
  action :post
end
5
votes

You can use bash commands in a chef recipe

bash 'install_something' do
  user 'root'
  cwd '/tmp'
  code <<-EOH
  wget http://www.example.com/tarball.tar.gz
  tar -zxf tarball.tar.gz
  cd tarball
  ./configure
  make
  make install
  EOH
end

Have this example from here: https://docs.chef.io/resource_bash.html. Just call you curl command. Keep in mind that this is not the real Chef way. The bash resource should only be used if there is no other escape.

0
votes

A simple command should work, similar to this.

  command "curl -m 5 -i -X POST -d \"payload={}\" http://www.some.place/here"

Consult the man page for curl to see how to add http headers.

0
votes

You can also use the bash resource. For example

bash 'make curl request' do
  cwd ::File.dirname(src_filepath)
  code <<-EOH
    curl -L https://github.com
    EOH
end

cwd is the directory in which the bash shell will be run in.