通过案例了解puppet创建虚拟资源解决资源冲突问题

虚拟资源是一种用来管理多种配置共同依赖同一资源的方法。如果多个类依赖同一个资源时则可避免写多个资源,也可以解决资源重定义的错误。 虚拟资源经常用于用户管理中,虚拟资源只会被声明一次,但可以运用一次或多次。

要使用虚拟资源是需要在资源声明开头加上字符“@”来使资源虚拟化。然后再使用下面两种方法之一来实例化虚拟资源:

  • "飞船"语法<||>

  • realize函数

更多详情可访问 www.kisspuppet.com  www.rsyslog.org

1. 定义两个用户,puppet和root,并将其虚拟化

注意定义虚拟资源必须在全局作用域或者节点作用域中定义,简单的理解,以下目录中site.pp就是全局作用域,包含(site.pp中import了nodes目录),其他模块中的都属于局部作用域。

1.1 在全局作用域中创建对应的pp文件

[root@linuxmaster1poc testing]# tree manifests/
manifests/
├── nodes
│   ├── puppetclient.pp
│   ├── virtual_group.pp
│   └── virtual_user.pp
└── site.pp
1 directory, 4 files

1.2 创建虚拟用户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 创建虚拟组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实化

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 files

2.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 files

2.3 实例化虚拟资源

2.3.1 在puppet模块中实例化

[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 #必须将节点作用域中的类包含进来
}
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{ #使用飞船语法实化用户puppet资源
#  realize User['puppet']User <| title == 'puppet' |>
}
class puppet::group{ #使用realize函数实化组puppet资源realize Group['puppet']
#  Group <| title == 'puppet' |>
}

2.3.2 在puppet_linux57poc模块中实例化

[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 #必须将节点作用域中的类包含进来
}
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函数实化用户xiaonuo和root资源realize( User['xiaonuo'],User['root'] )
}
class puppet_linux57poc::group{ #使用realize函数实化组xiaonuo和root资源realize( Group['xiaonuo'],Group['root'] )
}

3. 测试

3.1 测试puppet模块(略)

3.2 测试puppet_linux57poc模块

3.2.1 查看当前系统是否有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/motd

3.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 seconds

3.2.3 验证结果是否正确

[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]#

转载于:https://blog.51cto.com/dreamfire/1351173

通过案例了解puppet创建虚拟资源解决资源冲突问题相关推荐

  1. Git创建branch 与 解决分支冲突

    由于之前写一个Spring项目的时候是自己和一位大佬一起协作的,在中间差点因为我的git命令不熟悉而导致我的本地分支错误合并.这里仔细写一下git创建分支时候的一些坑和解决分支冲突时候的一些方法. 先 ...

  2. 怎么linux中创建虚拟路径,linux服务器创建虚拟路径解决文件上传路径隔离问题...

    需求环境 图片上传最简单的就是上传web项目下,这样图片与项目不可分离会产生很多不必要的影响.例如:重新部署项目需要把所有上传的图片再copy一份等. 图片与项目分离有好几种方式: 方式一.在linu ...

  3. 案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版

    案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版 网上众说纷纭: 有说是:select-editor问题? 有说是 ...

  4. puppet成长日记二 Package资源详细介绍及案例分析

    puppet成长日记二 Package资源详细介绍及案例分析 一.系统环境 1.puppet服务端 Release:RHEL6.4 HOSTNAME: puppetserver.rsyslog.org ...

  5. vsftpd创建虚拟用户,解决本地用户不安全问题,增强服务器安全性

    转载来源 : vsftpd创建虚拟用户,解决本地用户不安全问题,增强服务器安全性 : 摘要: 前面两篇介绍VSFTPd服务器的匿名用户和本地用户访问,基本满足文件的上传下载任务,今天给大家说说虚拟用户 ...

  6. 教你解决无法创建虚拟拨号连接的问题

    用户经常遇到无法创建虚拟拨号连接的问题,这是因为操作系统禁用了服务,可以通过下面的批处理解决: @echo PCVPN解决网吧限制 @pause  sc config  TapiSrv start= ...

  7. 创建虚拟机时提示“已拒绝虚拟机配置。请参见浏览器控制”的解决方法

    创建虚拟机时提示"已拒绝虚拟机配置.请参见浏览器控制"的解决方法 上面的问题出现在 VMwareESXi 6.5u2和VMwareESXi 6.7环境下,当然其他环境也会出现 这个 ...

  8. 【VirtualBox】VirtualBox使用现有的虚拟盘文件(如VHD)创建虚拟机时,报错:打开虚拟硬盘失败,“UUID already exist”的解决方法

    ###0.问题描述 使用现有的虚拟盘文件(如VHD)创建虚拟机时,报错:打开虚拟硬盘失败,"UUID already exists"的错误. ###1.参考博客 https://w ...

  9. iis5.1安装方法(适用于XP)以及运行调试asp程序,创建虚拟目录【整理】

    Author:张继飞 写在前面:因为要运行asp程序,建立一个小小的网站,呵呵.所以需要安装iis对环境进行安装设置.下面是从网上找到的一些资料,并加上自己的总结,成为一个整篇的方法介绍,为大家寻找一 ...

最新文章

  1. Shell函数返回值、删除函数、在终端调用函数
  2. K - TimesIsMoney(查询)
  3. scala 连接oracle_一分钟教你学会用java连接Oracle数据库
  4. spock 集成测试_Spock 1.2 –轻松进行集成测试中的Spring Bean模拟
  5. linux内核源码 -- list链表
  6. 【每天学点Python】案例四:52周存钱挑战
  7. WordPress页面教程【2021】
  8. Python语言的应用前景如何,应用方向有哪些。
  9. 【mysql】mysql查询结果添加固定值
  10. Linux系统之管道符
  11. pytorch优化器详解:Adam
  12. 2022.7.19 防火墙知识点
  13. 如何删除ActiveX控件
  14. 2.SpringBoot学习(二)——Spring Boot ConfigurationProperties
  15. 零基础入门网络渗透到底要怎么学?
  16. java保留小数点后7位,不够补0,去小数点存库,带小数点展示
  17. 弱连通和强连通_基本图论-连通分量(强/弱联通 割点/边 边/点双)
  18. Bootstrap图标、下拉菜单、按钮组、按钮式下拉菜单
  19. AVR单片机中ATmega8的AD转换探究
  20. 【MapReduce】实战:流量统计(完整Java代码)

热门文章

  1. UIAlertController 简单修改title以及按钮的字体颜色
  2. Django 之Form
  3. 需求:整个网站不能出现以下两个链接。思路:检索到网站中凡是出现该链接的都让它的href值为空...
  4. app——分享wap站,数据处理页面展示
  5. Cocos2d-x列表嵌套裁剪bug
  6. 企业大数据运用实战案例分享
  7. 利用 sys.sysprocesses 检查 Sql Server的阻塞和死锁
  8. 任天堂新音樂遊戲上市
  9. OpenCV单kinect多帧静止场景的深度图像去噪
  10. 高性能IO -Reactor模式的实现