{"id":232,"date":"2017-05-30T02:43:46","date_gmt":"2017-05-30T07:43:46","guid":{"rendered":"http:\/\/www.paulreed.ca\/?p=232"},"modified":"2017-05-30T02:55:24","modified_gmt":"2017-05-30T07:55:24","slug":"custom-facts-on-windows-with-puppet-and-xff-errors","status":"publish","type":"post","link":"https:\/\/paulreed.ca\/?p=232","title":{"rendered":"Custom Facts on Windows with Puppet and &#8220;&#8221;\\xFF&#8221;&#8221; Errors"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>It&#8217;s been a long while since I posted last (as usual). It&#8217;s also been a long while since I started working for Puppet.<\/p>\n<p>Until now, I haven&#8217;t really found much puppet things to blog about, but that&#8217;s about to change.<\/p>\n<p>Lets start with something I ran into today. Custom Facts on Windows with Puppet!<\/p>\n<p>Coincidentally, while working on something else, I dropped a fact into my facts.d folder and went on working on other things &#8230; only to bang my head against a wall for a few hours.<\/p>\n<h2>Custom facts are easy in Puppet!<\/h2>\n<p>&#8220;Custom facts are easy&#8221;, you say&#8230; &#8220;All I need to do is drop something like this on the OS&#8221;:<br \/>\n<code>echo my_custom_fact=value123 &gt; C:\\ProgramData\\PuppetLabs\\facter\\facts.d\\my_custom_fact.txt<br \/>\n<\/code><em>[Note: The code above is a single wrapped line]<\/em><\/p>\n<p><a href=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-243 size-full\" src=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error6.png\" alt=\"echo my_custom_fact=value123 &gt; C:\\ProgramData\\PuppetLabs\\facter\\facts.d\\my_custom_fact.txt\" width=\"727\" height=\"18\" \/><\/a><\/p>\n<p>&#8230; and you would be wrong. L<code><\/code>ike I was. That method works fine for posix based systems &#8230; but not for Windows (read on to learn why, and how to work around it).<\/p>\n<p>Of course, I figured my fact was fine &#8230; it created the file and the file looked okay, so I went on trying to troubleshoot the other thing I was working on while getting this error when running the puppet agent (&#8220;puppet agent -t&#8221;) on my node:<br \/>\n<a href=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-234 size-full\" src=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error1.png\" alt=\"\" width=\"838\" height=\"143\" srcset=\"https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error1.png 838w, https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error1-768x131.png 768w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><\/a><\/p>\n<p>What the hell does that mean? \\xFF? &#8230;<\/p>\n<h2>File encoding types&#8230; ugh.<\/h2>\n<p>Well, after some google-fu, turns out that \\xFF is one of the starting byte codes for a file encoding type &#8230;. ok, so it looks like a file encoding error.<br \/>\nSo like a dumbass, I was checking all my other files and certificate properties, even rebuilt the VM a few (dozen) times (because that&#8217;s what I was working on when I hit this error)&#8230; not realizing all along it was the filetype of my custom fact that was causing this bullsh*t.<\/p>\n<p>Ok &#8230; so what&#8217;s going on here?<br \/>\n<a href=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-236 size-full\" src=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error2.png\" alt=\"\" width=\"844\" height=\"375\" srcset=\"https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error2.png 844w, https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error2-768x341.png 768w\" sizes=\"(max-width: 844px) 100vw, 844px\" \/><\/a><\/p>\n<p>It turns out Puppet on Windows really doesn&#8217;t like things that aren&#8217;t UTF8 encoded&#8230; especially in facter &#8230;. and the error output is basically useless because it doesn&#8217;t point you in the direction of factor or the custom facts.d file at all.<\/p>\n<p>That&#8217;s right, hours and many rebuilds of my VM because windows &#8220;echo &gt;&#8221; in Windows decides to create files in &#8220;UCS-2 Little Endian&#8221; encoding &#8230; and Puppet (or well, more likely some of the ruby code in behind it for facter) can&#8217;t properly decode\/re-encode Windows UCS-2 to UTF-8.<\/p>\n<p>Okay, great &#8230; save the file in UTF8, and away we go&#8230;<br \/>\n<a href=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-237 size-full\" src=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error3.png\" alt=\"\" width=\"832\" height=\"351\" srcset=\"https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error3.png 832w, https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error3-768x324.png 768w\" sizes=\"(max-width: 832px) 100vw, 832px\" \/><\/a><\/p>\n<p>BTW, You&#8217;ll need to run puppet agent twice to clear out the old, wrongly encoded fact from your master to get a clean run&#8230;<br \/>\n<a href=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-238 size-full\" src=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error4.png\" alt=\"\" width=\"575\" height=\"248\" \/><\/a><\/p>\n<h2>But&#8230;. Automation? Scripting?<\/h2>\n<p>Well &#8230; we can&#8217;t very well use the <em>&#8220;echo something &gt; facts.d\/something.txt&#8221;<\/em> method anymore &#8230;. so what&#8217;s the fix that I can use in an automated process?<\/p>\n<p><strong>Powershell to the rescue!<\/strong><\/p>\n<p>Here&#8217;s a way to do it that will create a proper UTF-8 file that Puppet\/facter can read:<br \/>\n<code>echo \"my_custom_fact=value123\" | out-file -filepath C:\\ProgramData\\PuppetLabs\\facter\\facts.d\\my_custom_fact.txt -encoding UTF<\/code><em><br \/>\n[Note: The code above is a single wrapped line]<br \/>\n<\/em><br \/>\n<a href=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-239 size-full\" src=\"http:\/\/www.paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error5.png\" alt=\"\" width=\"844\" height=\"122\" srcset=\"https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error5.png 844w, https:\/\/paulreed.ca\/wp-content\/uploads\/2017\/05\/2017-05-29error5-768x111.png 768w\" sizes=\"(max-width: 844px) 100vw, 844px\" \/><\/a><\/p>\n<p>That&#8217;ll do it &#8230; of course, if you&#8217;re not working in powershell (or using a provisioning shell from vagrant like I am), then you just need to wrap it in a call to powershell like so (and make sure you escape your quotes properly using backticks and not single quotes!):<br \/>\n<code>powershell -Command \"echo `\"my_custom_fact=value123`\" | out-file -filepath C:\\ProgramData\\PuppetLabs\\facter\\facts.d\\my_custom_fact.txt -encoding UTF8\"<\/code><br \/>\n<em>[Note: The code above is a single wrapped line]<\/em><\/p>\n<p>Ok, That&#8217;s it &#8230; hours of figuring out WTF was happening so you don&#8217;t have to go through it yourself &#8230; hopefully google will pick this up for searches on &#8216;Puppet Windows &#8220;&#8221;\\xFF&#8221;&#8221; errors&#8217; and save you some time.<\/p>\n<p>Until next time!<br \/>\n-Paul<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, It&#8217;s been a long while since I posted last (as usual). It&#8217;s also been a long while since I started working for Puppet. Until now, I haven&#8217;t really found much puppet things to blog about, but that&#8217;s about to change. Lets start with something I ran into today. Custom Facts on Windows with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,54,21],"tags":[56,55,46,57],"_links":{"self":[{"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/posts\/232"}],"collection":[{"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paulreed.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=232"}],"version-history":[{"count":11,"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":249,"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions\/249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/paulreed.ca\/index.php?rest_route=\/wp\/v2\/media\/251"}],"wp:attachment":[{"href":"https:\/\/paulreed.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paulreed.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paulreed.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}