NOTE: This page was generated from the Puppet source code on 2021-01-22 10:27:38 +0000
Executes external commands.
Any command in an exec
resource must be able to run multiple times
without causing harm — that is, it must be idempotent. There are three
main ways for an exec to be idempotent:
apt-get update
.)onlyif
, unless
, or creates
attribute, which prevents
Puppet from running the command unless some condition is met.refreshonly => true
, which allows Puppet to run the
command only when some other resource is changed. (See the notes on refreshing
below.)The state managed by an exec
resource represents whether the specified command
needs to be executed during the catalog run. The target state is always that
the command does not need to be executed. If the initial state is that the
command does need to be executed, then successfully executing the command
transitions it to the target state.
The unless
, onlyif
, and creates
properties check the initial state of the
resource. If one or more of these properties is specified, the exec might not
need to run. If the exec does not need to run, then the system is already in
the target state. In such cases, the exec is considered successful without
actually executing its command.
A caution: There’s a widespread tendency to use collections of execs to manage resources that aren’t covered by an existing resource type. This works fine for simple tasks, but once your exec pile gets complex enough that you really have to think to understand what’s happening, you should consider developing a custom resource type instead, as it is much more predictable and maintainable.
Duplication: Even though command
is the namevar, Puppet allows
multiple exec
resources with the same command
value.
Refresh: exec
resources can respond to refresh events (via
notify
, subscribe
, or the ~>
arrow). The refresh behavior of execs
is non-standard, and can be affected by the refresh
and
refreshonly
attributes:
refreshonly
is set to true, the exec runs only when it receives an
event. This is the most reliable way to use refresh with execs.onlyif
, unless
, or creates
condition
is no longer met after the first run, the second run does not occur.refresh
command, and receives an
event, it runs its normal command. Then, if any onlyif
, unless
, or creates
conditions are still met, the exec runs its refresh
command.onlyif
, unless
, or creates
attribute that prevents it
from running, and it then receives an event, it still will not run.noop => true
, would otherwise have run, and receives
an event from a non-noop resource, it runs once. However, if it has a refresh
command, it runs that instead of its normal command.In short: If there’s a possibility of your exec receiving refresh events, it is extremely important to make sure the run conditions are restricted.
Autorequires: If Puppet is managing an exec’s cwd or the executable file used in an exec’s command, the exec resource autorequires those files. If Puppet is managing the user that an exec should run as, the exec resource autorequires that user.
exec { 'resource title':
command => # (namevar) The actual command to execute. Must either be...
cwd => # The directory from which to run the command. If
environment => # An array of any additional environment variables
group => # The group to run the command as. This seems to...
logoutput => # Whether to log command output in addition to...
path => # The search path used for command execution...
provider => # The specific backend to use for this `exec...
refresh => # An alternate command to run when the `exec...
returns => # The expected exit code(s). An error will be...
timeout => # The maximum time the command should take. If...
tries => # The number of times execution of the command...
try_sleep => # The time to sleep in seconds between 'tries'....
umask => # Sets the umask to be used while executing this...
user => # The user to run the command as. > **Note:*...
# ...plus any applicable metaparameters.
}
(Namevar: If omitted, this attribute’s value defaults to the resource’s title.)
The actual command to execute. Must either be fully qualified
or a search path for the command must be provided. If the command
succeeds, any output produced will be logged at the instance’s
normal log level (usually notice
), but if the command fails
(meaning its return code does not match the specified code) then
any output is logged at the err
log level.
Multiple exec
resources can use the same command
value; Puppet
only uses the resource title to ensure exec
s are unique.
The directory from which to run the command. If this directory does not exist, the command will fail.
An array of any additional environment variables you want to set for a
command, such as [ 'HOME=/root', 'MAIL=root@example.com']
.
Note that if you use this to set PATH, it will override the path
attribute. Multiple environment variables should be specified as an
array.
The group to run the command as. This seems to work quite haphazardly on different platforms – it is a platform issue not a Ruby or Puppet one, since the same variety exists when running commands as different users in the shell.
Whether to log command output in addition to logging the
exit code. Defaults to on_failure
, which only logs the output
when the command has an exit code that does not match any value
specified by the returns
attribute. As with any resource type,
the log level can be controlled with the loglevel
metaparameter.
Default: on_failure
Allowed values:
true
false
on_failure
The search path used for command execution. Commands must be fully qualified if no path is specified. Paths can be specified as an array or as a seperated list.
The specific backend to use for this exec
resource. You will seldom need to specify this — Puppet will usually discover the appropriate provider for your platform.
Available providers are:
An alternate command to run when the exec
receives a refresh event
from another resource. By default, Puppet runs the main command again.
For more details, see the notes about refresh behavior above, in the
description for this resource type.
Note that this alternate command runs with the same provider
, path
,
user
, and group
as the main command. If the path
isn’t set, you
must fully qualify the command’s name.
(Property: This attribute represents concrete state on the target system.)
The expected exit code(s). An error will be returned if the executed command has some other exit code. Can be specified as an array of acceptable exit codes or a single value.
On POSIX systems, exit codes are always integers between 0 and 255.
On Windows, most exit codes should be integers between 0 and 2147483647.
Larger exit codes on Windows can behave inconsistently across different tools. The Win32 APIs define exit codes as 32-bit unsigned integers, but both the cmd.exe shell and the .NET runtime cast them to signed integers. This means some tools will report negative numbers for exit codes above 2147483647. (For example, cmd.exe reports 4294967295 as -1.) Since Puppet uses the plain Win32 APIs, it will report the very large number instead of the negative number, which might not be what you expect if you got the exit code from a cmd.exe session.
Microsoft recommends against using negative/very large exit codes, and you should avoid them when possible. To convert a negative exit code to the positive one Puppet will use, add it to 4294967296.
Default: 0
The maximum time the command should take. If the command takes longer than the timeout, the command is considered to have failed and will be stopped. The timeout is specified in seconds. The default timeout is 300 seconds and you can set it to 0 to disable the timeout.
Default: 300
The number of times execution of the command should be tried. This many attempts will be made to execute the command until an acceptable return code is returned. Note that the timeout parameter applies to each try rather than to the complete set of tries.
Default: 1
The time to sleep in seconds between ‘tries’.
Default: 0
Sets the umask to be used while executing this command
The user to run the command as.
Note: Puppet cannot execute commands as other users on Windows.
Note that if you use this attribute, any error output is not captured due to a bug within Ruby. If you use Puppet to create this user, the exec automatically requires the user, as long as it is specified by name.
The $HOME environment variable is not automatically set when using this attribute.
Executes external binaries directly, without passing through a shell or performing any interpolation. This is a safer and more predictable way to execute most commands, but prevents the use of globbing and shell built-ins (including control logic like “for” and “if” statements).
feature == posix
["feature", "posix"] ==
Passes the provided command through /bin/sh
; only available on
POSIX systems. This allows the use of shell globbing and built-ins, and
does not require that the path to a command be fully-qualified. Although
this can be more convenient than the posix
provider, it also means that
you need to be more careful with escaping; as ever, with great power comes
etc. etc.
This provider closely resembles the behavior of the exec
type
in Puppet 0.25.x.
feature == posix
Execute external binaries on Windows systems. As with the posix
provider, this provider directly calls the command with the arguments
given, without passing it through a shell or performing any interpolation.
To use shell built-ins — that is, to emulate the shell
provider on
Windows — a command must explicitly invoke the shell:
exec {'echo foo':
command => 'cmd.exe /c echo "foo"',
}
If no extension is specified for a command, Windows will use the PATHEXT
environment variable to locate the executable.
Note on PowerShell scripts: PowerShell’s default restricted
execution policy doesn’t allow it to run saved scripts. To run PowerShell
scripts, specify the remotesigned
execution policy as part of the
command:
exec { 'test':
path => 'C:/Windows/System32/WindowsPowerShell/v1.0',
command => 'powershell -executionpolicy remotesigned -file C:/test.ps1',
}
operatingsystem == windows
["operatingsystem", "windows"] ==
NOTE: This page was generated from the Puppet source code on 2021-01-22 10:27:38 +0000