29

In puppet, if define command is > 80 characters, how can I wrap into two line to do it?

  exec { 'create_domain':
    command => "some command exceed 80 character...........................................................how to do how to do?.......",
  }
TheOneTeam
  • 25,806
  • 45
  • 116
  • 158

4 Answers4

22

It's sort of ugly, but if the last character in a string is a \ followed by a newline, then the string is continued on the next line. My sample.pp manifest is below:

exec { 'wrapped_string_example':
  command => "/bin/echo 12345678901234567890123456789012345678901234567890\
wrapped > /var/tmp/test.txt";
}

Running this with puppet apply sample.pp gives the following output

$ puppet apply sample.pp
notice: /Stage[main]/Exec[wrapped_string_example]/returns: executed successfully
notice: Finished catalog run in 0.10 seconds

And catting the created file shows the lines have wrapped:

$ cat /var/tmp/test.txt 
12345678901234567890123456789012345678901234567890wrapped

See https://github.com/puppetlabs/puppet/blob/9fbb36de/lib/puppet/parser/lexer.rb#L537 (as of Puppet v2.7.0)

Also this is sort of a known issue: http://projects.puppetlabs.com/issues/5022

Jon
  • 3,573
  • 2
  • 17
  • 24
pwan
  • 2,894
  • 2
  • 24
  • 38
  • gvim only with ruby syntax highlight – TheOneTeam Jul 10 '12 at 16:54
  • 1
    If spaces in the middle of the command don't matter then you can still indent the next line to the "correct" column in the continued command after the backslash... – David Gardner Oct 06 '16 at 14:39
  • Not really an answer to the question, but you can alternatively ignore the puppet-lint complaints locally using `# lint:ignore:140chars`, as described at http://puppet-lint.com/controlcomments/ – Peterino May 30 '17 at 08:52
9

For big chunks of data, heredocs are the best way of dealing with long lines in Puppet manifests. The /L interpolation option is particularly useful. /L causes \ at the end of a line to remove newlines. For example, the following does what you'd expect, stripping indentation and newlines, including the trailing newline.

  sshkey { 'example.com':
    ensure  => present,
    type    => 'ssh-rsa',
    key     => @(KEY/L),
      RfrXBrU1T6qMNllnhXsJdaud9yBgWWm6OprdEQ3rpkTvCc9kJKH0k8MNfKxeBiGZVsUn435q\
      e83opnamtGBz17gUOrzjfmpRuBaDDGmGGTPcO8Dohwz1zYuir93bJmxkNldjogbjAWPfrX10\
      8aoDw26K12sK61lOt6GTdR9yjDPdG4zL5G3ZjXCuDyQ6mzcNHdAPPFRQdlRRyCtG2sQWpWan\
      3AlYe6h6bG48thlo6vyNvOD8s9K0YBnwl596DJiNCY6EsxnSAhA3Uf9jeKqlVqqrxhEzHufx\
      07iP1nXIXCMUV
      |-KEY
    target  => '/home/user/.ssh/authorized_keys',
  }

Or to keep the final newline, leave out the - before the end text:

exec { 'create_domain':
  command => @(CMD/L),
    /bin/echo 123456789012345678901234567890123456789012345678901234567890123456\
    wrapped > /var/tmp/test.txt
    | CMD
}
Jon
  • 3,573
  • 2
  • 17
  • 24
7

As of Puppet 3.5 you have a couple of options that i have used. Ruby allows you to concat strings over a couple of lines.

string = "line #1"\
         "line #2"\
         "line #3"

p string # => "line #1line #2line #3"

Another option, as of Puppet 3.5 they have added HereDoc functionality. This will allow you to put the string in a section of a source code file that is treated as if it were a separate file.

$mytext = @(EOT)
    This block of text is
    visibly separated from
    everything around it.
    | EOT

The puppet documentation is here: https://docs.puppet.com/puppet/4.9/lang_data_string.html#heredocs

mccartjm
  • 121
  • 1
  • 5
  • 2
    Bear in mind that the HereDoc sample you provided keeps the line breaks. The question is asking about splitting a command that belongs on one line over several lines in manifest. – Steztric Oct 31 '18 at 04:24
5

If you really care about the 80cols limit you can always abuse a template to achieve that goal

exec {'VeryLongExec':
    command => template("${module}/verylongexec")
 }

Then put the actual command in that template file

Credits should go to Jan Vansteenkiste to figure