一、puppetmaster配置:
1. 增加环境变量,仅用于测试编写的变量是否生效。
1
2
3
4
[iyunv@lw-01 ~]# vi /etc/profile 增加以下内容:
#puppet facter
export FACTERLIB=/etc/puppet/modules/facts/lib/facter/ #自定义变量文件存放路径
[iyunv@lw-01 ~]# source /etc/profile
2. 自定义变量文件路径在: /etc/puppet/modules/$NAME/lib/facter #$NAME随便定义,可以被其它的模块使用。
1
2
3
4
5
6
7
8
9
10
[iyunv@lw-01 modules]# pwd
/etc/puppet/modules
[iyunv@lw-01 modules]# tree facts/
facts/
└── lib
└── facter #此目录下为每个rb文件定义一个facter变量
├── lannet.rb
├── load_avg.rb
└── log_num.rb
2 directories, 6 files
1
2
3
4
5
6
7
[iyunv@lw-01 modules]# cd facts/lib/facter/
[iyunv@lw-01 facter]# cat log_num.rb
Facter.add(:log_num) do #log_num 为自定义变量名
setcode do
%x{/usr/bin/who |wc -l}.chomp
end
end
3. 测试变量
1
2
3
4
5
6
[iyunv@lw-01 facter]# facter log_num
3
或者:
[iyunv@lw-01 facter]# facter | grep log_num
log_num => 3
4. 定义一个测试模块
1
2
3
4
5
6
7
8
[iyunv@lw-01 modules]# tree test/
test/
├── files
├── manifests
│ └── init.pp
└── templates
└── test.rb
3 directories, 2 files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@lw-01 test]# cat manifests/init.pp
class test {
test::pwd
}
class test::pwd{
$log_num= "$log_num" #调用变量
file{"/root/test/test.txt": #puppet agent端会生成test.txt
ensure => present,
content => template('test/test.rb'),
mode => '644',
owner => 'root',
group => 'root',
}
}
1
2
[iyunv@lw-01 test]# cat templates/test.rb
log_num: <%= scope.lookupvar('test::pwd::log_num') %>
二、puppet agent端测试
1
2
3
4
5
6
7
8
9
[iyunv@lw-02 ~]# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for 192.168.2.11
Info: Applying configuration version '1429004437'
Notice: /Stage[main]/Test::Pwd/File[/root/test/test.txt]/ensure: created
Notice: /Stage[main]/Test::Exec/Exec[test]/returns: executed successfully
Notice: Finished catalog run in 1.77 seconds
1
2
3
[iyunv@lw-02 ~]# cd /root/test/
[iyunv@lw-02 test]# cat test.txt
log_num: 3 #测试成功,log_num成为变量
1
[iyunv@lw-02 ~]# facter -p #可以看到所有服务器端定义的fact变量
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com