通过案例了解puppet创建虚拟资源解决资源冲突问题
通過案例了解puppet創(chuàng)建虛擬資源解決資源沖突問題
虛擬資源是一種用來管理多種配置共同依賴同一資源的方法。如果多個(gè)類依賴同一個(gè)資源時(shí)則可避免寫多個(gè)資源,也可以解決資源重定義的錯(cuò)誤。 虛擬資源經(jīng)常用于用戶管理中,虛擬資源只會(huì)被聲明一次,但可以運(yùn)用一次或多次。
要使用虛擬資源是需要在資源聲明開頭加上字符“@”來使資源虛擬化。然后再使用下面兩種方法之一來實(shí)例化虛擬資源:
"飛船"語法<||>
realize函數(shù)
更多詳情可訪問 www.kisspuppet.com ?www.rsyslog.org
1. 定義兩個(gè)用戶,puppet和root,并將其虛擬化
注意定義虛擬資源必須在全局作用域或者節(jié)點(diǎn)作用域中定義,簡單的理解,以下目錄中site.pp就是全局作用域,包含(site.pp中import了nodes目錄),其他模塊中的都屬于局部作用域。
1.1 在全局作用域中創(chuàng)建對應(yīng)的pp文件
[root@linuxmaster1poc testing]# tree manifests/ manifests/ ├── nodes │ ├── puppetclient.pp │ ├── virtual_group.pp │ └── virtual_user.pp └── site.pp 1 directory, 4 files
1.2 創(chuàng)建虛擬用戶puppet、root、xiaonuo
[root@linuxmaster1poc testing]# vim manifests/nodes/virtual_user.pp class alluser{include alluser::puppet,alluser::root } class alluser::puppet{@user { 'puppet':ensure => present,uid => '52',gid => '52',home => '/var/lib/puppet',shell => '/sbin/nologin',} } class alluser::root{@user { 'root':ensure => present,uid => '0',gid => '0',home => '/root',shell => '/bin/bash',} } class alluser::xiaonuo{@user { 'xiaonuo':ensure => present,uid => '600',gid => '600',home => '/home/xiaonuo',shell => '/sbin/nologin',} }1.3 創(chuàng)建虛擬組puppet、root和xiaonuo
[root@linuxmaster1poc testing]# vim manifests/nodes/virtual_group.pp class allgroup{include allgroup::puppet,allgroup::root } class allgroup::puppet{@group { 'puppet':ensure => present,name => 'puppet',gid => '52',allowdupe => false,members => 'puppet',} } class allgroup::root{@group { 'root':ensure => present,name => 'root',gid => '0',allowdupe => false,members => 'root',} } class allgroup::xiaonuo{@group { 'xiaonuo':ensure => present,name => 'xiaonuo',gid => '600',allowdupe => false,members => 'xiaonuo',} }2. 編寫puppet模塊,將虛擬資源用戶puppet和組puppet實(shí)化
2.1 編寫pupppet模塊
[root@linuxmaster1poc testing]# tree environment/modules/puppet environment/modules/puppet ├── files ├── manifests │ ├── config.pp │ ├── init.pp │ ├── install.pp │ ├── params.pp │ └── service.pp ├── README └── templates├── auth.conf.erb├── namespaceauth.conf.erb└── puppet.conf.erb 3 directories, 9 files2.2 編寫puppet_linux57poc模塊
[root@linuxmaster1poc testing]# tree agents/modules/puppet_linux57poc/ agents/modules/puppet_linux57poc/ ├── files ├── manifests │ └── init.pp └── templates├── facts.txt.erb└── motd.erb 3 directories, 3 files2.3 實(shí)例化虛擬資源
2.3.1 在puppet模塊中實(shí)例化
[root@linuxmaster1poc testing]# vim environment/modules/puppet/manifests/config.pp class puppet::config{include puppet::paramsinclude puppet::puppet_config,puppet::namespaceauth_config,puppet::auth_config,puppet::user,puppet::groupinclude alluser,allgroup #必須將節(jié)點(diǎn)作用域中的類包含進(jìn)來 } class puppet::puppet_config{file { '/etc/puppet/puppet.conf':ensure => present,content => template('puppet/puppet.conf.erb'),owner => 'puppet',group => 'puppet',mode => '0644',backup => main,require => Class['puppet::install','puppet::user','puppet::group'],notify => Class['puppet::service'],} } class puppet::auth_config{file { '/etc/puppet/auth.conf':ensure => present,content => template('puppet/auth.conf.erb'),owner => 'puppet',group => 'puppet',mode => '0644',backup => main,require => Class['puppet::install','puppet::user','puppet::group'],notify => Class['puppet::service'],} } class puppet::namespaceauth_config{file { '/etc/puppet/namespaceauth.conf':ensure => present,content => template('puppet/namespaceauth.conf.erb'),owner => 'puppet',group => 'puppet',mode => '0644',backup => main,require => Class['puppet::install','puppet::user','puppet::group'],notify => Class['puppet::service'],} } class puppet::user{ #使用飛船語法實(shí)化用戶puppet資源 # realize User['puppet']User <| title == 'puppet' |> } class puppet::group{ #使用realize函數(shù)實(shí)化組puppet資源realize Group['puppet'] # Group <| title == 'puppet' |> }2.3.2 在puppet_linux57poc模塊中實(shí)例化
[root@linuxmaster1poc testing]# cat agents/modules/puppet_linux57poc/manifests/init.pp class puppet_linux57poc{include puppet_linux57poc::motd_install,puppet_linux57poc::motd_config,puppet_linux57poc::facts,puppet_linux57poc::user,puppet_linux57poc::groupinclude alluser,allgroup #必須將節(jié)點(diǎn)作用域中的類包含進(jìn)來 } class puppet_linux57poc::motd_install{package{ setup:ensure => present,} } class puppet_linux57poc::motd_config{file{ "/etc/motd":owner => "xiaonuo",group => "root",mode => 0440,content => template("puppet_linux57poc/motd.erb"),backup => 'main',require => Class['puppet_linux57poc::motd_install','puppet_linux57poc::user','puppet_linux57poc::group']} } class puppet_linux57poc::facts{file{ "/etc/mcollective/facts.txt":owner => "root",group => "root",mode => 0400,content => template("puppet_linux57poc/facts.txt.erb"),backup => 'main',require => Class['puppet_linux57poc::motd_install','puppet_linux57poc::user','puppet_linux57poc::group']} } class puppet_linux57poc::user{ #使用realize函數(shù)實(shí)化用戶xiaonuo和root資源realize( User['xiaonuo'],User['root'] ) } class puppet_linux57poc::group{ #使用realize函數(shù)實(shí)化組xiaonuo和root資源realize( Group['xiaonuo'],Group['root'] ) }3. 測試
3.1 測試puppet模塊(略)
3.2 測試puppet_linux57poc模塊
3.2.1 查看當(dāng)前系統(tǒng)是否有xiaonuo用戶和組
[root@linux57poc puppet]# id xiaonuo id: xiaonuo: No such user [root@linux57poc puppet]# cat /etc/group | grep xiaonuo [root@linux57poc puppet]# [root@linux57poc puppet]# ll /etc/motd -rwxrwxrwx 1 puppet puppet 313 Jan 2 06:17 /etc/motd3.2.2 同步puppetmaster
[root@linux57poc puppet]# puppet agent -t --environment=testing info: Retrieving plugin info: Loading facts in /var/lib/puppet/lib/facter/fact_apply.rb info: Caching catalog for puppet_linux57poc.dev.shanghaigm.com info: Applying configuration version '1389555288' notice: /Stage[main]/Allservice::Lm_sensors_service/Service[lm_sensors]/ensure: ensure changed 'running' to 'stopped' notice: /Group[xiaonuo]/ensure: created notice: /Stage[main]/Alluser::Xiaonuo/User[xiaonuo]/ensure: created ... info: FileBucket adding {md5}b2090646c444c5ddf1533749743ebd71 info: /Stage[main]/Mcollective::Facter/File[/etc/mcollective/facts.yaml]: Filebucketed /etc/mcollective/facts.yaml to main with sum b2090646c444c5ddf1533749743ebd71 notice: /Stage[main]/Sysctl::Exec/Exec[sysctl -p >/dev/null &]/returns: executed successfully notice: /Stage[main]/Puppet_linux57poc::Motd_config/File[/etc/motd]/owner: owner changed 'puppet' to 'xiaonuo' notice: /Stage[main]/Puppet_linux57poc::Motd_config/File[/etc/motd]/group: group changed 'puppet' to 'root' notice: /Stage[main]/Puppet_linux57poc::Motd_config/File[/etc/motd]/mode: mode changed '0777' to '0440' notice: /Stage[main]/Allservice::Bluetooth_service/Service[bluetooth]/ensure: ensure changed 'running' to 'stopped' notice: Finished catalog run in 4.54 seconds3.2.3 驗(yàn)證結(jié)果是否正確
[root@linux57poc puppet]# id xiaonuo uid=600(xiaonuo) gid=600(xiaonuo) groups=600(xiaonuo) [root@linux57poc puppet]# cat /etc/group | grep xiaonuo xiaonuo:x:600: [root@linux57poc puppet]# ll /etc/motd -r--r----- 1 xiaonuo root 313 Jan 2 06:17 /etc/motd [root@linux57poc puppet]#
轉(zhuǎn)載于:https://blog.51cto.com/dreamfire/1351173
總結(jié)
以上是生活随笔為你收集整理的通过案例了解puppet创建虚拟资源解决资源冲突问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net导出Excel
- 下一篇: sicily 1150. 简单魔板