exec resources
Puppet
In Puppet, the combined configuration to be applied to a host is called a catalog, and the process of applying it is called a run.
The Puppet client software is called the agent. Puppet calls the definition of the
host itself a node. The Puppet server is called the master.
We want Puppet to run a certain command directly using an exec resource. This is a very flexible and powerful resource, and we can use it to implement almost anything in Puppet.
ubuntu@ip-172-31-45-62:/etc/puppet$ sudo mkdir -p modules/exec/manifests
# modules/exec/manifests/init.pp class exec { exec { 'Run a command': command => '/bin/echo `/bin/date` >/tmp/output.txt', } }
The line
exec { 'Run a command':
declares an exec resource with the name 'Run a command'. The name can be anything; it's not otherwise used by Puppet, except that like all resource names it can't be the same as another instance of the same resource type.
echo and date, are specified with their full path. This is because Puppet wants to be sure exactly which command we mean. When Puppet runs, it applies the exec resource by running the command:
command => '/bin/echo `/bin/date` >/tmp/output.txt',
This command will write the following text to /tmp/output.txt:
With the node definition:
# manifests/site.pp node 'puppet-agent' { include user include sudoers include exec }
Run Puppet:
ubuntu@puppet-agent:/etc/puppet$ sudo puppet agent --test Info: Retrieving plugin Info: Caching catalog for puppet-agent.ec2.internal Info: Applying configuration version '1419811309' Notice: /Stage[main]/Exec/Exec[Run a command]/returns: executed successfully Notice: Finished catalog run in 0.11 seconds
Check the output produced:
ubuntu@puppet-agent:/etc/puppet$ cat /tmp/output.txt Mon Dec 29 00:01:59 UTC 2014
As in the previous section, Puppet requires us to specify the full path to any command referenced in an exec resource. However, we can provide a list of paths for Puppet to search for commands, using the path attribute. For example:
class exec { exec { 'Run a command': #command => '/bin/echo `/bin/date` >/tmp/output.txt', command => 'echo `date` >/tmp/output.txt', path => ['/bin', '/usr/bin'], } }
Now when Puppet sees a command name, it will search the directories you specify looking for the matching commands.
If we want to specify a set of default search paths for all exec resources, we can put this in our manifests/site.pp file:
Exec { path => ['/bin', '/usr/bin'], }
Note the capital 'E' for Exec. This means "make this the default for all exec resources." Then we can use unqualified commands without an explicit path attribute. So, the modules/exec/manifests/init.pp now looks like this:
class exec { exec { 'Run a command': command => 'echo `date` >/tmp/output.txt', } }
Puppet
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization