简体   繁体   中英

How to construct a puppet resource from Python

I would like to construct a puppet resource from within 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?

From your comment it seems you just want to be able to output your python objects into puppet manifest format. 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.

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. Hopefully, this is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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