在 Puppet 中解析 JSON 字符串

3

我正在尝试在Puppet清单中解析一个非常简单的JSON文件,但是我遇到了困难。

以下示例Puppet清单有效,但它仅打印JSON文件中的条目。

include stdlib
$hash = loadjson('/tmp/file.json')
notify("$hash")

JSON文件

{
  "output": {
    "message": "This is the entire value",
    "return_value": "0"
  }
}

我希望能够将"message"分配给变量"$message"并将"return_value"分配给变量"$return_value"


1
你不需要包含stdlib,因为你正在使用其中的自定义函数,它在插件同步期间会自动加载。实际上,在这里包含它是没有任何作用的。 - Matt Schuchard
1个回答

3
您要写的内容是:

您需要编写:

  $hash = loadjson('/tmp/file.json')
  $message      = $hash['output']['message']
  $return_value = $hash['output']['return_value']
  notice("$message, $return_value")

更简洁地说:
  $hash = loadjson('/tmp/file.json')
  [$message, $return_value] = $hash['output']
  notice("$message, $return_value")

正如上面的评论所提到的,这里实际上不需要包含 include stdlib

非常感谢。loadjson和parsejson有什么区别?我在哪里可以找到这些的额外文档? - user1074593
1
这两个文档都在 https://github.com/puppetlabs/puppetlabs-stdlib 上记录。看起来其中一个期望的是 JSON 字符串,另一个则是一个 JSON 文件。 - Alex Harvey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接