大网数据广告活动
服务器运维问答

Puppet Split function fails

· 52CE 我爱测

Problem Puppet split function fails since Puppet has been upgraded puppet to version 3.5.1 , while this worked until 3.4.3 Code 1 $ports_regex = "80\\|443" 2 3 $ports_string = regsubst($ports_regex, '\\\|', ',', 'G') 4 5 notify{"…

问题原文

原帖链接 · 030 · CC BY-SA 3.0

Problem

Puppet split function fails since Puppet has been upgraded puppet to version 3.5.1, while this worked until 3.4.3

Code

 1  $ports_regex = "80\\|443"
 2
 3  $ports_string       = regsubst($ports_regex, '\\\|', ',', 'G')
 4
 5  notify{"CP1 ${ports_string}":}
 6
 7  $ports_array = split($ports_string, ',')
 8
 9  notify{"CP2 ${ports_array}":}
10
11  each($ports_array) |$port| {
12      notify{"CP3 ${port}":}
13
14      file {"${port}":
15          path => "c:/temp/puppettest/${port}",
16          ensure => present;
17      }
18  }

Outcome

3.4.3

Notice: /Stage[main]/Main/File[443]/ensure: created
Notice: CP2 80443
Notice: /Stage[main]/Main/Notify[CP2 80443]/message: defined 'message' as 'CP2 80443'
Notice: CP1 80,443
Notice: /Stage[main]/Main/Notify[CP1 80,443]/message: defined 'message' as 'CP1 80,443'
Notice: CP3 80
Notice: /Stage[main]/Main/Notify[CP3 80]/message: defined 'message' as 'CP3 80'
Notice: /Stage[main]/Main/File[80]/ensure: created
Notice: CP3 443
Notice: /Stage[main]/Main/Notify[CP3 443]/message: defined 'message' as 'CP3 443'

3.5.1

C:\Windows\system32>puppet apply --parser future --debug --verbose "C:\temp\puppettest\site.pp"
Error: Invalid tag cp2  at C:/temp/puppettest/site.pp:9 on node ...
Error: Invalid tag cp2  at C:/temp/puppettest/site.pp:9 on node ...

Question

How to solve this issue?

已采纳解答

原帖链接 · 030 · CC BY-SA 4.0

Ticket closed and marked as "won't fix", see https://tickets.puppetlabs.com/browse/PUP-2612

Henrik Lindberg added a comment - 2014/05/19 3:43 PM - edited

Ah, the next issue is that interpolation of hashes and arrays produce their source equivalent text where it used to just run all the content together.

You can see that if you compare the following with the current and future parsers:

$a = [1,2,3]

notice $a

notice "$a"

I am not sure what you are trying to achieve here - the original seems to have created "CP 2 80443", and you now get "CP2 [80, 443]" which it then barfs on. Then when you iterate of $ports_array, you will get a $port that is first 80 and then 443 (when the regexp has been corrected with 6 backslashes). This means you put 2 notify in the catalog; "CP3 80", and then "CP3 443", and then creates 2 file resources, one titled "80", and then one titled "443".

The Resource implementation is responsible for the interpretation of the title string, and it does odd things when facing punctuation, spaces, numbers and brackets.

Tip is to use notice to get output with intermediate results. I used this for the first part:

$ports_regex = "80\|443"

$ports_string = regsubst($ports_regex, '\\\|', ',', 'G')

notice "ports_regex = '${ports_regex}'"

notice "ports_string = '${ports_string}'"

$ports_array = split($ports_string, ',')

notice "ports_array = '${ports_array}'"

$a = "CP1 ${ports_string}"

notice $a

notify{$a:}

$b = "CP2 ${ports_array}"

notice $b

notify{$b:}

If you want the old behavior of simply running all the content of the array together into one string without any separators, you should use the join function in stdlib (i.e. join($ports_array, ''), but then you could just do that with a separate substitute of the commas.

You can also do this in a different order - something like:

$ports_array = $ports_regex.split('\\\|')

$ports_string = $ports_array.join('')

The first splits the string into an array, and the second constructs a string with the numbers joined without punctuation and brackets. You can then use that string to interpolate into the titles starting with CP.

Or if you do not want to type so many backslashes, you can use a regexp directly:

$ports_array = $ports_regex.split(/\|/)

$ports_string = $ports_array.join('')

Hope that helps

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-04-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。