On Thursday, August 1, 2013 2:37:03 PM UTC-5, Jacob McCoy Wade wrote:
>
> I'm looking for some help in getting the proper escape sequence within the
> generate function.
> Ultimately what I'm trying to do is generate some random strings, store
> them as variables, and use those variables to populate configuration files
> stored as templates.
> I can get this to work when applying the template on the localhost, but it
> fails when applied via the puppet master.:
>
>> $random_1 = generate("/bin/sh", "-c", "/bin/cat /dev/urandom | /usr/bin/
>> tr -dc 'a-z0-9' | /usr/bin/fold -w 8 | /usr/bin/head -n 1 | /usr/bin/tr-d
>> '\n'"
>> )
>
>
> The closest I've come is to use:
>
>> $random_1 = generate('/bin/sh', '-c', '\"/bin/cat /dev/urandom | /usr
>> /bin/tr -dc \'a-z0-9\' | /usr/bin/fold -w 8 | /usr/bin/head -n 1 | /usr
>> /bin/tr -d \'\n\'\"')
>
>
> This however ends up populating the variable with the following and
> appears to ignore the the "/usr/bin/tr -d '\n'" as there are line breaks
> inserted:
>
>> /bin/sh: "/bin/cat: No such file or directory
>>
>
>
So, that's the wrong thing to do, because 'cat' will keep reading its input
until it reaches the end, which it never will in this case. It may be that
having 'head' in the pipeline would rescue it by closing its own input, but
you might end up with a bunch of stalled, orphaned processes. You should
instead use /bin/dd or some other command that allows you to limit the
amount of data read from /dev/urandom.
But that brings us to the next problem: The output you see on the master
appears to be telling you that the file you are trying to cat
(/dev/urandom) does not exist there. Possibly it's there, but it or its
parent directory is not readable by Puppet. Unless you can sort that out,
you will need an altogether different approach.
> What I'm really wanting (I think) to be executed on the puppet master is
>
>> "/bin/sh" "-c" "/bin/cat /dev/urandom | /usr/bin/tr -dc 'a-z0-9' |
>> /usr/bin/fold -w 8 | /usr/bin/head -n 1 | /usr/bin/tr -d '\n'"
>
>
> Any help would be appreciated.
>
There is also a third, more fundamental problem: it is unlikely that you
really want to do what you are trying to do. If you generate a completely
random component to a config file on every run, then that file will never
match from one run to the next, so it will always be re-synced. That is
rarely the intent. More often, you want different, random strings for each
node, but you want those strings to be stable from run to run for any given
node. One way to achieve that might be to construct strings as the
concatenation of a constant stem and a random number generated via Puppet's
built-in fqdn_rand() function.
Even if that would not be sufficient, I have trouble seeing why it makes
sense to use generate() for this purpose. It would be not only
lighter-weight but also more flexible to use Ruby's rand() function via
Puppet's inline_template() function. You could even use fqdn_rand() to
seed the Ruby RNG, so as to get a broader range of consistent pseudorandom
values. And, you would have as much control as you want over the random
data -- such as, for instance, ensuring that it does not contain null (0)
bytes.
As for the question in your title, to insert the literal two-character
sequence '\n' into a double-quoted Puppet string, escape the backslash by
doubling it: "... /usr/bin/tr -d '\\n'". (See
http://docs.puppetlabs.com/puppet/3/reference/lang_datatypes.html#strings.)
Note that only the outermost quotes affect what escape sequences are
recognized; the inner single quotes in your string are just ordinary
characters to Puppet.
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.