前面的两篇Blog介绍了如何部署Puppet;
本篇Blog,将详细描述如何使用Puppet。


比如,我们需要在系统中创建一个组【god_group】

我们先需要查看系统当前拥有的用户组:

[root@puppet puppet]# cat /etc/group | grep god       
[root@puppet puppet]# 

可以看到,其中并不包含我们希望创建的【god_group】

编写Puppet的资源文件(清单,manifest):

[root@puppet puppet]# vi group.pp
[root@puppet puppet]# 
[root@puppet puppet]# cat group.pp 
group{'group_god':
name => 'god_group',
ensure => present,
gid => 5000,
}
[root@puppet puppet]# 

尝试运行:

[root@puppet puppet]# puppet apply -v --noop group.pp 
Info: Loading facts in /etc/puppet/modules/mcollective/lib/facter/mco_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Notice: Compiled catalog for puppet.adamhuan.com in environment production in 0.16 seconds
Info: Applying configuration version '1565138975'
Notice: /Stage[main]/Main/Group[group_god]/ensure: current_value absent, should be present (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.09 seconds
[root@puppet puppet]# 
[root@puppet puppet]# cat /etc/group | grep god       
[root@puppet puppet]# 

正式运行:

[root@puppet puppet]# puppet apply -v group.pp        
Info: Loading facts in /etc/puppet/modules/mcollective/lib/facter/mco_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Notice: Compiled catalog for puppet.adamhuan.com in environment production in 0.11 seconds
Info: Applying configuration version '1565138997'
Notice: /Stage[main]/Main/Group[group_god]/ensure: created
Notice: Finished catalog run in 0.03 seconds
[root@puppet puppet]# 
[root@puppet puppet]# cat /etc/group | grep god
god_group:x:5000:
[root@puppet puppet]# 

可以看到,我们期望的组【god_group】已经按照预期被创建了。


创建一个用户【lenka】:

[root@puppet puppet]# vi add_user.pp
[root@puppet puppet]# 
[root@puppet puppet]# cat add_user.pp 
user{'Lenka Liu':
ensure => present,
system => false,
comment => 'Liu Dream Sea',
shell => '/bin/bash',
home => '/home/lenka',
managehome => true,
groups => 'god_group',
uid => 2000,
name => 'lenka',
}
[root@puppet puppet]# 
[root@puppet puppet]# cat /etc/passwd | grep lenka
[root@puppet puppet]# 
[root@puppet puppet]# puppet apply -v add_user.pp 
Info: Loading facts in /etc/puppet/modules/mcollective/lib/facter/mco_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Notice: Compiled catalog for puppet.adamhuan.com in environment production in 0.24 seconds
Info: Applying configuration version '1565139795'
Notice: /Stage[main]/Main/User[Lenka Liu]/ensure: created
Notice: Finished catalog run in 0.43 seconds
[root@puppet puppet]# 
[root@puppet puppet]# cat /etc/passwd | grep lenka
lenka:x:2000:2000:Liu Dream Sea:/home/lenka:/bin/bash
[root@puppet puppet]# 
[root@puppet puppet]# id lenka
uid=2000(lenka) gid=2000(lenka) groups=2000(lenka),5000(god_group)
[root@puppet puppet]# 
[root@puppet puppet]# ls -ltr /home
total 0
drwx------. 2 adamhuan adamhuan 62 Jul 15 19:19 adamhuan
drwx------  2 lenka    lenka    62 Aug  6 18:03 lenka
[root@puppet puppet]# 
[root@puppet puppet]# ls -ltra /home/lenka/
total 12
-rw-r--r--  1 lenka lenka 231 May 22  2018 .bashrc
-rw-r--r--  1 lenka lenka 193 May 22  2018 .bash_profile
-rw-r--r--  1 lenka lenka  18 May 22  2018 .bash_logout
drwxr-xr-x. 4 root  root   35 Aug  6 18:03 ..
drwx------  2 lenka lenka  62 Aug  6 18:03 .
[root@puppet puppet]# 

获得Puppet中的某个类型的帮助信息:

[root@puppet puppet]# puppet describe package -s -m

package
=======
Manage packages.  There is a basic dichotomy in package
support right now:  Some package types (e.g., yum and apt) can
retrieve their own package files, while others (e.g., rpm and sun)
cannot.  For those package formats that cannot retrieve their own files,
you can use the `source` parameter to point to the correct file.
Puppet will automatically guess the packaging format that you are
using based on the platform you are on, but you can override it
using the `provider` parameter; each provider defines what it
requires in order to function, and you must meet those requirements
to use a given provider.
**Autorequires:** If Puppet is managing the files specified as a
package's `adminfile`, `responsefile`, or `source`, the package
resource will autorequire those files.


Parameters
----------
    adminfile, allow_virtual, allowcdrom, category, configfiles,
    description, ensure, flavor, install_options, instance, name,
    package_settings, platform, responsefile, root, source, status,
    uninstall_options, vendor

Meta Parameters
---------------
    alias, audit, before, loglevel, noop, notify, require, schedule, stage,
    subscribe, tag

Providers
---------
    aix, appdmg, apple, apt, aptitude, aptrpm, blastwave, dpkg, fink,
    freebsd, gem, hpux, macports, msi, nim, openbsd, opkg, pacman, pip, pkg,
    pkgdmg, pkgin, pkgutil, portage, ports, portupgrade, rpm, rug, sun,
    sunfreeware, up2date, urpmi, windows, yum, zypper
[root@puppet puppet]# 

安装软件包【nginx】

当前,我的系统是没有安装【nginx】的:

[root@puppet puppet]# yum list | grep nginx
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository extras is listed more than once in the configuration
collectd-nginx.x86_64                   5.8.1-1.el7                     epel    
munin-nginx.noarch                      2.0.49-3.el7                    epel    
nextcloud-nginx.noarch                  10.0.4-2.el7                    epel    
nginx.x86_64                            1:1.12.2-3.el7                  epel    
nginx-all-modules.noarch                1:1.12.2-3.el7                  epel    
nginx-filesystem.noarch                 1:1.12.2-3.el7                  epel    
nginx-mod-http-geoip.x86_64             1:1.12.2-3.el7                  epel    
nginx-mod-http-image-filter.x86_64      1:1.12.2-3.el7                  epel    
nginx-mod-http-perl.x86_64              1:1.12.2-3.el7                  epel    
nginx-mod-http-xslt-filter.x86_64       1:1.12.2-3.el7                  epel    
nginx-mod-mail.x86_64                   1:1.12.2-3.el7                  epel    
nginx-mod-stream.x86_64                 1:1.12.2-3.el7                  epel    
owncloud-nginx.noarch                   9.1.5-1.el7                     epel    
pcp-pmda-nginx.x86_64                   4.1.0-5.el7_6                   updates 
python2-certbot-nginx.noarch            0.36.0-1.el7                    epel    
[root@puppet puppet]# 
[root@puppet puppet]# rpm -qa | grep nginx
[root@puppet puppet]# 

使用Puppet安装:

[root@puppet puppet]# vi install_package_by_yum.puppet
[root@puppet puppet]# 
[root@puppet puppet]# cat install_package_by_yum.puppet 
package{'nginx':
ensure => installed,
provider => yum
}
[root@puppet puppet]# 
[root@puppet puppet]# puppet apply -v install_package_by_yum.puppet 
Info: Loading facts in /etc/puppet/modules/mcollective/lib/facter/mco_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Notice: Compiled catalog for puppet.adamhuan.com in environment production in 0.05 seconds
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')
Info: Applying configuration version '1565140220'

Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Notice: Finished catalog run in 26.93 seconds
[root@puppet puppet]# 
[root@puppet puppet]# rpm -qa | grep nginx                          
nginx-mod-mail-1.12.2-3.el7.x86_64
nginx-1.12.2-3.el7.x86_64
nginx-filesystem-1.12.2-3.el7.noarch
nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64
nginx-mod-http-image-filter-1.12.2-3.el7.x86_64
nginx-mod-stream-1.12.2-3.el7.x86_64
nginx-all-modules-1.12.2-3.el7.noarch
nginx-mod-http-perl-1.12.2-3.el7.x86_64
nginx-mod-http-geoip-1.12.2-3.el7.x86_64
[root@puppet puppet]# 

可以看到,已经安装好了。

不过,上面有一段警告:

Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
   (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')

这段警告不影响安装,不过看上去可能不太友好。

可以通过上面告警中的描述,设置【allow_virtual】为【false】,从而禁用该条警告;具体如下:

以安装【http】为例:

[root@puppet puppet]# rpm -qa | grep http
nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64
nginx-mod-http-image-filter-1.12.2-3.el7.x86_64
nginx-mod-http-perl-1.12.2-3.el7.x86_64
nginx-mod-http-geoip-1.12.2-3.el7.x86_64
[root@puppet puppet]# 
[root@puppet puppet]# cat install_package_by_yum.puppet 
package{'httpd':
ensure => installed,
provider => yum,
allow_virtual => false,
}
[root@puppet puppet]# 
[root@puppet puppet]# puppet apply -v install_package_by_yum.puppet 
Info: Loading facts in /etc/puppet/modules/mcollective/lib/facter/mco_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/package_provider.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/pe_version.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/puppet_settings.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
Info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/service_provider.rb
Notice: Compiled catalog for puppet.adamhuan.com in environment production in 0.04 seconds
Info: Applying configuration version '1565140371'
Notice: /Stage[main]/Main/Package[httpd]/ensure: created
Notice: Finished catalog run in 25.62 seconds
[root@puppet puppet]# 
[root@puppet puppet]# rpm -qa | grep http                           
nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64
nginx-mod-http-image-filter-1.12.2-3.el7.x86_64
httpd-tools-2.4.6-89.el7.centos.1.x86_64
httpd-2.4.6-89.el7.centos.1.x86_64
nginx-mod-http-perl-1.12.2-3.el7.x86_64
nginx-mod-http-geoip-1.12.2-3.el7.x86_64
[root@puppet puppet]# 

可以看到,这次就没有警告了。


终了,…

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒