Friday, August 28, 2015

Daily Puppet - Virtual Resources

Virtual Resources are used when there is a need to have the same resource part of many classes.

Lets look at an example.

We have a user named "terry", who has to be part of two classes

1. test --> a very generic module for Ubuntu
2. testsql --> Another very generic module :), but applied to a different OS, CentOS

So, we define it first,



This we will insert in two modules(manifests), test and testsql




We have assigned "test" module assigned to centos server named centosserver, and "testsql" module assigned to a Ubuntu system named sqlserver1.



We will run now run the puppet agent on both the systems



Refer to the error message for sqlserver1. It says "Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration:"

To overcome this, we use virtual resources



Look at the declaration, "@" with user, this indicates, the user resource declared is a virtual resource.

Now, we need to Realize the resource in init.pp in modules test and testsql , so that the resource is created on the target systems.



From the snippet it can be seen that, realization is done in two ways,

a) realize (User['terry'])
b) User <|title == 'terry' |>

(Don't forget to include the class test::virtual in both the init.pp files, otherwise, we can not get the resource at all )

Now, we run the puppet agent on the clients again



With virtual resources, we can create user terry on both the systems.





No comments:

Post a Comment