How to construct a puppet resource from Python
<p>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?</p>

<p>This is a simple example of a puppet resource.</p>

<pre><code>file { '/etc/passwd':
  owner =&gt; root,
  group =&gt; root,
  mode  =&gt; 644
}
</code></pre>

<p>If I had the string <code>/etc/passwd</code>, variable with a value of <code>root</code>, another variable with a value of <code>root</code>, and a variable <code>mode</code> with a value of <code>644</code>, how would I generate the above resource from within Python?</p>

<p>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 <strong>str</strong> function so that it outputs the manifest you need.</p>

<pre><code>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 =&gt; " + self.owner + "\n"
        mystring += "  group =&gt; " + self.group + "\n"
        mystring += "  mode =&gt; " + self.mode + "\n"
        mystring += "}\n"
        return mystring

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

<p>This would be the output:</p>

<pre><code>$ python fileresource.py 
file {'/etc/passwd':
  owner =&gt; root
  group =&gt; root
  mode =&gt; 0644
}
</code></pre>

<p>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.</p>

python puppet