Ask Your Question
2

How to store linux comman output in a variable

asked 2015-11-19 03:57:21 -0500

updated 2015-11-19 23:03:39 -0500

Hi Team,

Is it possible to store linux command result in variable ?

I am trying to store a encrypted value in a variable. To encrypt i am using base64 command. To store it in variable, I am using generate method. But I am not able to store executed value in a variable.

I have tried in the following ways.:

 ( i )  $secretvalue = generate("/bin/bash","-c","/usr/bin/echo ${password} | /usr/bin/base64")
 ( ii ) $secretvalue = generate("/usr/bin/echo ${password} | /usr/bin/base64")

In the second approach, I got the following error

Evaluation Error: Error while evaluating a Function Call, Generators can only contain alphanumerics, file separators, and dashes at

Any help is much appreciated.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2015-11-23 01:59:02 -0500

Option 1 :

To execute any command on Puppet Master server you can use inline_template function with ERB template inside and Ruby code for execute shell command:

$password = "12345"
$secretvalue = inline_template("<%= `/bin/echo ${password} | /usr/bin/base64` %>")
notify { "STDOUT: ${secretvalue}": }

Option 2 :

P.S. Just to encode string to Base64 format you can import puppetlabs-stdlib module and use base64 function from it:

$secretvalue = base64('encode', $password)
edit flag offensive delete link more
2

answered 2015-11-19 20:02:41 -0500

GregLarkin gravatar image

When you say that you cannot "store a value", what do you mean exactly? Do you mean you receive an error or is there some other problem? Also, I'm not sure that the generate function takes multiple arguments the way you can written it in your example.

I'm sure you know this, but base64 is not an encryption algorithm and easily reversed to recover your plaintext. If you are storing sensitive data, I would encourage you to use the hiera-eyaml backend with Puppet.

You can get more information here:

https://puppetlabs.com/blog/encrypt-your-data-using-hiera-eyaml

https://github.com/TomPoulton/hiera-eyaml#hiera-eyaml

edit flag offensive delete link more

Comments

@GregLarkin: Sorry for posting it in confusing way. Actually, I mean to say $secretvalue contains empty as always. It doesn't contain the value which was return by the base64 algorithm.

ArunRaj Rajavel gravatar imageArunRaj Rajavel ( 2015-11-19 22:36:26 -0500 )edit

@GregLarkin : My Configuration file has to be populated with base64 encrypted value. I am trying to do it manually in the Module. Thanks for the good article. But It will not help in my case.

ArunRaj Rajavel gravatar imageArunRaj Rajavel ( 2015-11-19 23:07:16 -0500 )edit

How is the variable ${password} set, i.e. is it looked up from external service, set from Hiera or some other process?

GregLarkin gravatar imageGregLarkin ( 2015-11-20 16:52:43 -0500 )edit

@GregLarkin: Yes It is been looked up from Hiera

ArunRaj Rajavel gravatar imageArunRaj Rajavel ( 2015-11-21 01:08:50 -0500 )edit
-2

answered 2015-11-20 08:34:55 -0500

scoffland gravatar image

This question has already been answered in http://ask.puppetlabs.com/question/16...

As mentioned here you can use /etc/facter/facts.d

Or with the generate function you might try adding spaces in you shell command

$secretvalue = generate("/bin/bash", "-c", "/usr/bin/echo ${password} | /usr/bin/base64")
edit flag offensive delete link more

Comments

@scoffland: I have tried the above same command. It is returning the secretvalue as empty. I don't get the secretvalue. But password contains values.

ArunRaj Rajavel gravatar imageArunRaj Rajavel ( 2015-11-21 01:42:20 -0500 )edit
1

Make sure you check if you should use /bin/echo or /usr/bin/echo.

GregLarkin gravatar imageGregLarkin ( 2015-11-24 19:01:23 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-11-19 03:57:21 -0500

Seen: 654 times

Last updated: Nov 23 '15