puppet系列之sudo模塊

 需求:每臺服務器上都創建dev組,專門給開發人員,給特定的開發人員建賬號,並加入dev組;

運維人員也將創建各自的用戶,並加入wheel組,通過sudo來對組裏成員做權限設置。

 

架構如下:

 

manfests裏面的文件內容如下:

  1. 1,init.pp 
  2. class sudo { 
  3.   case $::osfamily { 
  4.     'RedHat': { 
  5.       include "sudo::conf" 
  6.       import 'sudoers.pp' 
  7.     } 
  8.     default: { 
  9.       fail("$::osfamily not yet supported by the 'sudo' module!") 
  10.     } 
  11.   } 
  12.  
  13. 2,conf.pp 
  14. class sudo::conf { 
  15.   package { "sudo": 
  16.     ensure => present, 
  17.   } 
  18.  
  19.   # Source the sudoers file from the Puppet Master 
  20.   file { "/etc/sudoers": 
  21.     ensure  => present, 
  22.     owner   => 'root', 
  23.     group   => 'root', 
  24.     mode    => 0440, 
  25.     source  => "puppet:///modules/sudo/sudoers", 
  26.     require => Package["sudo"], 
  27.   } 
  28.  
  29.   # Source a new 'su' file for PAM (caution: this may be platform-specific) 
  30.   file { "/etc/pam.d/su": 
  31.     ensure => present, 
  32.     owner  => 'root', 
  33.     group  => 'root', 
  34.     mode   => 0644, 
  35.     source => "puppet:///modules/sudo/pam_su_el6" 
  36.   } 
  37.  
  38.   # Clear any config in sudoers.d 
  39.   file { "/etc/sudoers.d": 
  40.     ensure  => directory, 
  41.     owner   => 'root', 
  42.     group   => 'root', 
  43.     mode    => '0750', 
  44.     recurse => true, 
  45.     purge   => true, 
  46.     require => Package["sudo"], 
  47.   } 
  48.  
  49. 3,sudoers.pp 
  50. define sudo::sudoers (  
  51.   $sudo_sudoers , 
  52.   $sudo_sysadmins , 
  53.  # $admins   = split($sudo_sysadmins, ','), 
  54.  # $sudoers  = split($sudo_sudoers, ','), 
  55.   user { [ $sudo_sysadmins ]: 
  56.     ensure  => present, 
  57.     groups  => ['wheel'], 
  58.     require => Group['wheel'], 
  59.   } 
  60.  
  61.   user { [ $sudo_sudoers ]: 
  62.     ensure  => present, 
  63.     groups  => ['dev'], 
  64.     require => Group['dev'], 
  65.   } 
  66.  
  67.   group { "wheel": 
  68.     ensure => present, 
  69.   } 
  70.  
  71.   group { "dev": 
  72.     ensure => present, 
  73.   } 

files目錄文件內容如下:

  1. 1,pam_su_el6 
  2. #%PAM-1.0 
  3. # This file is managed by Puppet. 
  4. auth        sufficient  pam_rootok.so 
  5. # Uncomment the following line to implicitly trust users in the "wheel" group. 
  6. #auth       sufficient  pam_wheel.so trust use_uid 
  7. # Uncomment the following line to require a user to be in the "wheel" group. 
  8. auth        required    pam_wheel.so use_uid 
  9. auth        include     system-auth 
  10. account     sufficient  pam_succeed_if.so uid = 0 use_uid quiet 
  11. account     include     system-auth 
  12. password    include     system-auth 
  13. session     include     system-auth 
  14. session     optional    pam_xauth.so 
  15.  
  16. 2,sudoers 
  17.  
  18. ## Sudoers allows particular users to run various commands as 
  19. ## the root user, without needing the root password. 
  20. ## 
  21. ## Examples are provided at the bottom of the file for collections 
  22. ## of related commands, which can then be delegated out to particular 
  23. ## users or groups. 
  24. ##  
  25. ## This file must be edited with the 'visudo' command. 
  26.  
  27. ## Host Aliases 
  28. ## Groups of machines. You may prefer to use hostnames (perhap using  
  29. ## wildcards for entire domains) or IP addresses instead. 
  30. # Host_Alias     FILESERVERS = fs1, fs2 
  31. # Host_Alias     MAILSERVERS = smtp, smtp2 
  32.  
  33. ## User Aliases 
  34. ## These aren't often necessary, as you can use regular groups 
  35. ## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname  
  36. ## rather than USERALIAS 
  37. # User_Alias ADMINS = jsmith, mikem 
  38.  
  39.  
  40. ## Command Aliases 
  41. ## These are groups of related commands... 
  42.  
  43. ## Networking 
  44. #Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool 
  45.  
  46. ## Installation and management of software 
  47. Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum 
  48.  
  49. ## Services 
  50. #Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig 
  51.  
  52. ## Updating the locate database 
  53. #Cmnd_Alias LOCATE = /usr/bin/updatedb 
  54.  
  55. ## Storage 
  56. Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount 
  57.  
  58. ## Delegating permissions 
  59. #Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp  
  60.  
  61. ## Processes 
  62. #Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall 
  63.  
  64. ## Drivers 
  65. #Cmnd_Alias DRIVERS = /sbin/modprobe 
  66.  
  67. ## Denied commands for dev 
  68. Cmnd_Alias DEV_DENIED = /bin/su, /usr/sbin/visudo, /bin/chgrp, /usr/sbin/adduser, /usr/sbin/useradd, /usr/sbin/usermod, /usr/sbin/userdel, /usr/sbin/passwd, /sbin/shutdown, /sbin/init, /sbin/reboot, /usr/bin/reboot 
  69.  
  70. # Defaults specification 
  71.  
  72. # Disable "ssh hostname sudo <cmd>", because it will show the password in clear.  
  73. #         You have to run "ssh -t hostname sudo <cmd>". 
  74. Defaults    requiretty 
  75.  
  76. # Refuse to run if unable to disable echo on the tty. This setting should also be 
  77. # changed in order to be able to use sudo without a tty. See requiretty above. 
  78. Defaults   !visiblepw 
  79.  
  80. Defaults    env_reset 
  81. Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \ 
  82.                         LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \ 
  83.                         LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \ 
  84.                         LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \ 
  85.                         LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \ 
  86.                         _XKB_CHARSET XAUTHORITY" 
  87.  
  88. ## Next comes the main part: which users can run what software on  
  89. ## which machines (the sudoers file can be shared between multiple 
  90. ## systems). 
  91. ## Syntax: 
  92. ## 
  93. ##  user    MACHINE=COMMANDS 
  94. ## 
  95. ## The COMMANDS section may have other options added to it. 
  96. ## 
  97. ## Allow root to run any commands anywhere  
  98. root    ALL=(ALL)   ALL 
  99.  
  100. ## Allows members of the 'sys' group to run networking, software,  
  101. ## service management apps and more. 
  102. # %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS 
  103.  
  104. ## Allows people in group wheel to run all commands 
  105. %wheel  ALL=(ALL)   ALL 
  106.  
  107. ## Same thing without a password 
  108. # %wheel    ALL=(ALL)   NOPASSWD: ALL 
  109.  
  110. ## Allows members of the users group to mount and unmount the  
  111. ## cdrom as root 
  112. # %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom 
  113.  
  114. ## Allows members of the users group to shutdown this system 
  115. # %users  localhost=/sbin/shutdown -h now 
  116.  
  117. ## Denied DEV_DENIED and STORAGE commands for dev group 
  118. %dev     ALL=(ALL)       ALL, !DEV_DENIED, !STORAGE 

使用方法如下:

  1. include sudo 
  2. sudo::sudoers { "example": 
  3.   sudo_sysadmins => ['test-wheel-1','test-wheel-2'], 
  4.   sudo_sudoers   => ['test-sudo-1'], 

github地址如下:https://github.com/vTNT/puppet-sudo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章