- 论坛徽章:
- 0
|
<div><span class="Apple-style-span" style="background-color: rgb(255, 255, 255); "><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">因为eth0为内网网卡,所以$ipaddress取到的是内网IP。</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">更改为公网IP?</span></font><br><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">facter的脚本在:</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">centos 5
/usr/lib/ruby/site_ruby/1.8/facter
或
centos 4.5
/usr/lib/site_ruby/1.8/facter
可以通过facter输出中rubysitedir确定</span></font><br><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">更改ipaddress.rb</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">打开文件可以看到:</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">On the Unixes does an ifconfig, and returns the first non 127.0.0.0/8</span></font><br><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">我的内网ip为172地址段,所以,只需要改成返回第一个即不是127又不是172地址段的ip即可</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">将第一个</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;"> unless tmp =~ /^127\./
更改为
unless tmp =~ /^(172|127)\./
</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 17px;">自定义facter,输出机房信息,内容为idc => sh_idc或bj_idc等</span></font><br><blockquote style="font-family: arial, helvetica; font-size: 13px; line-height: 1.5em; padding-left: 0.8em; margin-left: 2.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(102, 102, 102); ">两种方式同步自定义的fact,<br>方法1:<br>当做文件,同步到所有的服务器。<br>创建一个module,user_define_fact。<br><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li>class user_define_fact{ </li><li>
file {"$rubysitedir/facter/idcinfo.rb": </li><li> ensure => file, </li><li> mode => 644, </li><li> source => "puppet:///modules/user_define_fact/idcinfo.rb"; </li><li> } </li><li>}</li></ol></div></blockquote><blockquote style="padding-left: 0.8em; margin-left: 2.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(102, 102, 102); "><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">其中$rubysitedir是facter自带的fact,指向facter所存放的路径。</span></font><br><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">方法2:</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">使用puppet的pulgin module</span></font><br><a class="externalLink" href="http://docs.puppetlabs.com/guides/plugins_in_modules.html" title="外部链接至 http://docs.puppetlabs.com/guides/plugins_in_modules.html" target="_blank" style="font-family: arial, helvetica; font-size: 13px; line-height: 1.5em; text-decoration: underline; color: rgb(0, 68, 187); ">http://docs.puppetlabs.com/guides/plugins_in_modules.html</a><br><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">步骤1、建立目录</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">/etc/puppet/modules/custom/lib/facter/</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">将rb文件放到该目录。</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">步骤2、</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">在master和client上同时打开pluginsync</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">[main]</span></font><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">pluginsync = true</span></font><br><br><font class="Apple-style-span" face="arial, helvetica" size="2"><span class="Apple-style-span" style="line-height: 1.5em;">idcinfo.rb内容为:</span></font><br><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li><li>Facter.add("idcinfo") do</li><li> setcode do</li><li> ip = Facter.value('ipaddress')</li><li> idc = case ip</li><li> when /22\.19\.15/ : "ShangHai_IDC"</li><li> when /115\.151\.214/ : "HeNan_IDC"</li><li> when /19\.197\.47/ : "ShenZhen_IDC"</li><li> when /14\.42\.29/ : "BeiJing_IDC"</li><li> else "idc info error"</li><li> end</li><li> idc</li><li> end</li><li>end</li></li></ol></div></blockquote><blockquote style="font-family: arial, helvetica; font-size: 13px; line-height: 1.5em; padding-left: 0.8em; margin-left: 2.5em; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(102, 102, 102); ">其中:<br>You can write a fact which uses other facts by accessing Facter.value(“somefact”) or simply Facter.somefact. The former will return nil for unknown facts, the latter will raise an exception.<br><br>查看<br>facter idcinfo<br>注:需要将rb文件放到$rubysitedir。<br><br><br>当前时间<br><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li>Facter.add(:current_time) do</li><li> setcode do</li><li> %x{/bin/date +%Y%m%d%H%M%S}.chomp</li><li> end</li><li>
end</li></ol></div></blockquote></span></div><div><br></div><div>ruby中使用正则:</div><div><a href="http://www.cnblogs.com/cnblogsfans/archive/2009/02/02/1382745.html" target="_blank" target="_blank">http://www.cnblogs.com/cnblogsfans/archive/2009/02/02/1382745.html</a></div><div><a href="http://yc75.iteye.com/blog/379929" target="_blank" target="_blank">http://yc75.iteye.com/blog/379929</a></div><div><br></div><div><div id="codeText" class="codeText"><ol start="1" class="dp-css"><li>Facter.add("ip_end") do</li><li><span class="Apple-tab-span" style="white-space:pre"> </span>setcode do</li><li><span class="Apple-tab-span" style="white-space:pre"> </span>ip = Facter.value('ipaddress')</li><li><span class="Apple-tab-span" style="white-space:pre"> </span>/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/.match(ip)</li><li><span class="Apple-tab-span" style="white-space:pre"> </span>ip_end=$4</li><li><span class="Apple-tab-span" style="white-space:pre"> </span>ip_end</li><li><span class="Apple-tab-span" style="white-space:pre"> </span>end</li><li>
end</li></ol></div></div> |
|