├── README.md ├── classes ├── classes1.pp ├── classes1.rb ├── classes1b.rb ├── classes2.pp ├── classes2.rb ├── classes3.pp └── classes3.rb ├── defaults ├── defaults1.pp └── defaults1.rb ├── functions ├── functions1.pp └── functions1.rb ├── nodes ├── nodes1.pp └── nodes1.rb ├── relationships ├── relationships1.pp └── relationships1.rb ├── resources ├── resources1.pp ├── resources1.rb ├── resources2.pp ├── resources2.rb ├── resources3.pp └── resources3.rb └── vars ├── vars1.pp └── vars1.rb /README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | The purpose of this repo is to provide some working (albeit contrived) examples 4 | of Ruby DSL, alongside their corresponding Puppet DSL equivalents. So it is 5 | purely a training and documentation aid. 6 | 7 | For the most authoritative documentation on Ruby DSL (at least at the time I 8 | wrote this) see the Ruby DSL Wiki page: 9 | 10 | 11 | 12 | ## Layout 13 | 14 | These directories contain several examples of Ruby DSL and Puppet DSL next to 15 | each other. So for example the file: 16 | 17 | defaults/defaults1.pp 18 | 19 | Should perform the same functional task as its .rb equivalent: 20 | 21 | defaults/defaults1.rb 22 | 23 | And this same pattern should apply to all the examples. 24 | 25 | If you want to try these examples, you should be able to just run 'puppet apply' 26 | across them. For example: 27 | 28 | puppet apply defaults/defaults1.rb 29 | puppet apply defaults/defaults1.pp 30 | 31 | Of course the layout of these files is in single files for example purposes 32 | only. In a real deployment you would always make sure your code is laid out 33 | using modules. See the Ruby DSL and Puppet documentation for more detail 34 | regarding this. 35 | -------------------------------------------------------------------------------- /classes/classes1.pp: -------------------------------------------------------------------------------- 1 | class foo { 2 | notice("foo") 3 | } 4 | node "default" { 5 | include foo 6 | } 7 | -------------------------------------------------------------------------------- /classes/classes1.rb: -------------------------------------------------------------------------------- 1 | hostclass :foo do 2 | notice ["foo"] 3 | end 4 | node "default" do 5 | create_resource :class, :foo 6 | end 7 | -------------------------------------------------------------------------------- /classes/classes1b.rb: -------------------------------------------------------------------------------- 1 | hostclass :foo do 2 | notice ["foo"] 3 | end 4 | node "default" do 5 | include "foo" 6 | end 7 | 8 | -------------------------------------------------------------------------------- /classes/classes2.pp: -------------------------------------------------------------------------------- 1 | class foo ($myparam) { 2 | notice($myparam) 3 | } 4 | node "default" { 5 | class { "foo": myparam => "foo" } 6 | } 7 | -------------------------------------------------------------------------------- /classes/classes2.rb: -------------------------------------------------------------------------------- 1 | hostclass :foo, :arguments => {"myparam" => nil} do 2 | notice [scope.lookupvar("myparam")] 3 | end 4 | node "default" do 5 | create_resource :class, :foo, :myparam => "foo" 6 | end 7 | -------------------------------------------------------------------------------- /classes/classes3.pp: -------------------------------------------------------------------------------- 1 | class foo ($myparam, $myparam2 = "default_value") { 2 | notice($myparam2) 3 | } 4 | node "default" { 5 | class { "foo": myparam => "foo" } 6 | } 7 | -------------------------------------------------------------------------------- /classes/classes3.rb: -------------------------------------------------------------------------------- 1 | hostclass :foo, :arguments => {"myparam" => nil, "myparam2" => AST::String.new(:value => "default_value")} do 2 | notice [scope.lookupvar("myparam2")] 3 | end 4 | node "default" do 5 | create_resource :class, :foo, :myparam => "foo" 6 | end 7 | -------------------------------------------------------------------------------- /defaults/defaults1.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | node "default" { 4 | # Defining 5 | Notify { 6 | message => "foo" 7 | } 8 | 9 | # Using 10 | notify {"bar": } 11 | } 12 | -------------------------------------------------------------------------------- /defaults/defaults1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | node "default" do 4 | # Defining 5 | scope.setdefaults(:notify, 6 | [Puppet::Parser::Resource::Param.new(:name => "message",:value => "foo")] 7 | ) 8 | 9 | # Using 10 | notify "foo" 11 | end 12 | -------------------------------------------------------------------------------- /functions/functions1.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | node "default" { 4 | # Using 5 | notice("foo") 6 | } 7 | -------------------------------------------------------------------------------- /functions/functions1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | node "default" do 4 | # Using 5 | notice ["foo"] 6 | end 7 | -------------------------------------------------------------------------------- /nodes/nodes1.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | # Defining 4 | node default { 5 | notice("foo") 6 | } 7 | -------------------------------------------------------------------------------- /nodes/nodes1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | # Defining 4 | node 'default' do 5 | notice ["foo"] 6 | end 7 | -------------------------------------------------------------------------------- /relationships/relationships1.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | define foo() { 4 | notify {"foo": message => "foo" } 5 | } 6 | define bar() { 7 | notify {"bar": message => "bar" } 8 | } 9 | 10 | node "default" { 11 | foo {"foo": 12 | require => Bar["bar"] 13 | } 14 | bar {"bar": } 15 | } 16 | -------------------------------------------------------------------------------- /relationships/relationships1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | define "foo" do 4 | notify "foo", :message => "foo" 5 | end 6 | define "bar" do 7 | notify "bar", :message => "bar" 8 | end 9 | 10 | node "default" do 11 | foo "foo", :require => "Bar[bar]" 12 | bar "bar" 13 | end 14 | -------------------------------------------------------------------------------- /resources/resources1.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | # Defining 4 | define foo() { 5 | notice($name) 6 | } 7 | 8 | node "default" { 9 | # Using 10 | foo {"foo": } 11 | } 12 | -------------------------------------------------------------------------------- /resources/resources1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | # Defining 4 | define "foo" do 5 | notice [@name] 6 | end 7 | 8 | node "default" do 9 | # Using 10 | create_resource :foo, "foo" 11 | end 12 | -------------------------------------------------------------------------------- /resources/resources2.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | # Exported resources requires storeconfigs to be configured: 4 | # 5 | # http://projects.puppetlabs.com/projects/1/wiki/Using_Stored_Configuration 6 | # 7 | 8 | define foo($msg) { 9 | notify {$msg: message => $msg } 10 | } 11 | 12 | node "default" { 13 | # Export 14 | @@foo {"bar": 15 | msg => "foo", 16 | } 17 | 18 | # Collection 19 | Foo <<| |>> 20 | } 21 | -------------------------------------------------------------------------------- /resources/resources2.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | # Exported resources requires storeconfigs to be configured: 4 | # 5 | # http://projects.puppetlabs.com/projects/1/wiki/Using_Stored_Configuration 6 | # 7 | 8 | define "foo", :msg do 9 | notify @msg, :message => @msg 10 | end 11 | 12 | node 'default' do 13 | # Export 14 | export do 15 | foo "bar", :msg => "foo" 16 | end 17 | 18 | # Collect 19 | newcoll = Puppet::Parser::Collector.new(scope, "Foo", nil, nil, :exported) 20 | scope.compiler.add_collection(newcoll) 21 | end 22 | -------------------------------------------------------------------------------- /resources/resources3.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | define foo($msg) { 4 | notify {$msg: message => $msg } 5 | } 6 | 7 | node "default" { 8 | # Virtualise 9 | @foo {"bar": 10 | msg => "foo", 11 | } 12 | 13 | # Realise 14 | Foo <| |> 15 | } 16 | -------------------------------------------------------------------------------- /resources/resources3.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | define "foo", :msg do 4 | notify @msg, :message => @msg 5 | end 6 | 7 | node 'default' do 8 | # Export 9 | virtual do 10 | foo "bar", :msg => "foo" 11 | end 12 | 13 | # Collect 14 | newcoll = Puppet::Parser::Collector.new(scope, "Foo", nil, nil, :virtual) 15 | scope.compiler.add_collection(newcoll) 16 | end 17 | -------------------------------------------------------------------------------- /vars/vars1.pp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | node "default" { 4 | # Defining 5 | $asdf = "foo" 6 | 7 | # Using 8 | notice($asdf) 9 | } 10 | -------------------------------------------------------------------------------- /vars/vars1.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env puppet 2 | 3 | node "default" do 4 | # Defining 5 | scope.setvar("asdf", "foo") 6 | 7 | # Using 8 | a = scope.lookupvar("asdf") 9 | notice [a] 10 | end 11 | --------------------------------------------------------------------------------