multi-line content= construct for puppet resource file command

12,036 views
Skip to first unread message

Robert Citek

unread,
Mar 6, 2013, 1:49:44 AM3/6/13
to puppet-users
Hello all,

How does one enter multi-line content using 'puppet resource file ...'
at the command line?

For example, I am trying to create a file called /tmp/hw.txt with two
lines of content:

$ cat /tmp/hw.txt
hello
world

This does not work:

$ puppet resource file hello_world \
path=/tmp/hw.txt \
ensure=file \
content="hello\nworld\n"

This does, but use "puppet apply" :

cat <<"eof" | puppet apply
file { "hello_world":
path => "/tmp/hw.txt",
ensure => "file",
content => "hello\nworld\n",
}
eof

Does anyone have any pointers on how to construct the content= line so
that I can get two lines of text?

Regards,
- Robert

Felix Frank

unread,
Mar 12, 2013, 8:36:04 AM3/12/13
to puppet...@googlegroups.com
Hi,

at first glance, it doesn't seem to be possible.

I had never thought of using puppet resource in this way. Is there a
reason why you prefer it over puppet apply?

Robert Citek

unread,
Mar 12, 2013, 3:55:22 PM3/12/13
to puppet...@googlegroups.com
Just preference and consistency. We've been using puppet as a
substitute for shell commands that use the 'command [options]
arguments ... ' pattern and 'puppet resource ...' matches that
pattern nicely. But if by using here-documents we can match the
pattern pretty closely and we gain the additional functionality, we'll
go that way.

Using 'puppet apply' with a here-document ....

puppet apply <<"eof"
file { "hello_world":
path => "/tmp/hw.txt",
ensure => "file",
content => "hello\nworld\n",
}
eof

... looks pretty close to using arguments to 'puppet resource' ...

$ puppet resource \
file hello_world \
path=/tmp/hw.txt \
ensure=file \
content="hello\nworld\n"

... but actually works. :)

Regards,
- Robert
> --
> 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 puppet-users...@googlegroups.com.
> To post to this group, send email to puppet...@googlegroups.com.
> Visit this group at http://groups.google.com/group/puppet-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Massimiliano Adamo

unread,
Sep 22, 2015, 9:05:00 AM9/22/15
to Puppet Users


Il giorno mercoledì 6 marzo 2013 07:49:44 UTC+1, Robert Citek ha scritto:
Hello all,

How does one enter multi-line content using 'puppet resource file ...'
at the command line?

For example, I am trying to create a file called /tmp/hw.txt with two
lines of content:

$ cat /tmp/hw.txt
hello
world

This does not work:

$ puppet resource file hello_world \
  path=/tmp/hw.txt \
  ensure=file \
  content="hello\nworld\n"

one of the following will work (or either both):
content => inline_template('<%= "hello" + "\n" + "world" %>'),
content => inline_template('<%= "hello\nworld" %>'),
 

jcbollinger

unread,
Sep 23, 2015, 9:16:55 AM9/23/15
to Puppet Users
You did not say exactly what "does not work" means, but I suppose the problem is that you get literal "\n" in the file instead of newlines.  The shell will not convert those when you execute the specific command you presented, and I guess Puppet expects to receive a literal value.  In that case, this is more a shell problem than a Puppet problem, and probably it can be solved via the shell.  Details vary depending on which shell you are using, but there are multiple possibilities, among them:

1. Include literal newlines in your parameter instead of "\n".  In bash, at least, this works as long as the newlines are quoted.  For example:

$ puppet resource file hello_world \
path
=/tmp/hw.txt \
ensure=file \
content
=
"hello
world
"

2. Use the shell's form for C-style escapes.  In bash, that would be:

$ puppet resource file hello_world \
path
=/tmp/hw.txt \
ensure=file \

content
=$'hello\nworld\n'

3. Obtain the wanted value as the output of a command.  In bash, that would be:

$ puppet resource file hello_world \
path
=/tmp/hw.txt \
ensure=file \

content
="`echo -e 'hello\nworld'`"

(By itself, however, this last alternative cannot produce a trailing newline in the content, as shell command substitution eats those.)


John

Reply all
Reply to author
Forward
0 new messages