Ask Your Question

Reading variables from external file to puppet

asked 2017-09-28 01:23:24 -0600

Anu Thakur gravatar image

updated 2017-09-28 12:48:26 -0600

$readfile is getting all the contents of variables.txt into it, but i want only the subsequent values of the variables assigned to them in .pp file as it is in variables.txt. Please help if it is possible.




$readfile    = file('variables.txt'),
$name       = (i want the value in variable.txt to be read here)


edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-09-28 12:41:57 -0600

Well, there are other ways to feed Puppet with values, but if you insist on using your variables.txt, you gotta “parse” it. Note, you can't really do any syntax stunts with Puppet though.

class my_module::create_user {
  $_file_contents = file('my_module/variables.txt')
  $_file_lines    = split($_file_contents, "\n")
  # ignores the possibility the right hand side contains equal signs
  $_file_tuples   = split($_file_lines, '=')
  $_file_hash     = Hash($_file_tuples)
  # now you're ready to use it
  $_name          = $_file_hash['name']

  # ... do your stuff using $_name ...

Your $_name still contains the surrounding single quotes. To eliminate these, you'd have to impose further constraints on what your variables.txt may contain.

edit flag offensive delete link more


That will only work if the file is in the module or on the puppet master, It's not clear if the OP has the file on the agent or the master

DarylW gravatar imageDarylW ( 2017-09-29 09:10:15 -0600 )edit

“`$readfile` is getting all the contents of variables.txt into it […]” → at least Anu's getting somehow some values loaded into the `$readfile` variable.

Kai Burghardt gravatar imageKai Burghardt ( 2017-09-29 10:26:14 -0600 )edit

Thanks Kai, I have found another way by using custom facts. Read the values of variables.txt and filtered them using grep and cut in fact(.rb) and used them as $::name

Anu Thakur gravatar imageAnu Thakur ( 2017-10-03 05:25:02 -0600 )edit

under lib/facter Facter.add(:name) do setcode do Facter::Util::Resolution.exec('cat variables.txt 2>&1 | grep name | cut -d "=" -f2') end

Anu Thakur gravatar imageAnu Thakur ( 2017-10-03 05:26:27 -0600 )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


Asked: 2017-09-28 01:23:24 -0600

Seen: 1,558 times

Last updated: Sep 28 '17