Martin Willemsma
2012-Jul-05 15:43 UTC
[Puppet Users] hash to_yaml in erb template not giving valid yaml
Hi, I want to use a file resource to write a facts.yaml file for Mcollective. For some reason it won''t provide a valid yaml format if I dump my scope to hash and convert to_yaml. file { "/etc/mcollective/facts.yaml" : owner => root, group => root, mode => 400, loglevel => debug, #content => inline_template("<%= scope.to_hash.reject { |k,v| !( k.is_a?(String) && v.is_a?(String) ) }.to_yaml %>"), #content => inline_template("<%= { \"a\" => 1, \"b\"=> 2}.to_yaml %>"), content => inline_template("<%= facts = {}; scope.to_hash.each_pair {|k,v| facts[k.to_s] = v.to_s unless k.to_s =~ /pkg_|_hours|_seconds|memoryfree|plugin_|config|_timestamp/ }; facts.to_yaml.sort %>"), } After a run the content is indented with 2 spaces and some facts give me garbage values. If I verify this on e.g. http://yamllint.com/ I have a syntax error with the 2 leading spaces. Snippet from ''/etc/mcollective/facts.yaml'' [root~]# cat /etc/mcollective/facts.yaml architecture: &id001 x86_64 hardwareisa: x86_64 hardwaremodel: *id001 kernelrelease: 2.6.18-274.17.1.el5xen clientversion: *id002 kernelversion: 2.6.18 lsbdistcodename: Final lsbdistdescription: CentOS release 5.8 (Final) lsbdistid: CentOS lsbdistrelease: "5.8" lsbmajdistrelease: "5" I did some debugging on the IRB. On the commandline I do get the expected format. To me it seems the to_yaml is not working for me in combination with inline template. What is causing the extra leading spaces? irb irb(main):001:0> h = { "a" => 1 , "b" => 2 } => {"b"=>2, "a"=>1} irb(main):002:0> require "yaml" => true irb(main):003:0> h.to_yaml => "--- \nb: 2\na: 1\n" irb(main):004:0> I see several snippets on the internets to write a facts.yaml file to be used with Mcollective. None of them work for me. Puppet version: 2.7.12 OS: Ubuntu 10.04 Any hints on how to fix this issue? --- kind regards, Martin Willemsma -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Stefan Schulte
2012-Jul-05 17:46 UTC
Re: [Puppet Users] hash to_yaml in erb template not giving valid yaml
On Thu, Jul 05, 2012 at 05:43:10PM +0200, Martin Willemsma wrote:> Hi, > > I want to use a file resource to write a facts.yaml file for > Mcollective. For some reason it won''t provide a valid yaml format if I > dump my scope to hash and convert to_yaml. > > file { > "/etc/mcollective/facts.yaml" : > owner => root, > group => root, > mode => 400, > loglevel => debug, > #content => inline_template("<%= scope.to_hash.reject { |k,v| !( > k.is_a?(String) && v.is_a?(String) ) }.to_yaml %>"), > > #content => inline_template("<%= { \"a\" => 1, \"b\"=> 2}.to_yaml %>"), > content => > inline_template("<%= facts = {}; scope.to_hash.each_pair {|k,v| > facts[k.to_s] = v.to_s unless k.to_s =~ > /pkg_|_hours|_seconds|memoryfree|plugin_|config|_timestamp/ }; > facts.to_yaml.sort %>"), > } >The to_yaml output should start with a "---\n" line, but if you sort your list (BTW: I get an error on ruby 1.9 when I call sort on a string) you may produce an invalid yaml file if the "---" is not on top anymore. -Stefan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.