Can we write many classes in a single manifest

1,280 views
Skip to first unread message

yarlagadda ramya

unread,
Feb 18, 2013, 2:07:39 AM2/18/13
to puppet...@googlegroups.com
Hi all,

Can we write many classes in a single manifest and apply that manifest??
For example i have created  manifest, file1.pp and it includes the following:

class one{
              exec{"--"
              command => "  ",
                      cwd => "   ",    
                      }
              }


class two{
              exec{"--"
              command => "  ",
                      cwd => "   ",    
                      }
              }

now can i write a manifest like this and apply it?

Can any one pls help me with it?

Keiran Sweet

unread,
Feb 18, 2013, 5:40:54 AM2/18/13
to puppet...@googlegroups.com
Hi There,
You can, however once a class is defined you must then apply it to the node using 'include'

Example:

[ Keiran ~]$ cat /tmp/example.pp

class class1 {
        exec { exec1 :
                command => "/bin/touch /tmp/${title}",
            }

    }

class class2 {
        exec { exec2 :
                command => "/bin/touch /tmp/${title}",
            }

    }

include class1
include class2

[ Keiran ~]$ puppet apply /tmp/example.pp
notice: /Stage[main]/Class2/Exec[exec2]/returns: executed successfully
notice: /Stage[main]/Class1/Exec[exec1]/returns: executed successfully
notice: Finished catalog run in 0.17 seconds
[ Keiran ~]$ ls -al /tmp/ |grep -i class
-rw-r--r--  1 Keiran Keiran     0 Feb 18 10:27 class1
-rw-r--r--  1 Keiran Keiran     0 Feb 18 10:27 class2
[ Keiran ~]$


Defining multiple classes in a single manifest for non-testing/learning may not always be deemed as best practice, It might be worth having a read of the following documentation:

Hope this helps,

K

jcbollinger

unread,
Feb 18, 2013, 9:37:34 AM2/18/13
to puppet...@googlegroups.com


On Monday, February 18, 2013 1:07:39 AM UTC-6, yarlagadda ramya wrote:
Hi all,

Can we write many classes in a single manifest and apply that manifest??


Yes and no.  You can write many classes in the same manifest file, but you cannot leverage their colocation in the same file to assign all of them to the same node at once.

 
For example i have created  manifest, file1.pp and it includes the following:

class one{
              exec{"--"
              command => "  ",
                      cwd => "   ",    
                      }
              }


class two{
              exec{"--"
              command => "  ",
                      cwd => "   ",    
                      }
              }

now can i write a manifest like this and apply it?

Can any one pls help me with it?


You should put all your classes in modules, and lay them out in the way the autoloader expects.  Example:

modules/mymodule/manifests/one.pp:
------
class mymodule::one {
  # resources ...
}


modules/mymodule/manifests/two.pp:
------
class mymodule::two {
  # resources ...
}

Note the correspondences between module name ("mymodule") and directory, and between class names and file names.  Those are significant.


If you want a simple, reusable way to apply both of those together to the same node, then create a class for it.  If the two (or more) belong to the same module, then you might be looking for a module main class:

modules/mymodule/manifests/init.pp:
------
class mymodule {
  include 'mymodule::one'
  include 'mymodule::two'
}


Note that that one is special: the fully-qualified name of the class is the same as the name of the module, and it (for that reason) is recorded in a file named 'init.pp' in the module's manifests directory.

To apply both to node "mynode", just assign class 'mymodule' to that node:

node 'mynode' {
  include 'mymodule'
}


John

Reply all
Reply to author
Forward
0 new messages