1、使用环境变量‘FACTERLIB’创建fact

1.1、在自定义目录里面定义一个fact,列出当前系统登录的用户数

[root@agent1 ~]# vim /var/lib/puppet/kiss_fact/users.rb

  1. Facter.add(:users) do
  2. setcode do
  3. %x{/usr/bin/who |wc -l}.chomp
  4. end
  5. end

[root@agent1 kiss_fact]# facter users #无显示结果,需要设置FACTERLIB

[root@agent1 kiss_fact]#

1.2、将自定义fact路径赋值给变量FACTERLIB

[root@agent1 kiss_fact]# export FACTERLIB=/var/lib/puppet/kiss_fact

[root@agent1 kiss_fact]# facter users

2

[root@agent1 kiss_fact]# facter | grep users

users => 2

备注:这种方法是对第一种方法的扩展,可以自己定义目录,不过需要将路径加到变量FACTERLIB中,可以在/etc/profile添加,这样系统启动的时候便可以自动加载。

2、使用pluginsync进行发布

这种方法比较特殊,节点factpath目录里除了编写好的rb文件之外,还需要在puppet模块中引用,运行一次之后才会转换成fact。通常在puppetmaster端模块里的lib库中添加,然后在puppet.conf中添加选项pluginsync=true即可,格式为ruby文件。

2.1、创建模块facts

[root@puppetmaster ~]# cd /etc/puppet/environments/kissprd/environment/modules/

[root@puppetmaster modules]# tree facts/ #目录结构

  1. facts/
  2. └── lib
  3. └── facter
  4. └── hwclock.rb

2 directories, 1 file

备注:也可以放在其他已经编写好的模块中

[root@puppetmaster facter]# vim hwclock.rb #自定义

fact:hwclock,显示节点硬件时间

  1. Facter.add(:hwclock) do
  2. setcode do
  3. %x{/usr/sbin/hwclock}.chomp
  4. end
  5. end

2.2、应用自定义fact至motd模块中

[root@puppetmaster kissprd]# vim application/modules/motd/manifests/init.pp

  1. class motd{
  2. package{ 'setup':
  3. ensure => present,
  4. }
  5. file{ '/etc/motd':
  6. ensure => present,
  7. owner => 'root',
  8. group => 'root',
  9. mode => '0644',
  10. source => "puppet://$puppetserver/modules/motd/etc/motd",
  11. require => Package['setup'],
  12. }
  13. notify { " Hardware-Clock: ${::hwclock}": } #添加一个通知,这里只是测试,没有实际意义
  14. }

2.3、在puppetmaster端的puppet.conf中添加选项pluginsync

[root@puppetmaster kissprd]# vim /etc/puppet/puppet.conf

  1. [main]
  2. logdir = /var/log/puppet
  3. rundir = /var/run/puppet
  4. ssldir = $vardir/ssl
  5. pluginsync = true #添加插件选项
  6. ...

2.4、在所有节点puppet.conf中添加pluginsync(通过在puppet模块中添加实现)

[root@puppetmaster kissprd]# vim environment/modules/puppet/templates/puppet.conf.erb

  1. ### config by puppet ###
  2. [main]
  3. logdir = /var/log/puppet
  4. rundir = /var/run/puppet
  5. ssldir = $vardir/ssl
  6. pluginsync = true #添加插件选项
  7. [agent]
  8. classfile = $vardir/classes.txt
  9. localconfig = $vardir/localconfig
  10. server = <%= scope.lookupvar('puppet::params::puppetserver') %>
  11. certname = <%= scope.lookupvar ('puppet::params::certname') %>

2.5、节点运行puppet agent进行测试

[root@agent1 ~]# facter -p hwclock #没有这个fact,自定义fact需要加上-p参数才能显示

[root@agent1 ~]# puppet agent -t --environment=kissprd #运行一次

  1. info: Retrieving plugin
  2. notice: /File[/var/lib/puppet/lib/facter/historys.rb]/ensure: removed
  3. notice: /File[/var/lib/puppet/lib/facter/hwclock.rb]/ensure: defined content as '{md5}d8cc9fe2b349a06f087692763c878e28'
  4. info: Loading downloaded plugin /var/lib/puppet/lib/facter/hwclock.rb #下载插件至节点factpath指定的目录
  5. info: Loading facts in /var/lib/puppet/lib/facter/hwclock.rb
  6. info: Caching catalog for agent1_cert.kisspuppet.com
  7. info: Applying configuration version '1396170375'
  8. notice: Hardware-Clock: Sun 30 Mar 2014 05:06:16 PM CST -0.055086 seconds
  9. notice: /Stage[main]/Motd/Notify[ Hardware-Clock: Sun 30 Mar 2014 05:06:16 PM CST -0.055086 seconds]/ message: defined 'message' as ' Hardware-Clock: Sun 30 Mar 2014 05:06:16 PM CST -0.055086 seconds' #应用
  10. notice: Finished catalog run in 0.51 seconds

[root@agent1 ~]# facter -p hwclock #自定义的hwclock生效

  1. hwclock => Sun 30 Mar 2014 05:06:25 PM CST -0.567090 seconds

[root@agent1 ~]# ll /var/lib/puppet/lib/facter/ #插件已经下载到本地

  1. total 4
  2. -rw-r--r-- 1 root root 79 Mar 30 17:06 hwclock.rb

关于factpath默认路径可通过以下命令查看,当然也可以在puppet.conf中进行修改

[root@agent1 ~]# puppet --genconfig | grep factpath

  1. factpath = /var/lib/puppet/lib/facter:/var/lib/puppet/facts

转载自:https://kisspuppet.gitbooks.io/puppet/content/puppet_learning_base10.html

puppet之自定义fact(转载)的更多相关文章

  1. SQL Server自定义函数( 转载于51CTO )

    用户自定义函数自定义函数不能执行一系列改变数据库状态的操作,可以像系统函数在查询或存储过程等的程序中使用,也可以像相信过程一样能过 execute 命令来执行.自定义函数中存储了一个 Transact ...

  2. iOS多线程编程之自定义NSOperation(转载)

    一.实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController. 1 // ...

  3. iOS-UICollectionView自定义布局

    UICollectionView自定义布局 转载: http://answerhuang.duapp.com/index.php/2013/11/20/custom_collection_view_l ...

  4. 【VUE】自定义组件

    [VUE]自定义组件 转载: ============================================ ======================================== ...

  5. 【Feign】自定义配置

    [Feign]自定义配置 转载: 自定义配置,如果在同一个工程,注意配置不要和@SpringBootApplication或@ComponentSacan放在用一个包下,就是不要被扫描上 packag ...

  6. 【hibernate】自定义转换器

    [hibernate]自定义转换器 转载:https://www.cnblogs.com/yangchongxing/p/10398255.html 1.转换基本属性 package cn.ycx.s ...

  7. PHP之道 - php各方面的知识汇总

    看到一个PHP的知识各方面的汇总,写的很有借鉴意义,搬过来了 转自: https://laravel-china.github.io/php-the-right-way/ 欢迎阅读 其他语言版本 参与 ...

  8. 【cocos2d-js官方文档】事件分发监听机制(摘录)

    简介 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处理用户交互就需要使用事件监听器了. 总概: 事件监听器(cc.EventListener) 封装用户的事件处理逻 ...

  9. Android开发--List与ArrayList区别

    List是一个接口,而ArrayList是一个类.  ArrayList继承并实现了List.  所以List不能被构造,但可以向上面那样为List创建一个引用,而ArrayList就可以被构造.  ...

随机推荐

  1. 序列化类型为“System.Data.Entity.DynamicProxies.ActionInfo_”的对象时检测到循环引用。

    解决方案: 加上 db.Configuration.ProxyCreationEnabled = false;这句话搞定~

  2. android学习——error opening trace file: No such file or directory (2)

    1.疑惑: 程序运行起来的时候日志总是显示下面这个错误,但是不影响程序的正常进行,我是用真机来测试的,android4.4.4(API17). 02-11 14:55:03.629 15525-155 ...

  3. IntellJ IDEA 所有快捷键

    登录下面网站. http://www.jetbrains.com/idea/documentation/ 下载Keymap for Windows/Linux 后面的PDF文档.

  4. Java基础-关键字-final

    在Java中,final关键字可以用来修饰类.方法和变量(包括成员变量和局部变量).下面就从这三个方面来了解一下final关键字的基本用法. 1.修饰类 当用final修饰一个类时,表明这个类不能被继 ...

  5. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  6. Linux Rootkit Sample && Rootkit Defenser Analysis

    目录 . 引言 . LRK5 Rootkit . knark Rootkit . Suckit(super user control kit) . adore-ng . WNPS . Sample R ...

  7. EPROCESS 进程/线程优先级 句柄表 GDT LDT 页表 《寒江独钓》内核学习笔记(2)

    在学习笔记(1)中,我们学习了IRP的数据结构的相关知识,接下来我们继续来学习内核中很重要的另一批数据结构: EPROCESS/KPROCESS/PEB.把它们放到一起是因为这三个数据结构及其外延和w ...

  8. Sublime Text 3 笔记

    Nearly all of the interesting files for users live under the data directory. The data directory is ~ ...

  9. jboss7.1.1配置mysql数据源

    http://blog.csdn.net/msz1992/article/details/8826754 #1.到http://www.mysql.com/downloads/connector/j/ ...

  10. Entity Framework 学习总结之一:ADO.NET 实体框架概述

    http://www.cnblogs.com/xlovey/archive/2011/01/03/1924800.html ADO.NET 实体框架概述 新版本中的 ADO.NET 以新实体框架为特色 ...