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.
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class test::sysusers inherits test { | |
user{'terry': | |
ensure => present, | |
uid => 4009, | |
home => '/home/terry', | |
shell => $shell, | |
} | |
} |
This we will insert in two modules(manifests), test and testsql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class testsql { | |
include ::test::virtual | |
notify { "I am from the testsql":} | |
class {'::test::sysusers': } | |
} |
We have assigned "test" module assigned to centos server named centosserver, and "testsql" module assigned to a Ubuntu system named sqlserver1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node 'sqlserver1.labhome.com' { | |
class { 'testsql':} | |
} | |
node 'centosserver.labhome.com' { | |
class { 'test':} | |
} |
We will run now run the puppet agent on both the systems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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# | |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class test::sysusers inherits test { | |
@user{'terry': | |
ensure => present, | |
uid => 5009, | |
home => '/home/terry', | |
shell => $shell, | |
} | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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":} | |
} |
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