首页 > 解决方案 > 如何在 ruby​​ 上将 puppet site.pp 转换为 json?

问题描述

我在 puppet 3.8 中有 site.pp 我想将 site.pp 转换为 JSON。

我找到了https://gist.github.com/atdt/3704340/be33cc0d1671dcbe8c4d4ede4cde3e0953546edf

https://www.gavinmogan.com/2012/03/14/output-pretty-json-puppet

但是,我不知道如何将 site.pp 转换为 JSON

$commons                                           = hiera('commons', {})

$post_repo_fqdn                                    = $commons['post_repo_fqdn']

$team_frontend_proxy_exclusions = 'localhost|127.0.0.1|10.2.*|*.dev.local|*.local|*-dev.local|*-test.local'
$use_team_frontend_proxy = true
$team_frontend_proxy_host = '10.2.53.213'
$update_history_job_delay_min = 10
$enot_test_mode = true

$team_frontend_web_client_external_url = 'team_frontend-web.local'
$team_frontend_url='https://team_frontend-web.local/help'
$team_frontend_admin_status_timeout = 30000

$returns_ttl = 120

$team_frontend_servers = {
  rest_api => { server_port => $team_frontend['rest_api']['ports'], size => 20 },
  web_app  => { server_port => $team_frontend['webapp']['ports'],   size => 20 },
}

commons.yaml:

commons:
  post_repo_fqdn                 : test-deploy1

如果运行(https://gist.github.com/atdt/3704340/be33cc0d1671dcbe8c4d4ede4cde3e0953546edf)ruby punch.rb site.pp,然后得到错误:

/usr/share/ruby/vendor_ruby/puppet/resource/type_collection_helper.rb:5:in `known_resource_types': undefined method `known_resource_types' for "":String (NoMethodError)
    from /usr/share/ruby/vendor_ruby/puppet/parser/parser_support.rb:120:in `import'
    from punch.rb:92:in `<main>'

编写测试红宝石脚本:

test.rb 
require 'json'
require 'pp'

data = File.read("/etc/puppet/environments/team_frontend/manifests/site.pp")

pp ap(JSON.parse(data.to_json))

并得到错误

/usr/share/ruby/json/common.rb:155:in `parse': 757: unexpected token at '"$commons                                           = hiera('commons', {})\n\n$post_repo_fqdn                                    = $commons['post_repo_fqdn']\n\n$team_frontend_proxy_exclusions = 'localhost|127.0.0.1|10.2.*|*.dev.local|*.local|*-dev.local|*-test.local'\n$use_team_frontend_proxy = true\n$team_frontend_proxy_host = '10.2.53.213'\n$update_history_job_delay_min = 10\n$enot_test_mode = true\n\n$team_frontend_web_client_external_url = 'team_frontend-web.local'\n$team_frontend_url='https://team_frontend-web.local/help'\n$team_frontend_admin_status_timeout = 30000\n\n$returns_ttl = 120\n\n$team_frontend_servers = {\n  rest_api => { server_port => $team_frontend['rest_api']['ports'], size => 20 },\n  web_app  => { server_port => $team_frontend['webapp']['ports'],   size => 20 },\n}\n\n"' (JSON::ParserError)
    from /usr/share/ruby/json/common.rb:155:in `parse'
    from test.rb:6:in `<main>'

如何将 puppet site.pp 转换为 json?感谢您的评论。我想从 puppet 变量迁移到 consul kv。

谢谢!

标签: jsonrubypuppetconsulconsul-kv

解决方案


推荐阅读