Hi, I''ve been doing some tests with some internal methods just to mess a little and learn a bit about puppet internals but found myself in a situation which I don''t really understand and I was hoping someone around could help me to get it. I have a test module with the following init.pp: class test1 { file {''/tmp/test1'': content => template(''test1/test1.erb''), } Test1::Print <| |> -> File[''/tmp/test1''] } The define test1::print looks like: define test1::print ( $message ) { notify{$message: } } So nothing really special, then in the template I try to catch all the resources in the catalog with "scope.catalog.resources" method, nothing I came up with by myself I saw it in a module and thought it was a neat trick and wanted to try it and learn what else I could do with this kind of stuff. The problem I''m having is that the template seems to be "compiled" before all the resources are in the catalog as the file only contains the following list of resources: Stage[main] Class[Settings] Class[main] Node[default] Class[Test1] In puppet''s output, the relationships seem to be well defined but feels like puppet is somehow applying (catching?) the template even before creating the actual file. The template has the following content: <% @test = scope.catalog.resources.select { |r| r.type } -%> <% @test.each do |t| -%> <%= t %> <% end %> Am I missing anything? I expected to see in the file the defined type test1::print... Thanks! -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Sep-10 13:23 UTC
[Puppet Users] Re: Ordering and templates question/issue
On Monday, September 9, 2013 6:13:46 PM UTC-5, sjr wrote:> > Hi, > > I''ve been doing some tests with some internal methods just to mess a > little and learn a bit about puppet internals but found myself in a > situation which I don''t really understand and I was hoping someone around > could help me to get it. > > I have a test module with the following init.pp: > > class test1 > { > > file > {''/tmp/test1'': > > content => > template(''test1/test1.erb''), > > > } > > > > Test1::Print <| |> -> > File[''/tmp/test1''] > > } > > The define test1::print looks like: > > define test1::print > ( > > > $message > > ) > { > > notify{$message: > } > > } > > So nothing really special, then in the template I try to catch all the > resources in the catalog with "scope.catalog.resources" method, nothing I > came up with by myself I saw it in a module and thought it was a neat trick > and wanted to try it and learn what else I could do with this kind of stuff. > > The problem I''m having is that the template seems to be "compiled" before > all the resources are in the catalog as the file only contains the > following list of resources: > > Stage[main] > Class[Settings] > Class[main] > Node[default] > Class[Test1] > > In puppet''s output, the relationships seem to be well defined but feels > like puppet is somehow applying (catching?) the template even before > creating the actual file. > > The template has the following content: > > <% @test = scope.catalog.resources.select { |r| r.type } -%> > <% @test.each do |t| -%> > <%= t %> > <% end %> > > Am I missing anything? I expected to see in the file the defined type > test1::print... > >You are missing several things, including at least 1. Puppet relationships, such as are declared via the arrow operators or ''require'' and ''before'' metaparameters, influence only the order in which resources are applied by the agent. They have no bearing on the order of events during catalog compilation. 2. Defined types are *types*, not resources. They are thoroughly analogous to native resource types such as File. You could see * instances* of defined types among your catalog resources if you declared any (you didn''t), but you will not see the defined types themselves. 3. scope.catalog.resources is an awful hack, not a "neat trick", because it is sensitive to when during the compilation process the template is evaluated. To build robust and reliable classes and modules you must avoid depending on mechanisms that are sensitive to evaluation order (sometimes inaccurately called "parse order", including by me). John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Hi John, thanks for your answer, first of all I must say I just wanted to experiment and learn, it''s not like I''m going to put such "awful hacks" :-) in production....mostly because I guess it''s easy they can be changed over the time as they are not part of puppet DSL and so, it can be harder to maintain and for other colleagues to understand. OK, now to the point...I forgot to include the node definition, sorry about that, it looks like: node hostname { class {''test1'': } test1::print {''test11'': message => ''test11'', } test1::print {''test12'': message => ''test13'', } test1::print {''test13'': message => ''test13'', } So I understand that when you say I haven''t defined any resource it''s because I forgot to mention that part, right? If so, why the define types are not showing up in the file? Also, as you can see in the content of the file I included in my previous message, Class[test1] was declared and showed in the content of the file, but the File resource itself was not there when the template was built. I have the feeling the template is evaluated before all this resources are present in the catalog...am I wrong? Is that expected behavior? Thanks. Sergio. On Tuesday, September 10, 2013 3:23:47 PM UTC+2, jcbollinger wrote:> > > > On Monday, September 9, 2013 6:13:46 PM UTC-5, sjr wrote: >> >> Hi, >> >> I''ve been doing some tests with some internal methods just to mess a >> little and learn a bit about puppet internals but found myself in a >> situation which I don''t really understand and I was hoping someone around >> could help me to get it. >> >> I have a test module with the following init.pp: >> >> class test1 >> { >> >> file >> {''/tmp/test1'': >> >> content => >> template(''test1/test1.erb''), >> >> >> } >> >> >> >> Test1::Print <| |> -> >> File[''/tmp/test1''] >> >> } >> >> The define test1::print looks like: >> >> define test1::print >> ( >> >> >> $message >> >> ) >> { >> >> notify{$message: >> } >> >> } >> >> So nothing really special, then in the template I try to catch all the >> resources in the catalog with "scope.catalog.resources" method, nothing I >> came up with by myself I saw it in a module and thought it was a neat trick >> and wanted to try it and learn what else I could do with this kind of stuff. >> >> The problem I''m having is that the template seems to be "compiled" before >> all the resources are in the catalog as the file only contains the >> following list of resources: >> >> Stage[main] >> Class[Settings] >> Class[main] >> Node[default] >> Class[Test1] >> >> In puppet''s output, the relationships seem to be well defined but feels >> like puppet is somehow applying (catching?) the template even before >> creating the actual file. >> >> The template has the following content: >> >> <% @test = scope.catalog.resources.select { |r| r.type } -%> >> <% @test.each do |t| -%> >> <%= t %> >> <% end %> >> >> Am I missing anything? I expected to see in the file the defined type >> test1::print... >> >> > You are missing several things, including at least > > 1. Puppet relationships, such as are declared via the arrow operators > or ''require'' and ''before'' metaparameters, influence only the order in which > resources are applied by the agent. They have no bearing on the order of > events during catalog compilation. > 2. Defined types are *types*, not resources. They are thoroughly > analogous to native resource types such as File. You could see * > instances* of defined types among your catalog resources if you > declared any (you didn''t), but you will not see the defined types > themselves. > 3. scope.catalog.resources is an awful hack, not a "neat trick", > because it is sensitive to when during the compilation process the template > is evaluated. To build robust and reliable classes and modules you must > avoid depending on mechanisms that are sensitive to evaluation order > (sometimes inaccurately called "parse order", including by me). > > > John > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Sep-11 13:35 UTC
[Puppet Users] Re: Ordering and templates question/issue
On Tuesday, September 10, 2013 8:53:10 AM UTC-5, sjr wrote:> > Hi John, > > thanks for your answer, first of all I must say I just wanted to > experiment and learn, it''s not like I''m going to put such "awful hacks" :-) > in production....mostly because I guess it''s easy they can be changed over > the time as they are not part of puppet DSL and so, it can be harder to > maintain and for other colleagues to understand. > > OK, now to the point...I forgot to include the node definition, sorry > about that, it looks like: > > node hostname { > class {''test1'': } > test1::print {''test11'': > message => ''test11'', > } > test1::print {''test12'': > message => ''test13'', > } > test1::print {''test13'': > message => ''test13'', > } > > So I understand that when you say I haven''t defined any resource it''s > because I forgot to mention that part, right? >Yes.> If so, why the define types are not showing up in the file? >You have not fully appreciated the implications of the evaluation-order dependency of scope.catalog.resources. The defined-type instances are not showing up in the file because their declarations have not yet been evaluated when the template is evaluated. Overall evaluation order is difficult to predict, and it is not stable with respect changes in your manifest set. I cannot emphasize strongly enough how important it is to avoid evaluation-order dependencies in your Puppet code.> Also, as you can see in the content of the file I included in my previous > message, Class[test1] was declared and showed in the content of the file, > but the File resource itself was not there when the template was built. >Of course it wasn''t. The value of the File''s ''content'' parameter is generated by evaluating the template via the template() function (on the master, during catalog compilation, the same as all Puppet functions are executed). Puppet must compute the values of the resource''s parameters before it can add the resource to the catalog.> > I have the feeling the template is evaluated before all this resources are > present in the catalog...am I wrong? Is that expected behavior? > >As described above, in this case the template will reliably be evaluated before the File because the template evaluation is subsidiary to the File''s declaration. Also, it happens that the template is evaluated before the three declarations of Test1::Print instances, but you should not over-interpret that result. In particular, you should not use it to try to predict evaluation order within other manifest sets. That should not be a problem, however, since you will be carefully avoiding code for which evaluation order males any difference. Right? John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Sep-11 13:37 UTC
[Puppet Users] Re: Ordering and templates question/issue
On Wednesday, September 11, 2013 8:35:07 AM UTC-5, jcbollinger wrote:> > males any difference > >"makes", that is. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Hi John, On Wednesday, September 11, 2013 3:35:07 PM UTC+2, jcbollinger wrote:> > > > On Tuesday, September 10, 2013 8:53:10 AM UTC-5, sjr wrote: >> >> Hi John, >> >> thanks for your answer, first of all I must say I just wanted to >> experiment and learn, it''s not like I''m going to put such "awful hacks" :-) >> in production....mostly because I guess it''s easy they can be changed over >> the time as they are not part of puppet DSL and so, it can be harder to >> maintain and for other colleagues to understand. >> >> OK, now to the point...I forgot to include the node definition, sorry >> about that, it looks like: >> >> node hostname { >> class {''test1'': } >> test1::print {''test11'': >> message => ''test11'', >> } >> test1::print {''test12'': >> message => ''test13'', >> } >> test1::print {''test13'': >> message => ''test13'', >> } >> >> So I understand that when you say I haven''t defined any resource it''s >> because I forgot to mention that part, right? >> > > > Yes. > > > >> If so, why the define types are not showing up in the file? >> > > > You have not fully appreciated the implications of the evaluation-order > dependency of scope.catalog.resources. The defined-type instances are not > showing up in the file because their declarations have not yet been > evaluated when the template is evaluated. Overall evaluation order is > difficult to predict, and it is not stable with respect changes in your > manifest set. I cannot emphasize strongly enough how important it is to > avoid evaluation-order dependencies in your Puppet code. >I see, I was expecting the template being evaluated later due to the ordering dependency I created but I guess I was missing the "evaluation order"...it''s a bit clearer now.> > >> Also, as you can see in the content of the file I included in my previous >> message, Class[test1] was declared and showed in the content of the file, >> but the File resource itself was not there when the template was built. >> > > > Of course it wasn''t. The value of the File''s ''content'' parameter is > generated by evaluating the template via the template() function (on the > master, during catalog compilation, the same as all Puppet functions are > executed). Puppet must compute the values of the resource''s parameters > before it can add the resource to the catalog. >That makes lots of sense....> > >> >> I have the feeling the template is evaluated before all this resources >> are present in the catalog...am I wrong? Is that expected behavior? >> >> > > As described above, in this case the template will reliably be evaluated > before the File because the template evaluation is subsidiary to the File''s > declaration. Also, it happens that the template is evaluated before the > three declarations of Test1::Print instances, but you should not > over-interpret that result. In particular, you should not use it to try to > predict evaluation order within other manifest sets. That should not be a > problem, however, since you will be carefully avoiding code for which > evaluation order males any difference. Right? > >Right ;-)> John > >Thanks a lot for taking the time to throw some light to my confusion. Sergio. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.