Ask Your Question
1

How to get the value of a resource's attributes

asked 2014-09-01 08:55:31 -0500

xkcd gravatar image

Say I have defined a resource like this:-

file { "package":
        path => "/root/my_package.x86_64.rpm",
}

How can I get access the attribute of the file resource from another resource. In the ideal case, I want to do this :-

exec { "Install My Package":
       require => File["package"],
       command => "/bin/rpm -i File["package"]->path",
       refreshonly => true,
  }

I've searched a lot on Google and puppetlabs site but can't find any help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2014-09-02 03:42:37 -0500

Zathras gravatar image

You can set a variable that bot resources have access to

$path_to_package

file { 'package':
        path => $package,
}

exec { 'Install My Package':
       require => File["package"],
       command => "/bin/rpm -i ${path_to_package}",
       refreshonly => true,
}

But if you only need to install a rpm package, you can just use the Package resource itself: https://docs.puppetlabs.com/references/latest/type.html#package-attribute-source

edit flag offensive delete link more
2

answered 2014-09-02 10:40:14 -0500

reidmv gravatar image

updated 2014-09-02 10:40:54 -0500

Trying to reference and return parameter values from resources is problematic due to parse-order dependent behavior, and so should be avoided in general. If there is a situation where you can guarantee that a resource has already been added to the catalog and you want to introspect one of its parameters, you can do so today with the getparam() function from the puppetlabs/stdlib module.

https://forge.puppetlabs.com/puppetlabs/stdlib#getparam

In the particular example you gave, you might retrieve the value by calling

getparam(File["package"], "path")

There is work underway that may eventually incorporate the ability to more directly refer to a particular parameter of a known resource but for now, the above function is how the stated end can be achieved.

That said, the better route is almost certainly to do as Zathras suggested and use a variable.

edit flag offensive delete link more

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: 2014-09-01 08:55:31 -0500

Seen: 925 times

Last updated: Sep 02 '14