溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Puppet模塊(二):YUM模塊及Yumrepo資源和Mount資源

發(fā)布時間:2020-06-19 12:27:41 來源:網(wǎng)絡(luò) 閱讀:10753 作者:月晴星飛 欄目:編程語言

作用:自動為客戶端配置YUM源,為使用yum安裝軟件包提供便捷。


1、服務(wù)端配置yum模塊

(1)模塊清單

[root@puppet ~]# tree /etc/puppet/modules/yum/
/etc/puppet/modules/yum/
├── files
│   ├── yum.conf
│   └── RPM-GPG-KEY-CentOS-6
├── manifests
│   ├── config.pp
│   ├── init.pp
│   ├── install.pp
│   └── params.pp
└── templates

    說明:這里只針對centos 6.5版本測試,其他的可以此類推,需要注意的就是本地源的光盤加載問題(見測試環(huán)節(jié))

(2)定義參數(shù)類

[root@puppet ~]# vi /etc/puppet/modules/yum/manifests/params.pp
class yum::params {
  case $operatingsystemrelease{
    6.5: {
      $yum_centos_descr = 'centos base rpm packages'   #本地倉庫描述信息
      $yum_centos_baseurl = 'file:///media/cdrom/'     #IOS文件或光盤mount到本地的路徑
      $yum_centos_pki = 'file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6' #YUMREPO文件中指定PKI文件路徑
      $yum_centos_pki_name = '/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6'   #PKI文件路徑,復(fù)制一份到puppet的modules中
      $yum_centos_pki_download = 'puppet:///modules/yum/RPM-GPG-KEY-CentOS-6'
      $yum_puppet_descr = 'puppet rpm packages for centos'    #puppet倉庫描述信息
      $yum_puppet_baseurl = 'http://yum.puppetlabs.com/el/6/products/$basearch'   #puppet倉庫路徑
      $yum_163_descr = '163 rpm packages for centos'          #163倉庫描述信息
      $yum_163_baseurl = 'http://mirrors.163.com/centos/$releasever/contrib/$basearch/'  #163倉庫路徑
    }
    default: { 
      fail("Module yum is not supported on ${::operatingsystem}") #6.5版本以外的會提示不支持,如有5.5、5.4這樣的版本可按參考6.5進(jìn)行參數(shù)定義
    }
  }
}

    注意:PKI文件RPM-GPG-KEY-CentOS-6是光盤中的存在的,系統(tǒng)不一樣,名稱也不一樣,需要確認(rèn)。

    說明:變量$operatingsystemrelease是通過facter獲取計算機(jī)的信息的,運(yùn)行下面的命令可查看計算機(jī)相關(guān)信息:

[root@puppet ~]# facter | grep operatingsystemrelease
operatingsystemrelease => 6.5

(3)定義安裝類

[root@puppet ~]# vi /etc/puppet/modules/yum/manifests/install.pp
class yum::install{
  #1、確保yum被安裝
  package { 'yum':
    ensure => installed,
  }
  #2、創(chuàng)建光盤掛載目錄
  file { '/media/cdrom':
    ensure => directory,
    owner  => root,
    group  => root,
  }
  #3、確保光盤被掛載
#方法一:EXEC
#  exec { "mount_cdrom":
#    path    => "/usr/bin:/usr/sbin:/bin",
#    command => '/bin/mount /dev/cdrom /media/cdrom/',
#    creates => '/media/cdrom/RPM-GPG-KEY-CentOS-6',
#    require => File['/media/cdrom'],
#  }
#方法二:MOUNT
  mount { 'mount_cdrom':
    name     => "/media/cdrom",       #掛載到的目標(biāo)目錄
    ensure   => mounted,              #要求被掛載狀態(tài)
    fstype   => "iso9660",            #光盤的文件類型,使用mount命令或cat /etc/fstab查看
    device   => "/dev/cdrom",         #光盤的設(shè)備,是個鏈接,實(shí)際指向/dev/sr0
    options  => "ro",                 #以只讀方式掛載光盤
    atboot   => true,                 #允許開機(jī)啟動時掛載上
    remounts => true,                 #允許重新掛載
    require  => File['/media/cdrom'], #要求掛載目標(biāo)目錄存在
  }
  #4、安裝源前先備份
  file { '/etc/yum.repos.d/bak':
    ensure  => directory,
    owner   => root,
    group   => root,
  }
  exec { "repos_bak":
    path    => "/usr/bin:/usr/sbin:/bin",
    command => 'mv -f /etc/yum.repos.d/[^c]*.repo /etc/yum.repos.d/bak/',  #將不是小寫c開頭的repo文件強(qiáng)制移動到備份目錄
    creates => '/etc/yum.repos.d/bak/CentOS-Base.repo',   #當(dāng)備份目錄存在這個文件時不再執(zhí)行此資源
    require => File['/etc/yum.repos.d/bak'],              #要求備份目錄存在
  }
}

    說明:新生成的repo文件都是小寫c開頭的,因此將不是c開頭([^c]*)的repo文件移至bak,

原文件都是CentOS開頭的,外加一個puppetlabs.repo,可以將[^c]*.repo改成CentOS*.repo,只將原文件備份。

(4)定義配置類

[root@puppet ~]# vi /etc/puppet/modules/yum/manifests/config.pp
class yum::config{
  include yum::params
  include yum::config_file,yum::config_key,yum::config_repo
}
#1、定義配置文件
class yum::config_file{
  file { '/etc/yum.conf':               #yum主配置文件yum.conf路徑
    ensure  => present,                 #要求文件處于存在狀態(tài)
    owner   => 'root',                  #屬主為root
    group   => 'root',                  #屬組為root
    mode    => '0644',                  #文件權(quán)限為644
    source  => 'puppet:///modules/yum/yum.conf', #自動搜索yum模塊下的files目錄,因此省略/files
    require => Class['yum::install'],   #要求在配置之前先安裝yum軟件包
  }
  file { '/etc/yum.repos.d/centos-base.repo':  #設(shè)置光盤repo的一些屬性,文件名是yumrepo中的標(biāo)題名定義的,必須一致
    ensure  => present,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    require => Class['yum::config_repo'],      #要求設(shè)置之前yumrepo資源centos-base必須存在
  }
  file { '/etc/yum.repos.d/centos-puppet.repo':
    ensure  => present,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    require => Class['yum::config_repo'], 
  }
  file { '/etc/yum.repos.d/centos-163.repo': 
    ensure  => present,
    owner   => 'root',
    group   => 'root',
    mode    => '0644',
    require => Class['yum::config_repo'], 
  }
}
#2、定義pki證書文件
class yum::config_key{  #設(shè)置pki證書的一些屬性及下載位置
  file { $yum::params::yum_centos_pki_name: #證書名稱在參數(shù)類中定義
    ensure => present,
    owner  => 'root',
    group  => 'root',
    mode   => '0644',
    source => $yum::params::yum_centos_pki_download, #證書下載地址在參數(shù)類中定義
  }
}
#3、定義基本yum倉庫、puppet倉庫和163倉庫
class yum::config_repo{  
  yumrepo { centos-base:                          #創(chuàng)建yumrepo資源cenos-base
    descr    => $yum::params::yum_centos_descr,   #設(shè)置描述信息
    baseurl  => $yum::params::yum_centos_baseurl, #設(shè)置yum源下載地址
    enabled  => 1,                                #激活yum源
    gpgcheck => 1,                                #設(shè)置要求通過pki校驗(yàn)
    gpgkey   => $yum::params::yum_centos_pki,     #設(shè)置pki文件的位置
    require  => Class['yum::config_key'],         #要求pki文件必須存在
    priority => 1,                                #設(shè)置repo的優(yōu)先級為1(越小越高)
  }
  yumrepo { centos-puppet:
    descr    => $yum::params::yum_puppet_descr,
    baseurl  => $yum::params::yum_puppet_baseurl,
    enabled  => 1,
    gpgcheck => 0,                                #不要求通過pki校驗(yàn)
    priority => 2,
  }
  yumrepo { centos-163:
    descr    => $yum::params::yum_163_descr,
    baseurl  => $yum::params::yum_163_baseurl,
    enabled  => 1,
    gpgcheck => 0,
    priority => 3,
  }
}

    說明:創(chuàng)建了三個YUM源,都以centos(小寫)開頭的,客戶端安裝puppet時生成了puppetlabs.repo源,因此這里的centos-puppet源也可以省略。

(7)定義yum主類

[root@puppet ~]# vi /etc/puppet/modules/yum/manifests/init.pp
class yum {
  include yum::params,yum::install,yum::config
}

(8)定義節(jié)點(diǎn)文件,調(diào)用模塊

[root@puppet ~]# vi /etc/puppet/manifests/centostest.pp
node "centostest.ewin.com" {
  include ntp, yum
}

(9)應(yīng)用節(jié)點(diǎn)文件

[root@puppet ~]# vi /etc/puppet/manifests/site.pp
import "centostest.pp"

(10)復(fù)制文件

    將以下兩個文件從客戶端復(fù)制一份到服務(wù)端/etc/puppet/modules/yum/files下

/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

/etc/yum.conf

    注意在文件頭部加上以***釋,測試的時候方便分辨

### config  by  puppet ###


2、測試

(1)客戶端執(zhí)行測試

[root@centostest ~]# puppet agent --server puppet.ewin.com -test --noop
Info: Caching catalog for centostest.ewin.com
Info: Applying configuration version '1415332221'
Notice: Finished catalog run in 1.09 seconds

(2)查看客戶端日志

[root@centostest ~]# tailf /var/log/messages 
Nov  7 11:50:20 centostest puppet-agent[8809]: (/Stage[main]/Yum::Install/File[/media/cdrom]/ensure) created
Nov  7 11:50:20 centostest puppet-agent[8809]: (/Stage[main]/Yum::Install/Exec[mount_cdrom]/returns) executed successfully
Nov  7 11:50:22 centostest puppet-agent[8809]: (/Stage[main]/Yum::Install/File[/etc/yum.repos.d/bak]/ensure) created
Nov  7 11:50:22 centostest puppet-agent[8809]: (/Stage[main]/Yum::Install/Exec[repos_bak]/returns) executed successfully
Nov  7 11:50:23 centostest puppet-agent[8809]: (/Stage[main]/Yum::Config_key/File[/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6]/content) content changed '{md5}e8e57fd1a55dc5c6d82e60a444781b96' to '{md5}a27c559bf7660f101317ac3b41a7600b'
Nov  7 11:50:23 centostest puppet-agent[8809]: (/Stage[main]/Yum::Config_repo/Yumrepo[centos-base]/ensure) created
Nov  7 11:50:24 centostest puppet-agent[8809]: (/Stage[main]/Yum::Config_repo/Yumrepo[centos-puppet]/ensure) created
Nov  7 11:50:24 centostest puppet-agent[8809]: (/Stage[main]/Yum::Config_repo/Yumrepo[centos-163]/ensure) created
Nov  7 11:50:24 centostest puppet-agent[8809]: (/Stage[main]/Yum::Config_file/File[/etc/yum.conf]/content) content changed '{md5}5d8b0bf30a8ee9d66a9cb2642186ac37' to '{md5}8c1fab4142147877a3f77f89eb8ccb7c'
Nov  7 11:50:24 centostest puppet-agent[8809]: Finished catalog run in 4.27 seconds

    結(jié)論:可看到cdrom目錄創(chuàng)建、掛載命令執(zhí)行成功、備份命令成功、同步了PKI文件和CON文件、創(chuàng)建了三個YUMREPO

(3)查看客戶端掛載的光盤目錄

[root@centostest ~]# ls /media/cdrom/
CentOS_BuildTag  isolinux                  RPM-GPG-KEY-CentOS-Debug-6
EFI              Packages                  RPM-GPG-KEY-CentOS-Security-6
EULA             RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Testing-6
GPL              repodata                  TRANS.TBL
p_w_picpaths           RPM-GPG-KEY-CentOS-6

    結(jié)論:掛載成功,如果用的EXEC方法將會判斷RPM-GPG-KEY-CentOS-6存在時不會再執(zhí)行Exec,而且此文件是原文件,不是新建的空文件。

    經(jīng)測試umount /media/cdrom卸載光盤后會自動重啟掛載上,甚至卸載再刪除/media/cdrom目錄,也會恢復(fù),有興趣的可以試試。

(4)查看YUM源備份

[root@centostest ~]# ls /etc/yum.repos.d/bak/
CentOS-Base.repo       CentOS-Media.repo   CentOS-Vault.repo
CentOS-Debuginfo.repo  puppetlabs.repo

(5)查看客戶端同步的文件

[root@centostest ~]# cat /etc/yum.conf 
### config  by  puppet ###
[main]
...
[root@centostest ~]# cat /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
### config  by  puppet ###
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.5 (GNU/Linux)
...

    結(jié)論:從文件頭部注釋信息可以確認(rèn)文件同步成功

(6)查看YUM list

[root@centostest ~]# yum clean all
Loaded plugins: fastestmirror, refresh-packagekit, security
Cleaning repos: base puppetlabs-deps puppetlabs-products
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@centostest ~]# yum list
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
centos-163                                               | 2.9 kB     00:00     
centos-163/primary_db                                    | 1.2 kB     00:00     
centos-base                                              | 4.0 kB     00:00 ... 
centos-base/primary_db                                   | 4.4 MB     00:00 ...
centos-puppet                                            | 2.5 kB     00:00     
centos-puppet/primary_db  13%  [=-            ]  5.9 B/s |  17 kB     --:-- ETA

   結(jié)論:YUM三個倉庫都能成功加載,通過yum clean all再yum list可觀察。

[root@centostest ~]# yum list puppet
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
http://mirrors.163.com/centos/6/contrib/x86_64/repodata/repomd.xml: 
[Errno 12] Timeout on http://mirrors.163.com/centos/6/contrib/x86_64/repodata/repomd.xml: 
(28, 'Operation too slow. Less than 1 bytes/sec transfered the last 30 seconds')
Trying other mirror.
Installed Packages
puppet.noarch                  3.7.1-1.el6                  @puppetlabs-products
Available Packages
puppet.noarch                  3.7.3-1.el6                  centos-puppet

    結(jié)論:可以看到從163的源查找puppet超時,已安裝的包是從@puppetlabs安裝的,有效的包是在centos-puppet源中。


3、yum倉庫資源

yumrepo { "repo163":      #YUM源文件.repo的文件名
  descr    => "163 repo", #倉庫的描述
  baseurl  => "http://mirrors.163.com/centos/$releasever/contrib/$basearch/", #倉庫的url
  gpgcheck => "0",        #是否檢查倉庫中的軟件包GPG簽名,可設(shè)置的值為0或者1.
  enabled  => "1",        #是否開啟或者禁用倉庫,可設(shè)置的值為1或者0.1為開啟,0為禁用
}
{
  enablegroups    => ,  #是否可以使用yumgroup參數(shù),如yumgroup list,yumgroup install等
exclude         => ,  #排除那些軟件的安裝與更新,支持shell通配符。
failovermethod  => ,  #可設(shè)置的值為priority,roundrobin.
gpgkey          => ,  #倉庫的軟件包簽名,GPG密鑰的URL。
include         => ,  #包含配置url.
includepkgs     => ,  #只有匹配的軟件包才能安裝或者更新。
keepalive       => ,  #設(shè)置http/1.1選項(xiàng),可設(shè)置的為0或者1.
metadata_expire => ,  #元組數(shù)據(jù)的過期時間,單位時間為秒。
miirorlist      => ,  #倉庫的鏡像列表。
name            => ,  #倉庫名字。
priority        => ,  #優(yōu)先級,可設(shè)置的是從1-99.
protect         => ,  #啟用或者禁用對這個倉庫的保護(hù)??稍O(shè)置的值為0或者1.
proxy           => ,  #設(shè)置代理
}


4、mount掛載資源

  掛載共享文件夾

mount { "/mount":             #掛載的目標(biāo)目錄,等同于name參數(shù)
  device  => "192.168.0.1:/share/nfs",  #掛載的來源設(shè)備
  fstype  => "nfs",           #文件系統(tǒng)類型
  options => "_netdev,vers=3,tcp,rsize=8192,wsize=8192,noauto",  #其他選項(xiàng)
}

  掛載samba

mount {"/media":
  ensure => mounted,
  device => "http://172.22.2.89/public",
  fstype => cifs,
  options => "username=perofu,password=123456";
}

  參數(shù)說明

mount { 'mount_cdrom':
  name     => "/media/cdrom", #掛載到的目標(biāo)目錄
  ensure   => mounted, #要求被掛載狀態(tài)
  fstype   => "iso9660", #光盤的文件類型
  device   => "/dev/cdrom", #光盤的設(shè)備,是個鏈接,指向/dev/sr0
  options  => "ro", #以只讀方式掛載光盤
  atboot   => true, #允許開機(jī)啟動時掛載上
  remounts => true, #允許重新掛載
  require  => File['/media/cdrom'], #要求掛載目標(biāo)目錄存在
}

  查看文件系統(tǒng)類型:

[root@centostest ~]# mount
/dev/mapper/vg_centostest-lv_root on / type ext4 (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/vg_centostest-lv_home on /home type ext4 (rw)
/dev/sr0 on /media/cdrom type iso9660 (ro)

  發(fā)現(xiàn)以下報錯,是因?yàn)闆]有加上options => "ro",因?yàn)楣獗P是只讀的。

[mount_cdrom]) Could not evaluate: Execution of '/bin/mount /media/cdrom' returned 32: 
mount: block device /dev/sr0 is write-protected, mounting read-only


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI