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,

class test::sysusers inherits test {
user{'terry':
ensure => present,
uid => 4009,
home => '/home/terry',
shell => $shell,
}
}
view raw users hosted with ❤ by GitHub


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

class test ($value=$test::params::value,
$shell=$test::params::shell ) inherits test::params {
include test::virtual
contain test::config
User <|title == 'minder'|>
realize (User['hunter'])
class test::sysusers
}
view raw init.pp hosted with ❤ by GitHub

class testsql {
include ::test::virtual
notify { "I am from the testsql":}
class {'::test::sysusers': }
}
view raw sqlinit.pp hosted with ❤ by GitHub


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

node 'sqlserver1.labhome.com' {
class { 'testsql':}
}
node 'centosserver.labhome.com' {
class { 'test':}
}
view raw site.pp hosted with ❤ by GitHub


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

[root@centosserver ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
sh: dpkg: command not found
Info: Caching catalog for centosserver.labhome.com
Info: Applying configuration version '1440750961'
Notice: /Stage[main]/Test::Sysusers/User[terry]/ensure: created
Notice: Applied catalog in 0.14 seconds
root@sqlserver1:/home/user# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: Class[Test::Sysusers] is already declared; cannot redeclare at /etc/puppetlabs/code/environments/production/modules/testsql/manifests/init.pp:6 at /etc/puppetlabs/code/environments/production/modules/testsql/manifests/init.pp:6:2 on node sqlserver1.labhome.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
root@sqlserver1:/home/user#
view raw gistfile1.txt hosted with ❤ by GitHub


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

class test::sysusers inherits test {
@user{'terry':
ensure => present,
uid => 5009,
home => '/home/terry',
shell => $shell,
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


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.

cat modules/test/manifests/init.pp
class test ($value=$test::params::value,
$shell=$test::params::shell ) inherits test::params {
include test::virtual
include test::sysusers
contain test::config
User <|title == 'minder'|>
realize (User['terry'])
}
cat modules/testsql/manifests/init.pp
class testsql {
include ::test::virtual
include ::test::sysusers
User <|title == 'minder'|>
User <|title == 'wallander'|>
User <|title == 'terry' |>
notify { "I am from the testsql":}
}
view raw gistfile1.txt hosted with ❤ by GitHub


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