Exec is a very useful resource type present in Puppet which is used to executes external commands.
Puppet basically runs as a daemon in which it executes every 30 mins. So while writing an exec resource type make sure that the exec resource must be able to run multiple times without causing any harm to the machine i.e. it must be idempotent.
Suppose you just want to run a command like reload a service for that what you can do is?
It states that "reload httpd" exec resource type was executed successfully.
Another example you want to add a user "optimus" via exec resource and add that user to "transformers" group. In this case I have written two exec resources one to create a group [prerequisites as without transformers group a user optimus cannot be added].
exec { "Add user optimus":
command => "useradd optimus -g transformers",
path => "/usr/local/bin/:/bin/:/sbin/:/usr/sbin/",
require => Exec ["Add group transformers"],
}
exec { "Add group transformers":
command => "groupadd transformers",
path => "/usr/local/bin/:/bin/:/sbin/:/usr/sbin/",
}
Here you can see that we have used path attribute in both the exec resources separately to save our bit of a code we can get this defined globally and can use this as:
Exec { path => [ "/usr/local/bin/", "/bin/" , "/sbin/", "/usr/sbin/" ] }
exec { "Add user optimus":
command => "useradd optimus -g transformers",
require => Exec ["Add group transformers"],
}
exec { "Add group transformers":
command => "groupadd transformers",
}
Lets apply site.pp:
[root@puppet manifests]# puppet apply site.pp
notice: /Stage[main]//Exec[Add group transformers]/returns: executed successfully
notice: /Stage[main]//Exec[Add user optimus]/returns: executed successfully
notice: Finished catalog run in 0.36 seconds
[root@puppet manifests]#
But if you try to run above command again it will throw errors as both the user and group are already added to the machine.
[root@puppet manifests]# puppet apply site.pp
err: /Stage[main]//Exec[Add group transformers]/returns: change from notrun to 0 failed: groupadd transformers returned 9 instead of one of [0] at /etc/puppet/manifests/nodes.pp:10
notice: /Stage[main]//Exec[Add user optimus]: Dependency Exec[Add group transformers] has failures: true
warning: /Stage[main]//Exec[Add user optimus]: Skipping because of failed dependencies
notice: Finished catalog run in 0.09 seconds
[root@puppet manifests]#
Remember Puppet basically runs as a daemon in which it executes every 30 mins, so the code should be idempotent.
For getting above error resolved we can use onlyif or unless attribute.
unless => "grep -c optimus /etc/passwd", [For add user exec resource].
unless => "grep -c transformers /etc/group", [For add group exec resource].
So now our full code will be:
Exec { path => [ "/usr/local/bin/", "/bin/" , "/sbin/", "/usr/sbin/" ] }
exec { "Add user optimus":
command => "useradd optimus -g transformers",
unless => "grep -c optimus /etc/passwd",
require => Exec ["Add group transformers"],
}
exec { "Add group transformers":
command => "groupadd transformers",
unless => "grep -c transformers /etc/group",
}
Lets run this again:
[root@puppet manifests]# puppet apply site.pp
notice: Finished catalog run in 0.16 seconds
[root@puppet manifests]#
Comments
Post a Comment