Foreman架構的引入10-hostgroup如何轉換為本地的fact
在Foreman上可以根據業務邏輯設置多個主機組(Host Groups),并且可以將不同的節點加入到不同的主機組,這樣在每次操作“puppet run”的時候,只需要在搜索按鈕里搜索對應的主機組即可找到里面包含的所有節點,如下圖所示
但是,foreman目前在`puppet run`上對mcollective的集成度很低,基本就是只能運行一條命令。那么如果要在shell終端上通過mco命令去對這些自定義的`Host Groups`進行操作應該如何做呢。答案是轉換為facter。
自定義facter有四種方式,如下:[http://kisspuppet.com/2014/03/30/puppet_learning_base10/](http://kisspuppet.com/2014/03/30/puppet_learning_base10/)
這里介紹第三種方式將Foreman上設置的主機組(Host Groups)轉換為每個節點自己的facter
### 1、首先創建主機組
### 2、查看節點的主機組信息
其實相當于自定義了一個外部變量,變量名叫hostgroup,值為節點加入的組名稱
### 3、編寫一個fact模塊
模塊的功能就是將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 %>
~~~
### 4、Foreman上管理主機組和模塊fact
先導入類,然后在主機組里進行關聯即可,由于fact模塊是針對所有主機的,建議關聯到1級主機組,加入的節點會自動繼承。關聯完成后的效果如下
### 5、在Foreman上對兩個節點執行“puppet run”操作
### 6、查看facter信息是否生成
~~~
[root@foreman163 ~]# facter hostgroup
prd
[root@puppetmaster162 ~]# facter hostgroup
prd/kisspuppet
~~~
### 7、通過mco命令結合fact進行過濾查看
~~~
[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很強大,看你怎么用。
- 序
- 第一章:Puppet基礎篇
- 編寫此系列文檔的目的
- 如何學習和使用Puppet
- 安裝Puppet前期的準備工作
- 安裝、配置并使用Puppet
- 如何建立master和agent之間的認證關系
- Puppet更新方式的選型
- 編寫第一個完整測試模塊puppet
- 編寫第二個完整測試模塊yum
- Puppetmaster多環境配置
- 自定義fact實現的四種方式介紹
- 第二章:Puppet擴展篇
- 自定義fact結合ENC(hirea)的應用實踐
- 如何使用虛擬資源解決puppet沖突問題
- 如何擴展master的SSL傳輸性能(apache)
- 如何擴展master的SSL傳輸性能(nginx)
- 通過多進程增強master的負載均衡能力(nginx+mongrel)
- 通過橫向擴展puppetmaster增加架構的靈活性
- puppet代碼與版本控制系統的結合
- Puppet dashboard的部署及測試
- 第三章:MCollective架構篇
- MCollecitve架構的引入
- MCollective+MQ架構的部署
- Puppet插件的部署及測試
- MCollective各種插件的部署及測試
- MCollective安全性設計
- MQ的安全性設計
- 多MQ下MCollective高可用部署
- 第四章:Foreman架構的引入
- Foreman作為自動化運維工具為什么會如此強大
- 安裝前環境準備
- 安裝Foreman1.5架構(all-in-one)
- 安裝Foreman1.6架構(foreman與puppetmaster分離)
- 安裝Foreman1.7架構(源碼,僅測試使用)
- 整合puppetmaster
- Foreman結合mcollective完成push動作
- Foreman結合puppetssh完成push動作
- Foreman的ENC環境與fact環境的對比
- hostgroup如何轉換為本地的fact
- 智能變量與puppet模塊參數化類的結合
- Foreman報告系統的使用
- Foreman-proxy如何做負載均衡
- Foreman上如何展現代碼及文件內容
- Foreman如何和虛擬化管理軟件結合
- 如何借助Foreman完成自動化部署操作系統(一)
- 如何借助Foreman完成自動化部署操作系統(二)
- Foreman CLI(Hammer)工具的使用
- Foreman目前的不足之處