草庐IT

通过自定义fact增强MCollective推送更新元数据的灵活性

凌激冰 2023-03-28 原文
原文:http://kisspuppet.com/2013/11/10/my-fact/

www.kisspuppet.com puppet实战

欢迎puppet爱好者加入自动化运维交流总QQ群:296934942

目前由于Facter并不全面,许多关于主机和环境的信息并没有作为Facter的fact。编写自定义的fact,可以让节点的facter包含更多的元数据fact,增加MCollective选择元数据定位主机的灵活性。

1 自定义节点变量

首选,需要在每个节点自定义一个facts文档,文档中包含了每个节点自定义的fact信息。为了方便管理,所有变量的值都必须事先定义好,可在puppet服务端定义一个fact变量列表,里面包含所有节点的自定义fact信息。然后,节点根据各自的主机特性选择合适的fact信息。

[root@puppetserver ~]# vim /etc/mcollective/facts.txt #收集并定义所有节点的fact信息,仅仅作为查看用 fact_certname=<自定义> fact_apply1=apache fact_apply2=php fact_apply3=mysql fact_apply4=java fact_apply5=tomcat fact_apply6=oracle fact_apply7=nginx fact_apply8=jboss fact_apply9=haproxy fact_apply10=db2 …[root@agent1 ~]# cat /etc/mcollective/facts.txt #假设agent1节点具有以下fact变量信息 fact_certname=agent1.kisspuppet.com #puppet认证用,可写成其他名称 fact_apply3=mysql fact_apply4=java fact_apply10=db2 [root@agent2 ~]# cat /etc/mcollective/facts.txt #假设agent1节点具有以下fact变量信息 fact_certname=agent2.kisspuppet.com #puppet认证用,可写成其他名称 fact_apply2=php fact_apply3=mysql fact_apply7=nginx

2 创建file资源模块

由于自定义fact信息属于每个节点的特性,放在agents(存放单个节点个性模块的目录)目录中,可将这部分定义成一个class包含到每个节点的class agentN{}中。

[root@puppetserver ~]# cat /etc/puppet/agents/modules/agent1/manifests/init.pp class agent1{ include agent1::facts } class agent1::facts{ file{ "/etc/mcollective/facts.txt": owner => "root", group => "root", mode => 0400, content => template("agent1/facts.txt.erb"), backup => 'main', } } ...[root@puppetserver agents]# cat modules/agent1/templates/facts.txt.erb ------------Some custom facts variables------------- fact_certname=agent1.kisspuppet.com fact_apply3=mysql fact_apply4=java fact_apply10=db2 ... ---------------------------------------------------- [root@puppetserver agents]# cat modules/agent1/manifests/init.pp class agent1{ include agent1::facts } class agent1::facts{ file{ "/etc/mcollective/facts.txt": owner => "root", group => "root", mode => 0400, content => template("agent1/facts.txt.erb"), backup => 'main', } } ... [root@puppetserver agents]# cat modules/agent2/templates/facts.txt.erb ------------Some custom facts variables------------- fact_certname=agent2.kisspuppet.com fact_apply2=php fact_apply3=mysql fact_apply7=nginx ... ----------------------------------------------------

3 创建fact模块

3.1 创建全局模块

新建一个模块可命名为public,放在environment(存放基础环境的模块)模块中,自定义fact fact_apply.rb(过滤各自的自定义fact信息)

[root@puppetserver puppet]# cat environment/modules/public/lib/facter/fact_apply.rb # certname is usered for /etc/puppet/puppet.conf Facter.add("fact_certname") do setcode do Facter::Util::Resolution.exec("/bin/grep 'fact_certname=' /etc/mcollective/facts.txt |awk -F= '{print $2}'") end end # fact_apply1~N.rb # Facter.add("fact_apply1") do setcode do Facter::Util::Resolution.exec("/bin/grep 'fact_apply1=' /etc/mcollective/facts.txt |awk -F= '{print $2}'") end end Facter.add("fact_apply2") do setcode do Facter::Util::Resolution.exec("/bin/grep 'fact_apply2=' /etc/mcollective/facts.txt |awk -F= '{print $2}'") end end … Facter.add("fact_apply10") do setcode do Facter::Util::Resolution.exec("/bin/grep 'fact_apply10=' /etc/mcollective/facts.txt |awk -F= '{print $2}'") end end3.2 设置局部模块

如果自定义的fact属于某一个模块下具有的特性,只需要将fact信息定义到对应的模块中即可,无需创建全局fact模块,比如放在mysql模块中等。

4 开启模块插件功能

当pluginsync选项设置为true后,就打开了“模块中的插件”功能。当agent连接到master时,每一个agent都会检查他们的模块中的自定义代码。Puppet会将这些自定义代码同步到相关的agent中。然后他们就能在这些agent中使用了。

[root@puppetserver ~]# vim /etc/puppet/puppet.conf [main] pluginsync = true … [root@agent2 ~]# vim /etc/puppet/puppet.conf [main] pluginsync = true …

5 节点上测试自定义fact

5.1 节点运行puppet命令更新

[root@agent2 ~]# puppet agent --test info: Retrieving plugin notice: /File[/var/lib/puppet/lib/facter/fact_apply.rb]/ensure: defined content as '{md5}03bdfe12d6f40fb8abe0bd407dab6d69' info: Loading downloaded plugin /var/lib/puppet/lib/facter/fact_apply.rb #自动下载 info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb info: Loading facts in /var/lib/puppet/lib/facter/fact_apply.rb #自动载入 info: Caching catalog for agent2.kisspuppet.com info: Applying configuration version '1381211740'5.2 通过节点查看自定义fact是否生效

[root@agent2 ~]# facter -p | grep fact_ fact_apply2 => php fact_apply3 => mysql fact_apply7 => nginx fact_certname => agent1.kisspuppet.com [root@agent1 facter]# facter -p | grep fact_ fact_certname => agent2.kisspuppet.com fact_apply10 => db2 fact_apply3 => mysql fact_apply4 => java6 MCollective客户端测试自定义fact

[root@puppetserver facter]# mco inventory agent1.kisspuppet.com | grep fact_ fact_apply10 => db2 fact_apply3 => mysql fact_apply4 => java fact_certname => agent1.kisspuppet.com [root@puppetserver facter]# mco inventory agent2.kisspuppet.com | grep fact_ fact_apply2 => php fact_apply3 => mysql fact_apply7 => nginx fact_certname => agent2.kisspuppet.com

7 通过自定义facter定位主机触发更新

7.1 触发更新fact_apply4=java的主机

自定义fact fact_apply4=‘java’的主机目前只有agent1

[root@puppetserver facter]# mco puppet -v runonce mco facts -v --with-fact fact_apply4='java' Discovering hosts using the mc method for 2 second(s) .... 1 * [ ============================================================> ] 1 / 1 agent1.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 1 / 1 Pass / Fail: 1 / 0 Start Time: Tue Oct 08 14:24:08 +0800 2013 Discovery Time: 2003.39ms Agent Time: 1091.75ms Total Time: 3095.14ms7.2 触发更新fact_apply3=mysql和系统为RHEL5.7的主机

自定义fact fact_apply3=‘mysql’的主机有agent1和agent2,系统为RHEL5.7的主机只有agent2(通过系统自带fact获取),取交集,只有agent2会被触发更新。

[root@puppetserver facter]# mco puppet -v runonce rpc --np -F operatingsystemrelease='5.7' -F fact_apply3='mysql' Discovering hosts using the mc method for 2 second(s) .... 1 agent2.kisspuppet.com : OK {:summary=> "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 1 / 1 Pass / Fail: 1 / 0 Start Time: Tue Oct 08 14:22:23 +0800 2013 Discovery Time: 2004.56ms Agent Time: 1092.00ms Total Time: 3096.56ms

有关通过自定义fact增强MCollective推送更新元数据的灵活性的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  8. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  9. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐