Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3009543
  • 博文数量: 535
  • 博客积分: 15788
  • 博客等级: 上将
  • 技术积分: 6507
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 09:11
文章分类

全部博文(535)

文章存档

2016年(1)

2015年(1)

2014年(10)

2013年(26)

2012年(43)

2011年(86)

2010年(76)

2009年(136)

2008年(97)

2007年(59)

分类: 云计算

2011-10-25 11:29:43

因为eth0为内网网卡,所以$ipaddress取到的是内网IP。
更改为公网IP?

facter的脚本在:
centos 5 /usr/lib/ruby/site_ruby/1.8/facter 或 centos 4.5 /usr/lib/site_ruby/1.8/facter 可以通过facter输出中rubysitedir确定

更改ipaddress.rb
打开文件可以看到:
On the Unixes does an ifconfig, and returns the first non 127.0.0.0/8

我的内网ip为172地址段,所以,只需要改成返回第一个即不是127又不是172地址段的ip即可
将第一个
unless tmp =~ /^127\./ 更改为 unless tmp =~ /^(172|127)\./
自定义facter,输出机房信息,内容为idc => sh_idc或bj_idc等
两种方式同步自定义的fact,
方法1:
当做文件,同步到所有的服务器。
创建一个module,user_define_fact。
  1. class user_define_fact{
  2. file {"$rubysitedir/facter/idcinfo.rb":
  3.        ensure => file, 
  4.        mode => 644, 
  5.        source => "puppet:///modules/user_define_fact/idcinfo.rb"; 
  6.  } 
  7. }
其中$rubysitedir是facter自带的fact,指向facter所存放的路径。

方法2:
使用puppet的pulgin module


步骤1、建立目录
/etc/puppet/modules/custom/lib/facter/
将rb文件放到该目录。
步骤2、
在master和client上同时打开pluginsync
[main]
pluginsync = true

idcinfo.rb内容为:
  1. Facter.add("idcinfo") do
  2.     setcode do
  3.                 ip = Facter.value('ipaddress')
  4.         idc = case ip
  5.             when /22\.19\.15/        : "ShangHai_IDC"
  6.             when /115\.151\.214/       : "HeNan_IDC"
  7.             when /19\.197\.47/       : "ShenZhen_IDC"
  8.             when /14\.42\.29/        : "BeiJing_IDC"
  9.             else    "idc info error"
  10.         end
  11.       idc
  12.     end
  13. end
其中:
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.

查看
facter idcinfo
注:需要将rb文件放到$rubysitedir。


当前时间
  1. Facter.add(:current_time) do
  2.      setcode do
  3.          %x{/bin/date +%Y%m%d%H%M%S}.chomp
  4.      end
  5. end

ruby中使用正则:

  1. Facter.add("ip_end") do
  2. setcode do
  3. ip = Facter.value('ipaddress')
  4. /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/.match(ip)
  5. ip_end=$4
  6. ip_end
  7. end
  8. end
阅读(5848) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~