零基礎學習Puppet自動化配置管理系列文檔html
上一節講解了puppet基礎環境模塊puppet,除此以外影響puppet基礎環境的還有一個模塊叫yum源,固然這個是相對於RedHat系統而言的,若是是SLES系統,就要配置zypper源了,其它Linux系統也是如此。那麼配置yum源須要用到哪些資源呢?node
以前寫puppet模塊的時候用到了file資源、service資源、package資源,那麼這三個資源是否能知足yum模塊的配置呢,答案是確定的。然而官方給出了專用的yumrepo資源,管理能夠精確到repo裏面的每一行,使用仍是很是方便的,接下來,咱們使用官方給出的yumrepo資源來配置yum模塊。vim
注:上一節教會你們如何一步步建立一個完整模塊,爲了不重複,這一節就直接貼配置了。服務器
一、yum包須要被安裝;微信
二、yum主配置文件yum.conf須要配置正確;學習
三、每臺主機至少有兩個repo源,一個指向本地的ISO源,一個指向自定義的puppet源;測試
四、不一樣系統版本的repo源中的部分參數略有不一樣,好比baseurl。ui
一、建立yum模塊目錄結構google
[root@puppetmaster modules]# tree yum yum ├── files ├── manifests └── templates 3 directories, 0 files
二、建立package資源url
[root@puppetmaster manifests]# vim install.pp class yum::install{ package { 'yum': ensure => installed, #要求yum這個包處於安裝狀態 } }
三、建立params.pp
根據操做系統版本定義repo文件中的各項條目
eg. [root@agent1 ~]# facter | grep operatingsystemrelease 系統版本fact operatingsystemrelease => 5.7
因爲RedHat存在多個版本,不一樣版本yum源的指向不一樣,對應的pki認證文件也不一樣,所以應當設置一些變量,而後進行引用。如下只定義了系統版本爲5.七、5.八、和6.4的變量,若是有其它版本效仿便可。
[root@puppetmaster manifests]# vim params.pp class yum::params { case $operatingsystemrelease{ 5.7: { $yum_redhat_descr = 'rhel base rpm packages' #定義redhat光盤源的描述信息 $yum_puppet_descr = 'puppet rpm packages for rhel' #定義puppet源的描述信息 $yum_redhat_pki = 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel5' #定義redhat光盤源的pki認證文件位置 $yum_redhat_baseurl = 'file:///media/cdrom/Server' #定義redhat光盤源baseurl的下載位置 $yum_puppet_baseurl = 'ftp://puppetmaster.kisspuppet.com/RHEL5U7' #定義puppet源baseurl的下載位置 $yum_redhat_pki_name = '/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel5' #定義puppet源pki認證文件位置 $yum_redhat_pki_download = 'puppet:///modules/yum/PM-GPG-KEY/RPM-GPG-KEY-redhat-release-rhel5' #定義pki文件的服務器下載地址 } 5.8: { $yum_redhat_descr = 'rhel base rpm packages' $yum_puppet_descr = 'puppet rpm packages for rhel' $yum_redhat_pki = 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel5' $yum_redhat_baseurl = 'file:///media/cdrom/Server' $yum_puppet_baseurl = 'ftp://puppetmaster.kisspuppet.com/RHEL5U8' $yum_redhat_pki_name = '/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel5' $yum_redhat_pki_download = 'puppet:///modules/yum/PM-GPG-KEY/RPM-GPG-KEY-redhat-release-rhel5' } 6.4: { $yum_redhat_descr = 'rhel base rpm packages' $yum_puppet_descr = 'puppet rpm packages for rhel' $yum_redhat_pki = 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel6' $yum_redhat_baseurl = 'file:///media/cdrom' $yum_puppet_baseurl = 'ftp://puppetmaster.kisspuppet.com/RHEL6U4' $yum_redhat_pki_name = '/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel6' $yum_redhat_pki_download = 'puppet:///modules/yum/PM-GPG-KEY/RPM-GPG-KEY-redhat-release-rhel6' } default: { #定義若是沒有以上版本的系統,直接報如下錯誤,同時也是爲了方便調試 fail("Module yum is not supported on ${::operatingsystem}") } } }
四、建立config.pp文件
config.pp文件用於管理yum主配置文件yum.conf,repo文件的屬性,pki文件的屬性及下載地址和yumrepo源
[root@puppetmaster manifests]# vim config.pp class yum::config{ include yum::params #引用class yum::params include yum::config_file,yum::config_key,yum::config_repo } class yum::config_file{ file { '/etc/yum.conf': #建立file資源管理yum主配置文件yum.conf ensure => present, #要求文件處於存在狀態 owner => 'root', #屬主爲root group => 'root', #屬組爲root mode => '0644', #文件權限爲644 source => 'puppet:///modules/yum/etc/yum.conf', #要求從puppetmaster服務器指定目錄去下載 require => Class['yum::install'], #要求在配置以前先安裝yum軟件包 } file { '/etc/yum.repos.d/rhel-base.repo': #設置光盤repo的一些屬性 ensure => present, owner => 'root', group => 'root', mode => '0644', require => Class['yum::config_repo'], #要求設置以前yumrepo資源rhel-base必須存在 } file { '/etc/yum.repos.d/rhel-puppet.repo': #設置puppet repo的一些屬性 ensure => present, owner => 'root', group => 'root', mode => '0644', require => Class['yum::config_repo'], #要求設置以前yumrepo資源puppet必須存在 } } class yum::config_key{ #設置pki證書的一些屬性及下載位置 file { $yum::params::yum_redhat_pki_name: ensure => present, owner => 'root', group => 'root', mode => '0644', source => $yum::params::yum_redhat_pki_download, } } class yum::config_repo{ yumrepo { rhel-base: #建立yumrepo資源rhel-base descr => $yum::params::yum_redhat_descr, #設置描述信息 baseurl => $yum::params::yum_redhat_baseurl, #設置yum源下載地址 enabled => 1, #激活yum源 gpgcheck => 1, #設置要求經過pki校驗 gpgkey => $yum::params::yum_redhat_pki, #設置pki文件的下載位置 require => Class['yum::config_key'], #要求這個文件必須存在 priority => 1, #設置repo的優先級爲1(數字越小優先級越高) } yumrepo { rhel-puppet: descr => $yum::params::yum_puppet_descr, baseurl => $yum::params::yum_puppet_baseurl, enabled => 1, gpgcheck => 0, priority => 2, } }
五、建立init.pp文件
因爲params.pp文件中設置的變量名稱引用太長,這裏能夠在init.pp中將變量名簡化,方便引用。
class yum( $yum_redhat_descr = $yum::params::yum_redhat_descr, # $yum_puppet_descr = $yum::params::yum_puppet_descr, $yum_redhat_pki = $yum::params::yum_redhat_pki, $yum_redhat_baseurl = $yum::params::yum_redhat_baseurl, $yum_puppet_baseurl = $yum::params::yum_puppet_baseurl, $yum_redhat_pki_name = $yum::params::yum_redhat_pki_name, $yum_redhat_pki_download = $yum::params::yum_redhat_pki_download ) inherits yum::params { #設置這些變量依賴於yum::params類 include yum::config,yum::install #包含全部子class }
所以、上面定義的class yum::config_key和yum::config_repo能夠寫成如下格式
class yum::config_key{ #設置pki證書的一些屬性及下載位置 file { $yum_redhat_pki_name: ensure => present, owner => 'root', group => 'root', mode => '0644', source => $yum_redhat_pki_download, } } class yum::config_repo{ yumrepo { rhel-base: #建立yumrepo資源rhel-base descr => $yum_redhat_descr, #設置描述信息 baseurl => $yum_redhat_baseurl, #設置yum源下載地址 enabled => 1, #激活yum源 gpgcheck => 1, #設置要求經過pki校驗 gpgkey => $yum_redhat_pki, #設置pki文件的下載位置 require => Class['yum::config_key'], #要求這個文件必須存在 priority => 1, #設置repo的優先級爲1(數字越小優先級越高) } yumrepo { rhel-puppet: descr => $yum_puppet_descr, baseurl => $yum_puppet_baseurl, enabled => 1, gpgcheck => 0, priority => 2, } }
六、建立puppet.conf和pki文件
[root@puppetmaster yum]# tree files files ├── etc │ └── yum.conf #能夠從節點/etc/目錄下copy一個yum.conf文件進行配置管理 └── PM-GPG-KEY ├── RPM-GPG-KEY-puppet-release #本身作一個pki文件,如何作,請google ├── RPM-GPG-KEY-redhat-release-rhel5 #在RHEL5系統/etc/pki/rpm-gpg/目錄下面有對應的pki文件,將其命個別名便可 └── RPM-GPG-KEY-redhat-release-rhel6 #在RHEL6系統/etc/pki/rpm-gpg/目錄下面有對應的pki文件,將其命個別名便可 2 directories, 4 files
七、應用到節點上
[root@puppetmaster modules]# vim /etc/puppet/manifests/site.pp $puppetmaster = 'puppetmaster.kisspuppet.com' class environments{ include motd,puppet,yum } node default{ include environments }
八、在agent1上進行測試
[root@agent1 yum.repos.d]# mv * /tmp/ #將全部的repo文件移動到/tmp目錄下 [root@agent1 yum.repos.d]# puppet agent -t #運行一次puppet更新動做,能夠經過如下日誌看出更新 info: Caching catalog for agent1_cert.kisspuppet.com info: Applying configuration version '1395696487' info: create new repo rhel-puppet in file /etc/yum.repos.d/rhel-puppet.repo notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-puppet]/descr: descr changed '' to 'puppet rpm packages for rhel' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-puppet]/baseurl: baseurl changed '' to 'ftp://puppetmaster.kisspuppet.com/RHEL5U7' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-puppet]/enabled: enabled changed '' to '1' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-puppet]/gpgcheck: gpgcheck changed '' to '0' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-puppet]/priority: priority changed '' to '2' info: changing mode of /etc/yum.repos.d/rhel-puppet.repo from 600 to 644 info: create new repo rhel-base in file /etc/yum.repos.d/rhel-base.repo notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-base]/descr: descr changed '' to 'rhel base rpm packages' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-base]/baseurl: baseurl changed '' to 'file:///media/cdrom/Server' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-base]/enabled: enabled changed '' to '1' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-base]/gpgcheck: gpgcheck changed '' to '1' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-base]/gpgkey: gpgkey changed '' to 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel5' notice: /Stage[main]/Yum::Config_repo/Yumrepo[rhel-base]/priority: priority changed '' to '1' info: changing mode of /etc/yum.repos.d/rhel-base.repo from 600 to 644 notice: Finished catalog run in 0.51 seconds [root@agent1 yum.repos.d]# ls rhel-base.repo rhel-puppet.repo [root@agent1 yum.repos.d]# cat rhel-base.repo #查看更新的光盤源文件 [rhel-base] name=rhel base rpm packages baseurl=file:///media/cdrom/Server enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release-rhel5 priority=1 [root@agent1 yum.repos.d]# cat rhel-puppet.repo #插件更新的puppet源文件 [rhel-puppet] name=puppet rpm packages for rhel baseurl=ftp://puppetmaster.kisspuppet.com/RHEL5U7 enabled=1 gpgcheck=0 priority=2
說明:關於puppet的資源目前大概有48種,這裏就不一一介紹了,詳情可訪問http://docs.puppetlabs.com/references/stable/type.html
微信公衆號:puppet2014,可微信搜索加入,也能夠掃描如下二維碼進行加入
QQ交流羣:296934942