- Mastering Puppet 5
- Ryan Russell Yates Jason Southgate
- 115字
- 2021-07-16 17:46:18
Using the GET and SET methods to manage type properties
Each property defined in the type should implement a GET and SET method in the provider.
Puppet will then invoke these methods during a Puppet run to manage the property as follows:
- The GET method is called initially to retrieve the current value
- This is subsequently compared against the value declared by the user in the Puppet DSL
- If the values are different, then the SET method is invoked to update that value if necessary.
This is shown in the following code:
Puppet::Type.type(:mynewtype).provide(:yum) do
...
def version
version = rpm('-q', resource[:name])
if version =~ /^#{Regexp.escape(resource[:name])}-(.*)/
$1
end
end
def version=(value)
yum('install', "#{resource[:name]}-#{resource[:version]}")
end
...
end