puppet(1.7-2.1)

puppet配置模块(一)node

模块是puppet的最大单元,模块里面有类,类下面有资源。同步文件、远程执行命令、cron等叫作资源,都是经过模块来实现的。下面咱们来定义一个模块:web

在服务端上作以下操做:
mkdir /etc/puppet/modules/testm  //模块名字就是 testm
cd   /etc/puppet/modules/testm
apache

mkdir {files,manifests,templates}   //一个模块下须要有这三个目录,files存一些文件(能够为空),manifests存配置文件,templates存模板(能够留空)
touch manifests/init.pp  //这个是必须的
vi  manifests/init.pp   //内容以下
class testm{
file {"/tmp/2.txt":
owner => "root",
group => "root",
mode => 0400,
source => "puppet://$puppetserver/modules/testm/1.txt"
}
}
说明:类名字也叫作testm, 类下面定义了一个资源file,文件名字叫作/tmp/2.txt ,owner,group,mode定义文件的属主、数组以及权限,source定义这个文件从哪里获取。 $puppetserver一会也要定义一下,这里指的是puppet server服务器上/etc/puppet/modules/testm/files/1.txt
vim




puppet配置模块(二)windows

下面要继续定义一个很关键的配置文件:
vim  /etc/puppet/manifests/site.pp   //内容以下
$puppetserver = 'web9.xuan.com'
node 'web10'{
    include test
}
说明:$puppetserver 定义服务端的主机名,node后面为客户端的主机名,这里面定义该客户端要加载的模块
配置完成后,在客户端执行命令:
puppet agent --test  --server=web9.xuan.com   //若是客户端上启动了puppet服务,不用执行这命令,它也会自动同步的
数组




puppet文件或目录资源(1)服务器

上面的模块其实只是同步了一个文件而已,那么要想同步一个目录如何作?咱们能够经过实现同步一个目录来作一个包发布系统。 好比在一台机器上编译安装好了apache,那么就能够经过这样的模块把这个apache目录整个分发到其余机器上。less

这是在服务端上的配置
模块配置文件以下:
ide

vi   /etc/puppet/modules/testm/manifests/init.ppui

class apache{
file {"/usr/local/apache2":                              (对方机器所在的目录)
owner => "root",
group => "root",
source => "puppet://$puppetserver/modules/testm/apache2",               (这个是从哪里下载)
recurse => true,                                     (针对目录的)
purge => true                                         (支持删除操做)
}
}

cd   /etc/puppet/modules/testm/files

mkdir  apache2   ;   cd   apache2

mkdir   {conf,bin,logs}

touch  conf/a.conf ; touch  bin/xuan  ; touchu  logs/qq.log

echo  "asdfghjkl"  > conf/a.conf

vim  /etc/puppet/manifests/site.pp

$puppetserver = 'web9.xuan.com'
node 'web10'{
    include test

    include apache
}

能够在客户端上ls /usr/local/下会有一个Apache2

服务端

vim  /etc/puppet/modules/testm/files/apache2/bin/xuan

qwertyuiop

tail  /var/log/messages


其中recurse=>true 这个参数很关键,它表示递归的意思,没有这个不能同步目录。purge参数能够保证当服务端删除某个文件,客户端能够跟着删除。





puppet远程执行命令

远程执行命令:

vi   /etc/puppet/modules/testm/manifests/init.pp

exec {"123":
unless => "test -f /tmp/xuan.txt",
path => ["/bin", "/sbin", "/usr/bin", "/usr/sbin"],
command => "/bin/touch /tmp/xuan.txt"
}

说明:unless后面的命令做为一个条件,当条件成立时,不会执行下面的命令,若是想要条件成立时,执行下面的命令,用 onlyif。要注意的是,咱们必定要给执行的这条命令加个条件,使用unless就能够,必须知足这个条件才能执行命令,不然这个命令会一直执行,不太稳当。









puppet配置cron

服务端上配置

cron资源:
cron {"xuan":
command => "/sbin/ntpdate time.windows.com",
user => "root",
minute => "*/10",

monthday  => "10-15",

# ensure => "absent"  //当增长了这行配置,则会把该cron删除掉
}

在客户端上crontab -l你会能够看到更改
说明:分时日月周分别对应puppet里面的minute,hour,monthday,month,weekday

本站公众号
   欢迎关注本站公众号,获取更多信息