环境

三台虚拟机:

192.168.60.128(policy hub)  server

192.168.60.129(client)      agent1

192.168.60.130(client)      agent2

 

配置完成了,现在准备测试,有以下需求

1.server、agent1建立test用户,agent2删除test用户

2.配置ssh让test用户不能登录

3.同步脚本目录

4.确保脚本具有执行权限

5.安装lamp环境

6.确保apache、mysql服务运行

 

下面例子是经过测试的,可能需要做一些修改才能使用,这些例子是在server上编写的。

server、agent1建立test用户,agent2删除test用户

 
  
  1. bundle agent user_add_del  # 文件名是user_add_del.cf
  2.     vars: 
  3.         "users" slist => {"test"}; 
  4.     classes: 
  5.         "add_$(users)" not => userexists("$(users)"); # 用户不存在则建立相应的class
  6.     commands: 
  7. # 如果相应classes存在则增加test用户,这里不能使用echo命令
  8. # cfengine不能管理简单的命令,最好使用脚本
  9. # ifvarclass允许在class expression使用变量
  10. # "cfengine::"这种形式不能使用变量
  11.         "/bin/bash $(sys.workdir)/inputs/shell/useradd.sh $(users)" 
  12.         ifvarclass => "add_$(users)&(192_168_60_128|192_168_60_129)"; 
  13. # "192_168_60_130"是hard class,有cfengine自动建立
  14.         "/usr/sbin/userdel -rf $(users)" 
  15.         ifvarclass => "!add_$(users)&192_168_60_130"; 

配置ssh让test用户不能登录

 
  
  1. bundle agent config_ssh  # 文件名config_ssh.cf
  2.     vars: 
  3.         # ssh config file 
  4.         "ssh" string => "/etc/ssh/sshd_config"; 
  5.      
  6.         # ssh config to set  
  7.         "sshd[DenyUsers]" string => "test"; 
  8.  
  9.     methods:  # methods调用agent bundle,前面说过
  10.         "sshd"  usebundle => edit_sshd;  
  11.  
  12. bundle agent edit_sshd 
  13.     files: 
  14.         "$(config_ssh.ssh)" 
  15.         handle => "edit_sshd", 
  16.         comment => "Set desired sshd_config parameters", 
  17.         edit_line => set_config_values("config_ssh.sshd"), 
  18.         classes => if_repaired("restart_sshd"); # promises处于repaired状态则建立restart_sshd
  19.      
  20.     commands: 
  21.         restart_sshd.!no_restarts:: 
  22.         "/etc/init.d/sshd reload" 
  23.         handle => "sshd_restart", 
  24.         comment => "Restart sshd if the configuration file was modified"; 
  25.  
  26. # Sets the RHS of variables in the file of the form # LHS RHS
  27. bundle edit_line set_config_values(v) 
  28.     vars: 
  29.         "index" slist => getindices("$(v)"); 
  30.         "cindex[$(index)]" string => canonify("$(index)"); #canonify除去class非法字符
  31.      
  32.     replace_patterns: 
  33.         "^\s*($(index)\s+(?!$($(v)[$(index)])).*|# ?$(index)\s+.*)$" 
  34.         replace_with => value("$(index) $($(v)[$(index)])"), 
  35.         classes => always("replace_attempted_$(cindex[$(index)])"); 
  36.  
  37.     insert_lines: 
  38.         "$(index) $($(v)[$(index)])" 
  39.         ifvarclass => "replace_attempted_$(cindex[$(index)])"; 
  40. body classes always(x)
  41. {
  42. promise_kept => { "$(x)" };
  43. promise_repaired => { "$(x)" };
  44. repair_failed => { "$(x)" };
  45. repair_denied => { "$(x)" };
  46. repair_timeout => { "$(x)" };
  47. }

同步脚本目录并确保脚本具有执行权限

 
  
  1. bundle agent copy_shell  # 文件名copy_shell.cf
  2.     vars: 
  3. # 这里同步的是masterfiles/shell目录,权限默认开通
  4. # 如果同步/var/ftp/pub/shell,需要在"bundle server access_rules"开通权限
  5.         "shell_location" string => "/var/cfengine/masterfiles/shell"; 
  6.         "inputs_location" string => "/var/cfengine/inputs"; 
  7.     files: 
  8.         "$(inputs_location)/shell" 
  9.         comment => "copy shell scripts from policy hub", 
  10.         handle  => "sync_shell_scripts", 
  11.         copy_from => secure_cp("$(shell_location)","$(sys.policy_hub)"), 
  12.         depth_search => shell_recurse("inf"), # 无限递归 
  13.         file_select  => shell_files; # 选择那种类型的文件同步,有点类似正则表达式
  14.  
  15.         "$(inputs_location)/shell" 
  16.         comment => "ensure shell scripts have execute permissions", 
  17.         handle  => "update_shell_files", 
  18.         perms   => m("755"), 
  19.         depth_search => shell_recurse("inf"); 
  20.  
  21. body file_select shell_files 
  22.     leaf_name => { ".*.sh" };  # 选择shell脚本同步
  23.     file_result => "leaf_name"; # 以leaf_name为标准同步
  24.  
  25. body depth_search shell_recurse(d) 
  26.     include_basedir => "true"; 
  27.     depth => "$(d)"; 

安装lamp环境

 
  
  1. # 安装依赖包
  2. bundle agent lamp_pkgs_installed # 文件名是lamp.cf 
  3.     vars: 
  4.         "desired_pkgs" slist => { "gcc", "gcc-c++",  
  5.                                   "libtermcap-devel",  
  6.                                   "libxml2-devel", 
  7.                                 };   
  8.      
  9.     packages: 
  10.         "$(desired_pkgs)" 
  11.             package_policy => "add", #没有则安装 
  12.             package_method => yum, 
  13.             comment => "install desired packages"; 
  14.  
  15. # 删除已经安装的rpm packages
  16. bundle agent rpm_pkgs_remove 
  17.     vars: 
  18.         "rpm_lamp" slist => { "httpd", "httpd-devel", 
  19.                               "mysql", "mysql-server", "mysql-devel", 
  20.                               "php", "php-devel", "php-mysql", "php-common", 
  21.                             };   
  22.      
  23.     packages: 
  24.         "$(rpm_lamp)" 
  25.             package_policy => "delete", 
  26.             package_method => yum, 
  27.             comment => "remove rpm version packages"; 

 
  
  1. bundle agent mysql_install  # 文件名是mysql_install.cf
  2.     vars: 
  3.         "install_dir" string => "/usr/local/lamp/mysql"; 
  4.         "config_file" string => "/etc/my.cnf"; 
  5.         "shell_dir"   string => "/mnt/public/shell"; #安装脚本的位置 
  6.     classes: 
  7.         "mysql_install_dir_exists" 
  8.             expression => fileexists("$(install_dir)"); 
  9.         "mysql_config_file_exists" 
  10.             expression => fileexists("$(config_file)"); 
  11.     reports: 
  12.         !mysql_install_dir_exists:: 
  13.             "$(install_dir) is not present."; 
  14.     commands: 
  15. # 安装目录不存在则调用脚本安装,这里一定要注意时间的安排
  16. # 因为脚本运行的时间比较长,必须在下次评估前安装完
  17. # 脚本运行期间不要运行cf-agent,这样会出错的
  18.         !mysql_install_dir_exists.Hr16.Min00_05::  
  19.             "$(shell_dir)/install_mysql.sh"; 
  20. # 配置文件不存在则拷贝
  21.         mysql_install_dir_exists&!mysql_config_file_exists:: 
  22.             "/bin/cp $(install_dir)/share/mysql/my-large.cf $(config_file)"; 
  23.             "/bin/chown root:root $(config_file)"; 
  24.             "/bin/chmod 644 $(config_file)"; 

 
  
  1. # 结构和mysql_install.cf类似
  2. bundle agent apache_install # apache_install.cf
  3.     vars: 
  4.         "install_dir" string => "/usr/local/lamp/apache"; 
  5.         "config_file" string => "/etc/httpd.conf"; 
  6.         "shell_dir"   string => "/mnt/public/shell"; 
  7.     classes: 
  8.         "apache_install_dir_exists" 
  9.             expression => fileexists("$(install_dir)"); 
  10.         "apache_config_file_exists" 
  11.             expression => fileexists("$(config_file)"); 
  12.     reports: 
  13.         !apache_install_dir_exists:: 
  14.             "$(install_dir) is not present."; 
  15.     commands: 
  16.         !apache_install_dir_exists.Hr17.Min00_05:: 
  17.             "$(shell_dir)/install_apache.sh"; 
  18.         apache_install_dir_exists&!apache_config_file_exists:: 
  19.             "/bin/ln -s $(install_dir)/conf/httpd.conf /etc/httpd.conf"; 

 
  
  1. # 结构和mysql_install.cf类似
  2. bundle agent php_install  # php_install.cf
  3.     vars: 
  4.         "install_dir" string => "/usr/local/lamp/php"; 
  5.         "mysql_dir" string => "/usr/local/lamp/mysql"; 
  6.         "apache_dir" string => "/usr/local/lamp/apache"; 
  7.         "config_file" string => "/etc/php.ini"; 
  8.         "shell_dir"   string => "/mnt/public/shell"; 
  9.     classes: 
  10.         "php_install_dir_exists" 
  11.             expression => fileexists("$(install_dir)"); 
  12.         "mysql_install_dir_exists" 
  13.             expression => fileexists("$(mysql_dir)"); 
  14.         "apache_install_dir_exists" 
  15.             expression => fileexists("$(apache_dir)"); 
  16.         "php_config_file_exists" 
  17.             expression => fileexists("$(config_file)"); 
  18.     reports: 
  19.         !php_install_dir_exists:: 
  20.             "$(install_dir) is not present."; 
  21.     commands: 
  22. # php必须等mysql、apache安装完后才能安装,因为有依赖性
  23.         !php_install_dir_exists.mysql_install_dir_exists.apache_install_dir_exists.Hr18.Min00_05:: 
  24.             "$(shell_dir)/install_php.sh"; 
  25.         php_install_dir_exists&!php_config_file_exists:: 
  26.             "/bin/ln -s $(install_dir)/etc/php.ini /etc/php.ini"; 

确保apache、mysql服务运行

 
  
  1. bundle agent start_process 
  2.     vars: 
  3.         "processes" slist => {"httpd", "mysqld"}; 
  4.     classes: 
  5.         "$(processes)_exist" expression => fileexists("/etc/init.d/$(processes)"); 
  6.     processes: 
  7.         "$(processes)" restart_class => canonify("start_$(processes)"); 
  8.     commands: 
  9.         "/etc/init.d/$(processes) start" 
  10.         ifvarclass => "start_$(processes)&$(processes)_exist"; 
  11.     reports: 
  12.         cfengine:: 
  13.             "--> apache is not running on $(sys.fqhost)" 
  14.             ifvarclass => "!httpd_exist"; 
  15.             "--> mysql is not running on $(sys.fqhost)" 
  16.             ifvarclass => "!mysqld_exist"; 

 把上面的文件都放在masterfiles目录并在masterfiles里修改promises.cf

 
  
  1. bundlesequence => {  
  2.                     "main", "user_add_del", "config_ssh", 
  3. "copy_shell",
  4.                     "lamp_pkgs_installed", "mysql_install", "apache_install", 
  5.                     "php_install",   
  6.                     
  7.                     };   
  8.  
  9.  inputs => {  
  10.             "cfengine_stdlib.cf",  
  11.            "user_add_del.cf", 
  12. "config_ssh.cf",
  13. "copy_shell.cf",
  14.             "lamp/lamp.cf", 
  15.             "lamp/mysql_install.cf", 
  16.             "lamp/apache_install.cf", 
  17.             "lamp/php_install.cf", 
  18.            };   

lamp安装脚本在附件中,使用Notepad++打开,把脚本复制到/mnt/public/shell中