Ask Your Question
0

Setting variables using hiera

asked 2015-05-18 09:54:46 -0500

confusedpuppet gravatar image

So in our enviornment we are trying use hiera as both an ENC and for setting variables for use in Puppet. I have the ENC portion working, but using it to set variables doesn't seem to work. For instance, here is the yaml for one of our machines:

machine1.dc.company.com:

---
classes:
  - apps::application

role: sm
enviornment: qa
shortname: machine1

Using the above yaml file and an ENC this machine is properly identified as being a qa box. But within a manifest I have the following:

class base::system_files {

# motd
        file { '/etc/motd':
                source          =>      'puppet://modules/base/%{::shortname}/motd',
                owner           =>      'root',
                group           =>      'root',
                mode            =>      644,
        }
}

And when running puppet in debug mode I can see that it's not inserting the shortname variable:

Error: /Stage[main]/Base::System_files/File[/etc/motd]: Could not evaluate: Could not retrieve file metadata for puppet://modules/base/%{::shortname}/motd: getaddrinfo: Name or service not known

I know I'm missing something simple, like I'm calling via top scope or something, but I've tried different notations ($shortname, $::shortname. etc).

Thanks in advance for any help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-18 10:18:17 -0500

arussell gravatar image

Putting a variable inside hiera doesn't make it a top scope variable. And it doesn't act the same as putting in the node scope of site.pp.

2 ways I see you can work this.

  1. Change yaml file to base::system_files::shortname: and change manifest to $shortname This will then use puppets auto hiera lookup Parameter Lookup
  2. Change manifest to do a hieralookup. $shortname = hiera('shortname')

If you use shortname other places it might be easier to use #2 so you don't have to have lots of variables in hiera for the same thing.

edit flag offensive delete link more

Comments

I definitely like the second option, as these variables are used throughout all of our manifests. I've added this above my class definition, but it's still not working. It also doesn't work when I invoke hiera from the command line, so I might have a larger issue.

confusedpuppet gravatar imageconfusedpuppet ( 2015-05-19 09:00:19 -0500 )edit

I believe it needs to be in the class definition not outside of it.

arussell gravatar imagearussell ( 2015-05-19 09:19:12 -0500 )edit

Yes you are correct, it had to be inside of the class definition. Also, in case anyone else is struggling with this, the source definition has to be in double quotes, not single quotes, else it will fail. Thanks for the help!

confusedpuppet gravatar imageconfusedpuppet ( 2015-05-19 10:48:55 -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-05-18 09:54:46 -0500

Seen: 236 times

Last updated: May 18 '15