Ask Your Question
0

Reading variables from external file to puppet

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

Anu Thakur gravatar image

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

$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.

variables.txt

name='abc'
port=404
user="xyz"

createuser.pp

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

Thanks,
Anu

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

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

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

Comments

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 -0500 )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 -0500 )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 -0500 )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 -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: 2017-09-28 01:23:24 -0500

Seen: 29 times

Last updated: Sep 28