How to use the command 'puppet apply' (with examples)
Puppet is a configuration management tool used to automate the deployment and management of applications, services, and system configurations. The puppet apply
command is a utility that allows users to apply Puppet manifests locally. It is particularly useful for testing Puppet code or deploying configurations on standalone nodes where a Puppet Master server is not available. The command processes Puppet manifests, compiles the code, and enacts the desired system state described in the manifest.
Use case 1: Apply a manifest
Code:
puppet apply path/to/manifest
Motivation:
Using the puppet apply
command to run a manifest file is essential when you want to locally apply configurations to a system without involving a Puppet Master. This is particularly useful in environments where you have standalone servers or during the development and testing phases of creating Puppet manifests. By applying a manifest locally, you can verify the changes and ensure that the configuration described in the manifest works as intended before deploying it into production.
Explanation:
puppet apply
: This initiates the command for applying Puppet code directly on the system.path/to/manifest
: This specifies the path to the manifest file you want to apply. A manifest file usually contains Puppet code which describes how the system should be configured.
Example output:
Notice: Compiled catalog for localhost in environment production in 0.03 seconds
Notice: /Stage[main]/Main/Notify[Hello, Puppet!]/message: defined 'message' as 'Hello, Puppet!'
Notice: Applied catalog in 0.04 seconds
This output indicates that the manifest has been compiled and applied successfully, specifying any resources that were changed or configured.
Use case 2: Execute puppet code
Code:
puppet apply --execute code
Motivation:
Executing Puppet code directly from the command line is particularly beneficial for quick tests, debugging, or performing ad-hoc changes. This approach allows users to run small snippets of Puppet code without the need to write them into a manifest file, thus providing a fast and efficient way to apply quick fixes or test configurations in a controlled environment.
Explanation:
puppet apply
: This is the core command to apply Puppet configurations.--execute
: This option is used to specify that the code to be executed is provided directly in the command line, rather than in a file.code
: This is the actual Puppet code snippet that you want to execute. It needs to be quoted if it contains spaces or special characters.
Example output:
Notice: Compiled catalog for localhost in environment production in 0.02 seconds
Notice: /Stage[main]/Main/Notify[Inline Puppet Code]/message: defined 'message' as 'Inline Puppet Code'
Notice: Applied catalog in 0.03 seconds
The output signifies that the provided Puppet code was successfully executed, showing any changes or messages configured.
Use case 3: Use a specific module and hiera configuration file
Code:
puppet apply --modulepath path/to/directory --hiera_config path/to/file path/to/manifest
Motivation:
This use case is crucial when you want to apply Puppet manifests that depend on additional modules and hiera configurations. By specifying the module path and hiera configuration, you ensure that the Puppet ecosystem has all necessary resources and data needed to apply the configurations accurately. This is essential in complex environments where different configurations and modules are used based on hiera data lookups to customize system setups.
Explanation:
puppet apply
: Again, this signifies the command for applying Puppet configurations.--modulepath path/to/directory
: This option specifies the path where Puppet should look for modules. Modules are reusable bundles of Puppet code and data, which can be used to configure diverse resources.--hiera_config path/to/file
: This indicates the path to the hiera configuration file. Hiera is a key/value lookup tool used by Puppet for externalizing data, which allows for separation between code and data.path/to/manifest
: As previously explained, this specifies the path to the manifest file you intend to execute with the given module path and hiera configuration.
Example output:
Notice: Compiled catalog for localhost in environment production in 0.05 seconds
Info: Applying configuration version '1625244826'
Notice: /Stage[main]/Custom_module/Notify[Configuration Applied]/message: defined 'message' as 'Configuration Applied'
Notice: Applied catalog in 0.07 seconds
This output indicates successful application of the manifest with consideration of modules and hiera configurations, reflecting any changes or applied configurations on the system.
Conclusion:
The puppet apply
command offers versatile use cases for the local execution and testing of Puppet manifests. Whether it’s applying complete manifest files, running Puppet snippets ad-hoc, or utilizing modules and hiera for more complex deployments, puppet apply
serves as a powerful tool for Puppet practitioners aiming for efficient configuration management and automation.