自动化运维工具puppet学习笔记之基础篇

 
 
  1. **************服务端安装软件****************** 
  2. shell#touch /etc/puppet/manifests/site.pp 
  3. shell#cat /etc/hosts 
  4.    192.168.1.254 nat.test.com 
  5. shell#/etc/init.d/puppetmaster start 
  6. shell#puppet cert --sign test.test.com 
  7. 签发证书,需要为每一个域名单独签发 
  8.  
  9. **************客户端配置服务****************** 
  10. shell#cat /etc/hosts 
  11.   192.168.1.254 nat.test.com 
  12.   127.0.0.1 test.test.com 
  13. shell#echo  "server = nat.test.com" >>/etc/puppet/puppet.conf 
  14. shell#puppet agent --no-daemonize --verbose 
  15.  
  16. 服务端文件 
  17. vim /etc/puppet/manifests/site.pp  
  18. node default{ 
  19. file {"/tmp/a.txt": 
  20.      content => "test\n", 
  21.      ensure  => present, 
  22.      backup  => ".bak", 
  23.      mode    => 500, 
  24.      owner   => root, 
  25.      group   => www, 
  26.      } 
  27.  
  28.  
  29. *********************客户端测试**************************** 
  30. puppet agent --verbose  --test 
  31.  
  32. *********************服务端创建模块************************** 
  33. shell#mkdir /etc/puppet/modules/snmpd/{files,manifests,templates} -p 
  34. shell#vim  /etc/puppet/modules/snmpd/manifests 
  35. ###################################################################################################################### 
  36. class snmpd { 
  37.     service { 
  38.         "snmpd": 
  39.         enable    => "true", 
  40.         ensure    => "running", 
  41.         require   => File["snmpd.conf"], 
  42.         subscribe => File["snmpd.conf"], 
  43.         name => $operatingsystem ? { 
  44.             default => "snmpd", 
  45.             }, 
  46.     } 
  47.  
  48.     package { 
  49.         "net-snmp": 
  50.         ensure => present, 
  51.         name => $operatingsystem ? { 
  52.             debian => "snmpd", 
  53.             ubuntu => "snmpd", 
  54.             default => "net-snmp", 
  55.             }, 
  56.     } 
  57.      
  58.     file { 
  59.         "snmpd.conf": 
  60. #         owner  => root, 
  61. #        group  => root, 
  62. #        mode   => 644, 
  63.         require   => Package["net-snmp"], 
  64.         path    => $operatingsystem ?{ 
  65.                default => "/etc/snmp/snmpd.conf", 
  66.                }, 
  67.     } 
  68.  
  69. ##################################################################################################### 
  70.  
  71. vim /etc/puppet/manifests/site.pp 
  72. node default{ 
  73. file {"/tmp/a.txt": 
  74.      content => "This is a test file date\n", 
  75.      ensure  => present, 
  76.      backup  => ".back", 
  77.      mode    => 500, 
  78.      owner   => root, 
  79.      group   => www, 
  80.      } 
  81. node 'test.test.com' { 
  82. file {"/tmp/b.txt": 
  83.      content => "This is a test file\n", 
  84.      ensure  => present, 
  85.      backup  => ".back", 
  86.      mode    => 500, 
  87.      owner   => root, 
  88.      group   => root, 
  89.      } 
  90. include snmpd  #包含创建的类 

 

 
 
  1.  http://docs.puppetlabs.com/guides/types/service.html 原文地址 
  2. service管理系统运行的服务进程,不幸的是不同的系统管理服务的方式是多样的. 有些系统上面对于服务管理很简单,有些系统提供复杂的强大的服务管理功能.puppet提供最基本的服务管理,你也可以指定provider,使用一些特性. 
  3. 注意,当一个服务从另一个资源收到一个事件,服务会重启,例如配置文件修改,可以要求相应的服务重启.不同的平台重启命令不同,你也可以手工指定重启服务的命令. 
  4. 特性 
  5. controllable provider 提供control 变量 
  6. enableable provider 可以enable和disable服务 
  7. refreshable provider 可以重启服务 
  8. 例子service {             "ssh":             ensure => running;            "nfs":             ensure => stopped;           } 
  9. 参数binary 
  10. 运行服务的命令的路径, 只用于不支持init的操作系统, 如果没有指定启动脚本,就用这个命令来启动服务. 
  11. enable 
  12. 服务在开机的时候是否启动,可以设置的值是true和false,需要provider支持enableable 
  13. ensure 
  14. 是否运行服务, running表示运行服务,stopped 表示停止服务 
  15. hasrestart 
  16. 指出管理脚本是否支持restart参数,如果不支持,就用stop和start实现restart效果. 可以设置的值是true 或 false 
  17. hasstatus 
  18. 指出管理脚本是否支持status参数,puppet用status参数来判断服务是否已经在运行了,如果不支持status参数,puppet利用查找运行进程列表里面是否有服务名来判断服务是否在运行. 可以设置的值是true或false 
  19. name 
  20. 该资源的namevar, 服务的名字,通常就是在/etc/init.d/目录下的名字 
  21. path 
  22. 启动脚本的搜索路径,可以用冒号分割多个路径,或者用数组指定. 
  23. pattern 
  24. 设置搜索进程列表的匹配字符串,用于不支持init脚本的系统.当要停止一个服务的时候,通过查看进程运行列表来判断. 
  25. provider 
  26. puppet提供下面的provider(只列出常见的系统) 
  27. debian debian系统的init模式的管理脚本,支持 enableable, refreshable. 
  28. freebsd init模式,支持enableable, refreshable. 
  29. init 标准的init模式,支持refreshable 
  30. redhat redhat的init模式,支持enableable, refreshable. 
  31. smf solaris新的服务管理框架,支持enableable, refreshable 
  32. restart 
  33. 指定重启脚本,否则就先停止该服务再启动该服务 
  34. start 
  35. 指定启动服务的命令,通常init模式的管理脚本都支持,不需要手工指定 
  36. status 
  37. 指定status命令,如果不指定,就从进程列表查询该服务 
  38. stop 
  39. 指定停止服务的脚本. 

 http://nocap.blog.163.com/blog/static/19052507420121030113615705/

 

 
 
  1. 官方文档 
  2. http://docs.puppetlabs.com/puppetdocs-latest.tar.gz 
  3. http://www.puppetlabs.com/downloads/docs/puppet_labs_docs_pdfs.zip 
  4.  
  5.  
  6. puppet  cert    sign    mytestagent.example.com     
  7. puppet  cert    sign    --all    
  8. puppet  master  --no-daemonize  --verbose  
  9. puppet  --genconfig 
  10. puppet  agent   --server    myserver.example.com    --waitforcert   60  --test 
  11. puppet  master  --configprint   modulepath 
  12. puppet  config  print   modulepath  --mode  master 
  13.  
  14. auth.conf 
  15. autosign.conf 
  16. ========================================== 
  17. rebuilt.example.com 
  18. *.scratch.example.com 
  19. *.local 
  20. ========================================== 
  21.  
  22. device.conf 
  23. ========================================== 
  24. [device certname] 
  25.         type    <type> 
  26.         url <url> 
  27. [router6.example.com] 
  28.         type    cisco 
  29.         url ssh://admin:password@ef03c87a.local 
  30.  
  31.  
  32. fileserver.conf 
  33. =========================================== 
  34. #   Files   in  the /path/to/files  directory   will    be  served 
  35. #   at  puppet:///mount_point/. 
  36. [mount_point] 
  37. path    /path/to/files 
  38. allow   *.example.com 
  39. deny    *.wireless.example.com 
  40. =========================================== 
  41.  
  42. tagmail.conf 
  43. require 
  44. Set  report=true  on your agent nodes 
  45. Set  reports=tagmail 
  46. Set  the  reportfrom  email address and either  the  smtpserver  or  sendmail  setting on  the puppet master 
  47. Create a  tagmail.conf  file at  the  location specified  in  the   tagmap  setting 
  48. ocated at   /etc/puppet/tagmail.conf  by default 
  49. A comma-separated  list of  tags and  !negated  tags;  valid  tags  include: 
  50. Explicit  tags 
  51. Class names 
  52. Puppet Documentation ? Configuring Puppet 40/411 
  53. “ all ” 
  54. Any valid Puppet  log  level  ( debug ,   info ,   notice ,   warning ,   err ,   alert ,   emerg ,   crit ,  or 
  55. verbose ) 
  56. A colon 
  57. A comma-separated  list of email addresses 
  58. The  list of  tags on a  line builds  the set of  resources whose messages will be  included  in  the mailing; 
  59. each additional  tag adds  to  the set,  and each  !negated  tag subtracts  from  the set. 
  60. So,   for example: 
  61. ============================================================================================== 
  62. all:    log-archive@example.com 
  63. webserver,  !mailserver:    httpadmins@example.com 
  64. emerg,  crit:   james@example.com,  zach@example.com,   ben@example.com 
  65. ============================================================================================== 
  66.  
  67.  
  68. service {   'sshd': 
  69. subscribe   =>  File['sshdconfig'], 
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. define  svn_repo($path) { 
  79. exec    {   "/usr/bin/svnadmin  create  ${path}/${title}": 
  80. unless  =>  "/bin/test  -d  ${path}", 
  81. svn_repo    {   'puppet_repo':  path    =>  '/var/svn_puppet'   } 
  82. svn_repo    {   'other_repo':       path    =>  '/var/svn_other'    } 
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. define  svn_repo($path) { 
  92.      exec   {   "create_repo_${name}": 
  93.      command    =>  "/usr/bin/svnadmin  create  ${path}/${title}", 
  94.      unless     =>  "/bin/test  -d  ${path}", 
  95.      if $require    { 
  96.          Exec["create_repo_${name}"]    {  
  97.          require    +>  $require, 
  98. svn_repo    {   'puppet': 
  99.      path               =>  '/var/svn', 
  100.      Puppet Documentation ? Language Guide 50/411 
  101.      require    =>  Package['subversion'], 
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. 变量赋值 
  111. $value  =   "${one}${two}" 
  112.  
  113. 数组 
  114. $foo    =[ 'one', 'two', 'three' ] 
  115. notice  $foo[1] 
  116. 将返回 two 
  117.  
  118.  
  119. host { 'one.example.com': 
  120.          ensure =>  present, 
  121.          alias  =>  [ 'satu','dua', 'tiga'  ], 
  122.          ip =>  '192.168.100.1', 
  123.  
  124.  
  125. 哈希 
  126. $myhash ={ key1 => 'myval', key2 => $b  } 
  127.  
  128.  访问hash元素 
  129. $myhash ={ key  => { subkey => 'b' }} 
  130. notice($myhash[key][subkey])  
  131.  
  132. 节点赋不同的值 
  133. ========================================================= 
  134. node    a   { 
  135. $setting    =   'this' 
  136. include class_using_setting 
  137. node    b   { 
  138. $setting    =   'that' 
  139. include class_using_setting 
  140. ========================================================= 
  141.  
  142.  
  143. calss的设置 
  144. ========================================================================================= 
  145. $test   =   'top' 
  146. class   myclass { 
  147.         exec    {   "/bin/echo  ${test}":   logoutput   =>  true    } 
  148. class   other   { 
  149.         $test   =   'other' 
  150.         include myclass 
  151. include other 
  152. =========================================================================================== 
  153.  
  154. 访问class里面的值 
  155. ======================================================== 
  156. class   myclass { 
  157. $test   =   'content' 
  158. class   anotherclass    { 
  159. $other  =   $myclass::test 
  160. ======================================================== 
  161.  
  162. 给变量中的数组增加元素 
  163. ========================================================================== 
  164. $ssh_users  =   [   'myself',   'someone'   ] 
  165. class   test    { 
  166.         $ssh_users  +=  ['someone_else'] 
  167. =========================================================================== 
  168.  
  169. 这里的+=是给数组增加值 
  170.  
  171.  
  172.  
  173.  
  174. 变量选择相关 
  175.  
  176.  
  177. file{ '/etc/config': 
  178.         owner     =>    $operatingsystem ? { 
  179.         'sunos'   =>    'adm', 
  180.         'redhat'  =>    'bin', 
  181.         default   =>    undef, 
  182.    }, 
  183. 默认值为undef, 
  184.  
  185.  
  186.  
  187. $owner=$operatingsystem ? { 
  188.       /(redhat|debian)/=>'bin', 
  189.       default          =>undef, 
  190.  
  191.  
  192. $system=$operatingsystem ? { 
  193.       /(redhat|debian)/ => "our system  is $1", 
  194.       default           => "our system  is unknown", 
  195.  
  196. 这里的$1将会返回redhat或者debian 
  197. $0将会返回整行 
  198.  
  199.  
  200.  
  201. case 
  202. ===================================================================================================== 
  203. case    $operatingsystem { 
  204.     'sunos':    {   include solaris }   #   apply   the solaris class 
  205.     'redhat':   {   include redhat  }   #   apply   the redhat  class 
  206.     default:    {   include generic }   #   apply   the generic class 
  207. ==================================================================================================== 
  208. ======================================================================================================= 
  209. case    $hostname { 
  210. 'jack','jill':       {  include hill    }   #   apply   the hill    class 
  211. 'humpty','dumpty':   {  include wall    }   #   apply   the wall    class 
  212. default:         {  include generic }   #   apply   the generic class 
  213. ======================================================================================================== 
  214. 如果v$hostname fact里面含有jack或者jill,那么将会include  hill 
  215. ======================================================================================================== 
  216. case    $hostname   { 
  217.         /^j(ack|ill)$/: {  include  hill   }    #   apply   the hill    class 
  218.         /^[hd]umpty$/:  {  include  wall   }    #   apply   the wall    class 
  219.         default:        {  include generic }    #   apply   the generic class 
  220. =========================================================================================================== 
  221. case    $hostname  { 
  222.         /^j(ack|ill)$/: { notice("Welcome $1!") } 
  223.         default:    { notice("Welcome stranger")    } 
  224. =========================================================================================================== 
  225.  
  226. if $variable { 
  227.      file { '/some/file':   ensure  => present } 
  228. }  else { 
  229.      file { '/some/other/file': ensure  => present } 
  230. ============================================================================================================ 
  231.  
  232. if  $server == 'mongrel'    { 
  233.         include mongrel 
  234. } elsif $server ==  'nginx' { 
  235.         include nginx 
  236. }   else    { 
  237.         include thin 
  238.  
  239.  
  240. if  $ram    >   1024    { 
  241.         $maxclient = 500 
  242.  
  243. ============================================================================================================= 
  244.  
  245. if ( $processor_count > 2 ) and ((  $ram >= 16 * $gigabyte  ) or ( $disksize > 1000 )) { 
  246.     include for_big_irons 
  247. } else { 
  248.    include  for_small_box 
  249. ================================================================================================================ 
  250. unless  $memorysize >   1024    { 
  251. $maxclient  =   500 
  252. ================================================================================================================ 
  253.  
  254. 虚拟资源 
  255. @user   {   'luke': ensure  =>  present } 
  256. User    <|  title   ==  luke    |>  
  257. realize User['luke'] 
  258. #将虚拟资源实例化 
  259.  
  260.  
  261. class   ssh { 
  262. @@sshkey    {   $hostname:  type    =>  dsa,    key =>  $sshdsakey  } 
  263. Sshkey  <<| |>> 
  264.  
  265.  
  266. 正则表达式 
  267.  
  268.  
  269. 安装module 
  270. puppet  module  install puppetlabs-apache   --version   0.0.2 
  271. puppet  module  list 
  272. puppet  module  search  apache 
  273. puppet  module  uninstall   puppetlabs-apache 
  274. puppet  module  upgrade puppetlabs-apache   --version   0.0.3 
  275. https://forge.puppetlabs.com 
  276.  
  277.  
  278. 当一个文件改变的时候如何运行一个命令 
  279. =============================================================================================== 
  280. file    {   "/etc/bind":    source  =>  "/dist/apps/bind"   } 
  281. exec    {   "/usr/bin/ndc   reload": 
  282.     subscribe   =>  File["/etc/bind"], 
  283.     refreshonly =>  true 
  284. ============================================================================================== 
  285.  
  286.  
  287. 如何确保创建一个用户之前另一个组已经存在 
  288. group   {   "fearme": 
  289.         ensure  =>  present, 
  290.         gid =>  1000 
  291. user    {   "tim": 
  292.         ensure  =>  present, 
  293.         gid =>  "fearme", 
  294.         groups  =>  ["adm", "staff",    "root"], 
  295.         membership  =>  minimum, 
  296.         shell   =>  "/bin/bash", 
  297.         require =>  Group["fearme"] 
  298.  
  299.  
  300.  
  301. class   base_class  { 
  302. define  testvar_file($myvar="bob")  { 
  303. file    {   $name: 
  304. content =>  template("john.erb"), 
  305. testvar_file    {   "/tmp/testvar": } 
  306. class   child_class inherits    base_class  { 
  307. Base_class::Testvar_file["/tmp/testvar"]    {   myvar   =>  fred    } 
  308.  
  309.  
  310. gem install rack 
  311. gem install passenger 
  312. passenger-install-apache2-module 

 

本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/1134203,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值