Ask Your Question
1

curl output in variable using a function [closed]

asked 2016-11-29 15:00:34 -0500

updated 2016-11-29 15:04:26 -0500

I have a curl command as follows:

 curl -H "X-Token: token val" -X GET  http://nodename.domain.com:8200/v1/generic/appname/projectname/environment/username | jq -r .data.value

I would have to get the output from the above curl command and assign it to a variable in puppet, how do i do that?

I see that there is custom facts, generate command and custom functions. I am not sure which one to use and what is the difference between custom fact and custom function.

Also, the curl will be executed in the puppet agent node side, so i am not seeing generate as a best fit for this since in the docs it is mentioned it runs in the master. May be a ruby code for doing this ?

  $username1 = get_username($token, hiera(‘key1’))
  $password1 = get_password($token, hiera(‘key2’)) 
  $username2 = get_username($token, hiera(‘key3’))
  $password2 = get_password($token, hiera(‘key4’))

Please provide some examples, many thanks!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Redsmile
close date 2017-01-31 15:56:04.162106

1 Answer

Sort by » oldest newest most voted
2

answered 2016-11-30 02:05:06 -0500

lupin gravatar image

Here a very simple example.

cat ../lib/puppet/parser/functions/pw_retreiver.rb

require 'rest-client'
require 'json'
module Puppet::Parser::Functions
  newfunction(:pw_retreiver, :type => :rvalue) do |args|
    url = args[0]
    uri = args[1]
    res = RestClient.get(url + uri)
    data = JSON.parse(res.body)
    data['name'] 
   end
end

Usage: [root@orc-app2 manifests]# grep -v "^#" init.pp

class m {
 $value = pw_retreiver('http://jsonplaceholder.typicode.com', '/users/1')

 notify { "Value:${value}": }

}

Result:

   [root@orc-app2 manifests]# puppet apply ../examples/init.pp 
    Notice: Compiled catalog for orc-app2.dev in environment production in 0.46 seconds
    Notice: Value:Leanne Graham
    Notice: /Stage[main]/M/Notify[Value:Leanne Graham]/message: defined 'message' as 'Value:Leanne Graham'
    Notice: Applied catalog in 0.01 seconds

Note: - No error checking - You need rest-client gem, if you're using PE you to install it with PE gem(/opt/puppetlabs/puppet/bin/gem) - You need to customise the args value to handle authentication - Actual data response of the url above can be view here http://jsonplaceholder.typicode.com/u...

Hth, Lupin

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-29 15:00:34 -0500

Seen: 146 times

Last updated: Nov 30 '16