设为首页 收藏本站
查看: 897|回复: 0

[经验分享] 运维自动化之puppet资源(1)

[复制链接]

尚未签到

发表于 2018-8-2 13:19:36 | 显示全部楼层 |阅读模式
  什么是puppet资源?
  puppet是一种Linux、Unix、windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。
  定义资源语法:
  每个资源必须有一个type一个title和一些属性。
type {'title':  attribute => value,
  
}
  一个简单示例:
vim test.pp  
notify {'notice':
  message => 'hello world!',
  
}    #保存,退出
  
puppet apply test.pp     #执行
  #在定义时,资源类型必须使用小写字符。
  #资源名称仅是一个字符串,但要求在同一个类型中其必须唯一。
  puppet describe --list  #可查看资源列表。
  puppet describe notify  #可查看资源的详细信息。
  资源间的依赖关系:
  puppet提供了before、require、notify和subscribe四个元参数来定义资源间的相关性。
  -这四个元参数都以另外的其他资源或资源数组作为其值,这也称作资源引用
  -资源引用要通过“Type['title']”的方式进行,如User['wang390750']
  …注意:资源引用时,其类型名的首字母要大写。
  before……Causes a resource to be applied before the target resource
  require……Causes a resource to be applied after the target resource
  notify……Causes a resource to be applied before the target resource
  ……The target resource will refresh if the notifying resource changes
  subscribe……Causes a resource to be applied after the target resource
  ……The subscribing resource will refresh if the target resource changes
  before示例:
[root@localhost tmp]# cat before.pp  
package {'httpd':
  
ensure => installed,
  
before => Service['httpd'],
  
}
  
service {'httpd':
  
ensure => true,
  
}
  
[root@localhost tmp]# puppet apply before.pp
  
Notice: Compiled catalog for localhost.localdomain in environment production in 0.33 seconds
  
Notice: /Stage[main]/Main/Package[httpd]/ensure: created
  
Notice: /Stage[main]/Main/Service[httpd]/ensure: ensure changed 'stopped' to 'running'
  
Notice: Finished catalog run in 70.95 seconds
  
[root@localhost tmp]# rpm -q httpd
  
httpd-2.2.15-29.el6.centos.x86_64
  
[root@localhost tmp]# netstat -ntpl | grep httpd
  
tcp        0      0 :::80                       :::*                        LISTEN      20692/httpd
  require示例:
[root@localhost 桌面]# cat require.pp  
group {'test':
  
ensure => present,
  
gid => 10001,
  
}
  
user {'test':
  
gid=>10001,
  
uid=>10001,
  
home => '/home/test',
  
managehome => true,
  
ensure => present,
  
require => Group['test'],
  
}
  
[root@localhost 桌面]# puppet apply require.pp
  
Notice: Compiled catalog for localhost.localdomain in environment production in 0.14 seconds
  
Notice: /Stage[main]/Main/Group[test]/ensure: created
  
Notice: /Stage[main]/Main/User[test]/ensure: created
  
Notice: Finished catalog run in 0.95 seconds
  
[root@localhost 桌面]# cat /etc/group | grep test
  
test:x:10001:
  
[root@localhost 桌面]# cat /etc/passwd | grep test
  
test:x:10001:10001::/home/test:/bin/bash
  notify示例:
[root@localhost 桌面]# netstat -ntpl | grep sendmail  
\tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      28156/sendmail
  
[root@localhost 桌面]# puppet apply notify.pp
  
Notice: Compiled catalog for localhost.localdomain in environment production in 0.10 seconds
  
Notice: /Stage[main]/Main/File[/root/command]/ensure: defined content as '{md5}1a39b842b386c79250fc70fdf68dbb14'
  
Notice: /Stage[main]/Main/Exec[/bin/bash /root/command]/returns: executed successfully
  
Notice: /Stage[main]/Main/Exec[/bin/bash /root/command]: Triggered 'refresh' from 1 events
  
Notice: Finished catalog run in 0.56 seconds
  
[root@localhost 桌面]# netstat -ntpl | grep sendmail
  
[root@localhost 桌面]#
  
[root@localhost 桌面]# cat notify.pp
  
file {'/root/command':
  
ensure => file,
  
source => '/tmp/test',
  
mode => '755',
  
owner => 'root',
  
group => 'root',
  
notify => Exec['/bin/bash /root/command'],
  
}
  
exec {'/bin/bash /root/command':
  
path => '/bin:/sbin:/usr/bin:/usr/sbin',
  
user => root,
  
group => root,
  
}
  
[root@localhost 桌面]# cat /tmp/test
  
/etc/init.d/sendmail stop
  subscribe示例:
[root@localhost tmp]# cat subscribe.pp  
package {'httpd':
  
ensure => installed,
  
}
  
service {'httpd':
  
ensure => true,
  
subscribe => Package['httpd'],
  
}
  资源间的应用次序链:
  "->"用来定义次序链,而"~>"用于定义通知链。
  他们既可以用于资源应用间,也可以用于资源申报之间。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-545422-1-1.html 上篇帖子: centos 搭建 puppet 下篇帖子: 在Puppet中用ERB模板来自动配置Apache虚拟主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表