How to use the command 'puppet apply' (with examples)
The ‘puppet apply’ command allows you to apply Puppet manifests locally. It is used to execute Puppet code or scripts and manage the resources defined in the specified manifests.
Use case 1: Apply a manifest
Code:
puppet apply path/to/manifest
Motivation: Applying a manifest is the most common use case of the ‘puppet apply’ command. You can use this command to apply a specific Puppet manifest file, which contains the desired configuration for your system.
Explanation:
puppet apply
: The command to execute Puppet code.path/to/manifest
: The path to the Puppet manifest file that you want to apply.
Example Output:
Notice: Compiled catalog for hostname in environment production in 0.12 seconds
Notice: Applied catalog in 0.65 seconds
Use case 2: Execute puppet code
Code:
puppet apply --execute code
Motivation: There might be cases where you want to execute Puppet code without using a separate manifest file. This can be useful for testing or running one-time tasks.
Explanation:
puppet apply
: The command to execute Puppet code.--execute code
: Specifies the Puppet code that you want to execute directly.
Example Output:
Notice: Scope(Class[main]): Executing puppet code directly!
Notice: Compiled catalog for hostname in environment production in 0.45 seconds
Notice: Applied catalog in 0.60 seconds
Use case 3: Use a specific module and hiera config file
Code:
puppet apply --modulepath path/to/directory --hiera_config path/to/file path/to/manifest
Motivation: In larger Puppet environments, it is common to use modules and Hiera for configuration and data management. This use case allows you to specify a custom module path and Hiera config file for your manifests.
Explanation:
puppet apply
: The command to execute Puppet code.--modulepath path/to/directory
: Specifies the directory containing the Puppet modules used by the manifests.--hiera_config path/to/file
: Specifies the Hiera config file to use for data lookup.path/to/manifest
: The path to the Puppet manifest file that you want to apply.
Example Output:
Notice: Compiled catalog for hostname in environment production in 0.55 seconds
Notice: Applied catalog in 0.78 seconds
Conclusion:
The ‘puppet apply’ command is a versatile tool for executing Puppet code locally. It allows you to apply manifests, execute code directly, and configure module and Hiera paths. By using this command, you can manage your system’s configuration and resources efficiently with Puppet.