LinuxPuppetTutorials

How to use Puppet DSL for Scripting

The Puppet Declarative Scripting Language (DSL) can easily be used as a Script interpreter to perform tasks and manage infrastructure, and even perform small smoke tests before adding the code to larger project and repositories.

Prerequisite task: Have a functioning Puppet agent V4 and above, the latest is V7.1 at the time of writing.

To run Puppet "scripts", the first step is to add the 'puppet apply' , along with the path to the Puppet binary, in the first line script interpreter.

Then, use the native behavior of "node default" to include the scripting class, and run some simple Puppet code.

The file to execute locally, we call it scripting.pp:

> cat scripting.pp
#!/opt/puppetlabs/bin/puppet apply
node default {contain scripting}
class scripting (
  String $file_contents = 'Hello World!',
) {
  file { '/tmp/directory':
    ensure => directory
  }
  file { '/tmp/directory/file':
    content => $file_contents,
    require => File['/tmp/directory']
  }
}

Next, mark the file as executable and run it:

> chmod u+x scripting.pp
> ./scripting.pp
Notice: Compiled catalog for server.local.domain in environment production in 0.04 seconds
Notice: /Stage[main]/Scripting/File[/tmp/directory]/ensure: created
Notice: /Stage[main]/Scripting/File[/tmp/directory/file]/ensure: defined content as '{sha256}03ba204e50d126e4674c015e04d82e84c21366780af1f43bd54a37816b6ab340'
Notice: Applied catalog in 0.08 seconds

That's it! Happy scripting in Puppet DSL!

Puppet DSL scripting
Puppet scripts can also be executed locally, using Puppet DSL!

Grant Davies
Grant is a kick-ass systems engineer from Australia.

    Leave a reply

    Your email address will not be published.

    More in:Linux