When your Puppet setup is getting larger, you must come across the duplicated resource declaration error.
We are not allowed to declare the same resource more than one time!
So Puppet introduces the virtual resource to solve this problem. A virtual resource will not be sent to the Puppet client until it is realized. Now, we could specify a virtual resource and mark it non-virtual as many times as you want in a configuration.
For example: I have a my_packages class which contains all the some package resources. It only contains virtual resources so any other class include this would do nothing.
class my_packages { # Standalone virtual package resources @package {[ 'git', 'gcc', 'gcc-c++', 'autoconf', 'automake', 'make', 'openssl', 'openssl-devel']: ensure => installed, } # Virtual custom resource for installing make define make { realize(Package['gcc'], Package['gcc-c++'], Package['autoconf'], Package['automake'], Package['make']) } @make{ 'make': } # Virtual custom resource for installing openssl define openssl { realize(Package['openssl'],Package['openssl-devel']) } @openssl{ 'openssl': } }
I have another class which specify a build server configuration.
class build_server { include my_packages # Install git and gcc realize(Package['git'], Package['gcc']) # Install all packages for make realize(My_packages::make['Make']) }
So this Puppet client may have other configuration other than the build server spec and that may also require the gcc or make packages. But since we could realize virtual resources more than once, the duplicated resource declaration error no longer exists.
Done =)
Reference: Puppet Labs Documentation – Virtual Resource Design Patterns
Of all the articles I have read on Virtual Resources, this is the most simple and precise explanation. Thank you very much for this.
LikeLike