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

生来就可爱ヽ(ⅴ<●) 提交于 2020-07-18 08:39:45

问题


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?.......",
  }

回答1:


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:

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

node 'pwan-central' {
        include test
}

Running this with 'puppet apply sample.pp' on a Ubuntu 11.10 with Puppet 2.7.1 gives the following output

mrpwan@pwan-central:~$ puppet apply sample.pp
notice: /Stage[main]/Test/Exec[create_domain]/returns: executed successfully
notice: Finished catalog run in 0.10 seconds

And catting the created file shows the lines have wrapped:

mrpwan@pwan-central:~$ cat /var/tmp/test.txt 
1234567890123456789012345678901234567890123456789012345678901234567890wrapped

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




回答2:


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/known_hosts',
  }

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

class test {
  exec { 'create_domain':
    command => @(CMD/L),
      /bin/echo 123456789012345678901234567890123456789012345678901234567890123456\
      wrapped > /var/tmp/test.txt
      | CMD
  }
}



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/11406234/puppet-how-can-i-wrap-a-command-into-two-line-if-80-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!