{"id":1565,"date":"2010-07-07T17:55:36","date_gmt":"2010-07-07T16:55:36","guid":{"rendered":"http:\/\/www.devco.net\/?p=1565"},"modified":"2010-07-07T18:07:45","modified_gmt":"2010-07-07T17:07:45","slug":"puppet_resources_on_demand","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2010\/07\/07\/puppet_resources_on_demand.php","title":{"rendered":"Puppet resources on demand with MCollective"},"content":{"rendered":"

Some time ago I wrote how to reuse Puppet providers<\/a> in your Ruby script, I’ll take that a bit further here and show you to create any kind of resource.<\/p>\n

Puppet works based on resources and catalogs. A catalog is a collection of resources and it will apply the catalog to a machine. So in order to do something you can do as before and call the type’s methods directly but if you wanted to build up a resource and say ‘just do it’ then you need to go via a catalog.<\/p>\n

Here’s some code, I don’t know if this is the best way to do it, I dug around the code for ralsh<\/em> to figure this out:<\/p>\n

<\/p>\n

\r\nparams = { :name => \"rip\",\r\n           :comment => \"R.I.Pienaar\",\r\n           :password => '......' }\r\n\r\npup = Puppet::Type.type(:user).new(params)\r\n\r\ncatalog = Puppet::Resource::Catalog.new\r\ncatalog.add_resource pup\r\ncatalog.apply\r\n<\/pre>\n

<\/code><\/p>\n

That’s really simple and doesn’t require you to know much about the inner workings of a type, you’re just mapping the normal Puppet manifest to code and applying it. Nifty.<\/p>\n

The natural progression – to me anyway – is to put this stuff into a MCollective agent<\/a> and build a distributed ralsh<\/em>.<\/p>\n

Here’s a sample use case, I wanted to change my users password everywhere:<\/p>\n

<\/p>\n

\r\n$ mc-rpc puppetral do type=user name=rip password='$1$xxx'\r\n<\/pre>\n

<\/code><\/p>\n

And that will go out, find all my machines and use the Puppet RAL to change my password for me. You can do anything puppet can, manage \/etc\/hosts<\/em>, add users, remove users, packages, services and anything even your own custom types can be used. Distributed and in parallel over any number of hosts.<\/p>\n

Some other examples:<\/p>\n

Add a user:
\n<\/p>\n

\r\n$ mc-rpc puppetral do type=user name=foo comment=\"Foo User\" managehome=true\r\n<\/pre>\n

<\/code><\/p>\n

Run a command using exec, with the magical creates<\/em> option:
\n<\/p>\n

\r\n$ mc-rpc puppetral do type=exec name=\"\/bin\/date > \/tmp\/date\" user=root timeout=5 creates=\"\/tmp\/date\"\r\n<\/pre>\n

<\/code><\/p>\n

Add an aliases entry:<\/p>\n

<\/p>\n

\r\n$ mc-rpc puppetral do type=mailalias name=foo recipient=\"rip@devco.net\" target=\"\/etc\/aliases\"\r\n<\/pre>\n

<\/code><\/p>\n

Install a package:<\/p>\n

<\/p>\n

\r\n$ mc-rpc puppetral do type=package name=unix2dos ensure=present\r\n<\/pre>\n

<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"

Some time ago I wrote how to reuse Puppet providers in your Ruby script, I’ll take that a bit further here and show you to create any kind of resource. Puppet works based on resources and catalogs. A catalog is a collection of resources and it will apply the catalog to a machine. So in […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","footnotes":""},"categories":[7],"tags":[121,78,21,13],"_links":{"self":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1565"}],"collection":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/comments?post=1565"}],"version-history":[{"count":12,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":1577,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/posts\/1565\/revisions\/1577"}],"wp:attachment":[{"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devco.net\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}