Ask Your Question
0

how to make attributes mandatory i.e if not specified puppet should raise exception?

asked 2017-02-26 22:54:28 -0500

Hyder gravatar image

I'm creating a custom type something like xyz and I want to make some attributes mandatory. its attributes are name, ipaddress and store. if ipaddress and store are not specified puppet should raise exception. for example : if given puppet apply -e 'xyz { "name":}' then puppet should raise exception saying ipaddress and store not specified. I have done research but no use, I didnt find solution this anywhere. please understand me and help me out Thank you

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2017-02-27 01:50:23 -0500

I personally use a few options. In Puppet 3.x, I use the stdlib. You can find it on GitHub here:

https://github.com/puppetlabs/puppetl...

Simply, this is a cool lib that allows you to check data types in your modules and throw terminating errors when they don't match.

Puppet 4.x now has support for data types built-in. You can find them here:

https://docs.puppet.com/puppet/4.9/langdatatype.html

Lastly, check if the value exists and use "fail" to throw a terminating error. The "fail" message can be anything you want.

https://docs.puppet.com/puppet/latest...

edit flag offensive delete link more

Comments

Thanks alot and one more I would like to ask I want the value of parameter value to be assinged to the next parameter for example newparam(:xyz) do end newparam(:abc) do abc = xyz [ how can i do this?] end

Hyder gravatar imageHyder ( 2017-02-27 05:57:02 -0500 )edit

Normally you would try and abstract this type of logic out of your class with a role/profile pattern.

class ::my::profile {
  $my_param = 'foobar'

  ::class1 {'no1':
    parameter => $my_param
  }

  ::class2 {'no2':
    parameter => $my_param
  }

luksi1 gravatar imageluksi1 ( 2017-02-27 13:44:45 -0500 )edit

If the original answer answers the original question, please upvote it, so that other people can easily find the solution.

luksi1 gravatar imageluksi1 ( 2017-02-27 13:45:23 -0500 )edit

Puppet::Type.newtype(:foo) do newparam(:bar) do end newparam(:foobar) do foobar = bar end I'm literally new to puppet please would you tell me like above syntax. btw I'll upvote it now. Thanks

Hyder gravatar imageHyder ( 2017-02-27 23:06:47 -0500 )edit
0

answered 2017-02-27 23:25:28 -0500

lavaman gravatar image

Puppet will fail if there are parameters that do not have defaults and have no values provided through class calls or hiera. These parameters are considered required and a value must be provided in order for the catalog to compile.

edit flag offensive delete link more

Comments

Puppet::Type.newtype(:foo) do newparam(:bar) do end newparam(:foobar) do foobar = bar (how can I assign this) end I'm literally new to puppet please would you tell me like above syntax. Thanks

Hyder gravatar imageHyder ( 2017-02-28 02:53:19 -0500 )edit

I apologize that I didn't see that you were writing a custom class in Ruby. This is a bit advanced, but there's a mess of code from puppetlabs on github. I suggest you start there.

luksi1 gravatar imageluksi1 ( 2017-03-01 01:55:08 -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-02-26 22:54:28 -0500

Seen: 46 times

Last updated: Feb 27