6

Chef - How to get the output of a command to a Ruby variable

I have the below scenario where i grep for a file

command "ls /filder1 | grep .txt"

grep gives an output of the filename/filenames if they exists .,

I want to store the output to a ruby variable and use it in the template to generate a config file.

How do i do that ?

Ravi D
  • 863
  • 8
  • 16

1 Answers1

11

Chef is Ruby so you can use backticks output = `find /filder1 | grep .txt`

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
  • but how to do that on the node? – brauliobo May 21 '14 at 20:20
  • 1
    Chef-Solo and variants runs exclusively on the node. Chef Client does some things to figure out what to run, downloads the needed files, then basically runs chef-solo ( at least last I looked in verison 10 and 11 ). It will have the results from the node running it as far as I know – EnabrenTane May 28 '14 at 20:14
  • 1
    Won't that get executed during the compilation pass through, as opposed to when Chef is running the converge process? – jaywhy13 May 29 '17 at 17:26