Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppet: How can I wrap a command into two line if >80 characters?

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?.......",
  }
like image 909
TheOneTeam Avatar asked Jul 10 '12 03:07

TheOneTeam


People also ask

How do I move multiple lines in Yaml?

Use "..." if you need to split lines in the middle of words or want to literally type linebreaks as \n : key: "Antidisestab\ lishmentarianism. \n\nGet on it." YAML is crazy.

How do I add a new line in Yaml?

If you would like them to be kept as newlines, use the literal style, indicated by a pipe ( | ). If instead you want them to be replaced by spaces, use the folded style, indicated by a right angle bracket ( > ). (To get a newline using the folded style, leave a blank line by putting two newlines in.


4 Answers

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

like image 116
pwan Avatar answered Oct 16 '22 18:10

pwan


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
}
like image 24
Jon Avatar answered Oct 16 '22 19:10

Jon


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

like image 20
mccartjm Avatar answered Oct 16 '22 20:10

mccartjm


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

like image 4
KrisBuytaert Avatar answered Oct 16 '22 20:10

KrisBuytaert