零基礎學習Puppet自動化配置管理系列文檔shell
在Foreman上能夠根據業務邏輯設置多個主機組(Host Groups),而且能夠將不一樣的節點加入到不一樣的主機組,這樣在每次操做「puppet run」的時候,只須要在搜索按鈕裏搜索對應的主機組便可找到裏面包含的全部節點,以下圖所示ide
可是,foreman目前在puppet run
上對mcollective的集成度很低,基本就是隻能運行一條命令。那麼若是要在shell終端上經過mco命令去對這些自定義的Host Groups
進行操做應該如何作呢。答案是轉換爲facter。ui
自定義facter有四種方式,以下:http://kisspuppet.com/2014/03/30/puppet_learning_base10/spa
這裏介紹第三種方式將Foreman上設置的主機組(Host Groups)轉換爲每一個節點本身的facterdebug
其實至關於自定義了一個外部變量,變量名叫hostgroup,值爲節點加入的組名稱orm
模塊的功能就是將Foreman上的變量「hostgroup」落地到每一個節點的/etc/facter/facts.d/${hostname}.txt文件中,內容爲fact的標準格式。
#模塊結構 [root@puppetmaster162 modules]# tree fact fact ├── files ├── manifests │ ├── config.pp │ ├── fact.pp │ ├── init.pp │ └── params.pp └── templates └── hostgroup.erb 3 directories, 5 files #模塊主配置文件init.pp [root@puppetmaster162 modules]# cat fact/manifests/init.pp class fact { tag("puppet_env") require fact::params $hostgroup_erb = $fact::params::hostgroup_erb include fact::config include fact::facter } #建立目錄以及文件 [root@puppetmaster162 modules]# cat fact/manifests/config.pp class fact::config{ file { '/etc/facter' : ensure => directory, owner => 'root', group => 'root', mode => '0644', } file { '/etc/facter/facts.d' : ensure => directory, owner => 'root', group => 'root', mode => '0644', require => File['/etc/facter'] } file{ "/etc/facter/facts.d/$hostname.txt": owner => "root", group => "root", mode => 0400, content => template($fact::hostgroup_erb), require => File['/etc/facter/facts.d'], } } #定義變量 [root@puppetmaster162 modules]# cat fact/manifests/params.pp class fact::params{ $hostgroup_erb = 'fact/hostgroup.erb' } #定義fact模板(緣由可參考http://kisspuppet.com/2013/11/10/mcollective-middleware/) [root@puppetmaster162 manifests]# cat fact.pp class fact::facter{ file{"/etc/mcollective/facts.yaml": owner => root, group => root, mode => 0440, loglevel => debug, # reduce noise in Puppet reports content => inline_template('<%= scope.to_hash.reject { |k,v| k.to_s =~ /(uptime.*|path|timestamp|free|.*password.*|.*psk.*|.*key)/ }.to_yaml %>'), } } #設置文件模板 [root@puppetmaster162 modules]# cat fact/templates/hostgroup.erb hostgroup=<%= @hostgroup %> foreman_env=<%= @foreman_env %>
先導入類,而後在主機組裏進行關聯便可,因爲fact模塊是針對全部主機的,建議關聯到1級主機組,加入的節點會自動繼承。關聯完成後的效果以下
[root@foreman163 ~]# facter hostgroup prd [root@puppetmaster162 ~]# facter hostgroup prd/kisspuppet
[root@puppetmaster162 ~]# mco ping -F hostgroup=prd foreman163.kisspuppet.com time=98.55 ms ---- ping statistics ---- 1 replies max: 98.55 min: 98.55 avg: 98.55 [root@puppetmaster162 ~]# mco ping -F hostgroup=prd/kisspuppet puppetmaster162.kisspuppet.com time=94.14 ms ---- ping statistics ---- 1 replies max: 94.14 min: 94.14 avg: 94.14 [root@puppetmaster162 ~]# mco puppet -v runonce -F hostgroup=prd/kisspuppet Discovering hosts using the mc method for 2 second(s) .... 1 * [ ============================================================> ] 1 / 1 puppetmaster162.kisspuppet.com : OK {:summary=> "Started a Puppet run using the 'puppet agent --test --color=false --splay --splaylimit 30' command"} ---- rpc stats ---- Nodes: 1 / 1 Pass / Fail: 1 / 0 Start Time: Thu Dec 18 15:13:09 +0800 2014 Discovery Time: 2004.07ms Agent Time: 85.19ms Total Time: 2089.26ms
注:以上方式只是提供了一種思路,更多的方式還須要根據具體的實際環境而改變,總之一點,fact很強大,看你怎麼用。