简体   繁体   English

如何从Python构建人偶资源

[英]How to construct a puppet resource from Python

I would like to construct a puppet resource from within Python. 我想从Python内部构造一个人偶资源。 If I had a hash of keys and values, or variables with values, how could this be done? 如果我有键和值的散列,或带有值的变量,该怎么做?

This is a simple example of a puppet resource. 这是一个人偶资源的简单示例。

file { '/etc/passwd':
  owner => root,
  group => root,
  mode  => 644
}

If I had the string /etc/passwd , variable with a value of root , another variable with a value of root , and a variable mode with a value of 644 , how would I generate the above resource from within Python? 如果我有字符串/etc/passwd ,变量的值为root ,另一个变量的值为root ,变量mode的值为644 ,我如何在Python中生成上述资源?

From your comment it seems you just want to be able to output your python objects into puppet manifest format. 从您的评论看来,您似乎只想能够将python对象输出为人偶清单格式。 Since there is not a python package that does this I propose writing your own classes to handle the resource types you need, then overriding the str function so that it outputs the manifest you need. 由于没有执行此操作的python包,因此我建议编写自己的类来处理所需的资源类型,然后重写str函数,使其输出所需的清单。

class fileresource:

    def __init__(self, mfile, owner, group, mode):
        self.mfile = mfile
        self.owner = owner
        self.group = group
        self.mode = mode

    def __str__(self):
        mystring = "file {'" + self.mfile + "':\n"
        mystring += "  owner => " + self.owner + "\n"
        mystring += "  group => " + self.group + "\n"
        mystring += "  mode => " + self.mode + "\n"
        mystring += "}\n"
        return mystring

if __name__ == "__main__":
    myfile = fileresource("/etc/passwd", "root", "root", "0644")
    print myfile

This would be the output: 这将是输出:

$ python fileresource.py 
file {'/etc/passwd':
  owner => root
  group => root
  mode => 0644
}

You could conceivably write an entire package that handles all the different types of puppet resources and use this in your code. 可以想象,您可以编写一个处理所有不同类型的p资源的完整程序包,并在代码中使用它。 Hopefully, this is what you are looking for. 希望这是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM