A collection of task oriented solutions in Puppet

 

Adhoc Puppetry with puppet apply execute

Challenge

Sometimes you want to try out a tiny snippet of puppet code, maybe to see what a property does to a file or check if the file type takes owner or user as a parameter. The puppet apply command, with the -e option allows you to run these kind of small snippets, often without requiring root.

Solution

$ puppet apply -e 'file { "/tmp/adhoc": content => "Written from puppet on ${::hostname}\n" }'
notice: /Stage[main]//File[/tmp/adhoc]/ensure: defined content as '{md5}c1047ebc91c191f0ef6ad5fedcc5a0df'

$ cat /tmp/adhoc
Written from puppet

$ ls -al /tmp/adhoc
-rw-r--r--. 1 deanw deanw 20 Dec 30 13:59 /tmp/adhoc

$ puppet apply -e 'file { "/tmp/adhoc": content => "Written from puppet\n", mode => "0640" }'
notice: /Stage[main]//File[/tmp/adhoc]/mode: mode changed '0644' to '0640'

$ ls -al /tmp/adhoc
-rw-r-----. 1 deanw deanw 20 Dec 30 13:59 /tmp/adhoc

Explanation

This is a simple way to allow easy testing of small puppet snippets while you become more familiar with the configuration language. As this is a full instance of puppet you can use facts in your snippets but when you find yourself going over a line or two it may well be time to investigate stand alone module testing.