puppet的默認資源node
默認資源能夠爲資源初始化屬性和值,一般默認資源聲明在site.pp文件首部,代碼以下:linux
[root@sh-web1 ~]# cat site.pp Exec { path => '/usr/bin:/bin:/usr/sbin:/sbin'}
聲明默認資源注意事項以下:nginx
一、聲明默認資源時首字母須要大寫,如exec聲明默認資源Exec、package聲明默認資源Package等.web
二、若是聲明資源有一個名稱空間資源"::",它的每一個環節都須要首字母大寫,如Concat::Fragment.shell
Exec默認資源的聲明方法以下:bash
Exec { path => '/usr/bin:/bin:/usr/sbin:/sbin'}
經過Exec默認資源聲明path屬性的環境變量值,在後續聲明exec資源時能夠直接調用系統命令而不用擔憂環境變量的問題.app
Package {provider => 'rpm'} #Package首字母大寫 package {"nginx":}
在默認資源中聲明provider屬性,指定包的安裝方式爲rpm,後續package資源中provider屬性均爲rpm.dom
puppet虛擬化資源ide
虛擬化資源與普通資源的區別,虛擬化資源定之後要先實例化再使用,而普通資源定義後直接能夠使用,定義虛擬化資源的方法是在資源前追加@,如@user,這時的user資源就是一個虛擬化資源.在代碼文件中將資源轉換爲虛擬資源後,puppet在執行的時候並不會調用它,若是想執行,須要經過realize函數或者"<||>"來實例化一個虛擬資源.函數
示例一:
但願在本機只建立test用戶.
建立用戶的puppet代碼以下:
class user { @user {"ops": ensure => present, home => '/data/home/ops', shell => '/bin/bash', } @user {"test": ensure => present, home => '/data/home/test', shell => '/bin/bash', } }
node節點調用:
node base { include admin } node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { include nginx } "sh-web1": { include user realize (User['test']) } } }
注意:若是是普通資源的話include user時應該是上面定義的2個用戶都被建立,可是定義爲虛擬資源時realize實例化只建立了1個用戶.
puppet運行的結果:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version '1509554205' Notice: /Stage[main]/User/User[test]/ensure: created Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: Finished catalog run in 0.22 seconds [root@sh-web1 ~]# cat /etc/passwd | grep test test:x:502:502::/data/home/test:/bin/bash [root@sh-web1 ~]# cat /etc/passwd | grep ops
示例二:
安裝nginx,普通資源定義:
init.pp文件.
class nginx { include app::nginx include web::nginx }
app.pp文件.
class app::nginx { package {"nginx": ensure => 'present', } }
web.pp文件.
class web::nginx { package {"nginx": ensure => 'present', } }
node節點引用:
node base { include admin } node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { # include nginx } "sh-web1": { include nginx } } }
puppet 更新:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: Package[nginx] is already declared in file /etc/puppet/modules/nginx/manifests/app.pp:4; cannot redeclare at /etc/puppet/modules/nginx/manifests/web.pp:4 on node sh-web1.localdomain Warning: Not using cache on failed catalog Error: Could not retrieve catalog; skipping run
註釋:報錯資源重複定義.
解決方案:使用虛擬資源定義解決:
nginx模塊下init.pp文件、app.pp文件、web.pp文件內容:
class nginx { include app::nginx include web::nginx @package {"nginx": ensure => installed} }
class app::nginx { realize (Package['nginx']) }
class web::nginx { realize (Package['nginx']) }
node節點引用:
node base { include admin } node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { # include nginx } "sh-web1": { include nginx } } }
puppet agent端更新:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version '1509555656' Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created Notice: Finished catalog run in 4.02 seconds
註釋:適用於多版本的nginx定義.
示例三:
實例化一個虛擬資源除了系統提供的realize函數外,還能夠用"<||>".
安裝nginx爲例:
nginx模塊下的init.pp文件.
class nginx { include app::nginx include web::nginx @package {"nginx": ensure => installed} }
nginx模塊下的app.pp文件.
class app::nginx { Package<| title =='nginx' |> }
nginx模板下的web.pp文件.
class web::nginx { Package<| title =='nginx' |> }
node節點文件node.pp文件.
node base { include admin } node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { # include nginx } "sh-web1": { include nginx } } }
puppet agent端更新:
[root@sh-web1 ~]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version '1509704319' Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created Notice: Finished catalog run in 9.20 seconds [root@sh-web1 ~]# rpm -qa nginx nginx-1.10.2-1.el6.x86_64