用python解析puppetapi-yaml

2024-06-26 01:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个脚本,它需要解析puppet输出的yaml输出。在

当我再次提出请求时示例https://puppet:8140/生产/目录/我的.testserver.no我要买些山药回来,看起来像:

--- &id001 !ruby/object:Puppet::Resource::Catalog
  aliases: {}
  applying: false
  classes: 
    - s_baseconfig
    ...
  edges: 
    - &id111 !ruby/object:Puppet::Relationship
      source: &id047 !ruby/object:Puppet::Resource
        catalog: *id001
        exported: 

等等。。。问题是当我山药负荷(yamlstream),我会得到一个错误,比如:

^{pr2}$

据我所知,yaml支持这一&id001部分。在

有什么办法吗?我可以告诉yaml解析器忽略它们吗? 我只需要从yaml流中提取几行,也许regex是我的朋友? 以前有人做过yaml清理正则表达式吗?在

可以使用curl获得yaml输出,如下所示:

curl --cert /var/lib/puppet/ssl/certs/$(hostname).pem --key /var/lib/puppet/ssl/private_keys/$(hostname).pem --cacert /var/lib/puppet/ssl/certs/ca.pem -H 'Accept: yaml' https://puppet:8140/production/catalog/$(hostname)

我在puppet邮件列表@http://www.mail-archive.com/puppet-users@googlegroups.com/msg24143.html中也找到了一些关于这个的信息。但我不能让它正常工作。。。在


Tags: httpssslyamlobjectvarlibpemresource
3条回答

我只需要上课部分。所以我最后创建了一个小python函数来剥离它。。。在

希望对某人有用:)

#!/usr/bin/env python

import re

def getSingleYamlClass(className, yamlList):
    printGroup = False
    groupIndent = 0
    firstInGroup = False
    output = ''

    for line in yamlList:
        # Count how many spaces in the beginning of our line
        spaceCount = len(re.findall(r'^[ ]*', line)[0])
        cleanLine = line.strip()

        if cleanLine == className:
            printGroup = True
            groupIndent = spaceCount
            firstInGroup = True

        if printGroup and (spaceCount > groupIndent) or firstInGroup:
            # Strip away the X amount of spaces for this group, so we get valid yaml
            output += re.sub(r'^[ ]{%s}' % groupIndent, '', line) + '\n'
            firstInGroup = False # Reset this
        else:
            # End of our group, reset
            groupIndent = 0
            printGroup = False

    return output

getSingleYamlClass('classes:', open('puppet.yaml').readlines())

我已经给PyYAML的创建者kirillsimonov发了电子邮件,请求帮助解析Puppet YAML文件。在

他很乐意为下面的代码提供帮助。这段代码是用来解析Puppet日志的,但是我确信您可以修改它来解析其他Puppet YAML文件。在

我们的想法是为Ruby对象创建正确的加载程序,然后PyYAML可以读取数据。在

接下来是:

#!/usr/bin/env python

import yaml

def construct_ruby_object(loader, suffix, node):
    return loader.construct_yaml_map(node)

def construct_ruby_sym(loader, node):
    return loader.construct_yaml_str(node)

yaml.add_multi_constructor(u"!ruby/object:", construct_ruby_object)
yaml.add_constructor(u"!ruby/sym", construct_ruby_sym)


stream = file('201203130939.yaml','r')
mydata = yaml.load(stream)
print mydata

我相信问题的关键是puppet在rubyfu中使用yaml“tags”,这让默认的python加载程序感到困惑。尤其是,PyYAML不知道如何构造ruby/对象:木偶*Resource::Catalog,这很有意义,因为这是一个ruby对象。在

下面是一个链接,显示了yaml标记的各种用法:http://www.yaml.org/spec/1.2/spec.html#id2761292

我用蛮力的方法克服了这一点,只需做一些类似的事情:

cat the_yaml | sed 's#\!ruby/object.*$##gm' > cleaner.yaml

但是现在我遇到了一个问题,*resource_table*块将PyYAML与其复杂键混淆(使用“?”表示一个复杂键的开始。在

如果你能找到一个好办法,请告诉我。。。但是考虑到puppet与ruby之间的联系,直接用ruby编写脚本可能会更容易些。在

相关问题 更多 >