puppet的主要资源解释及示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nange_nice/article/details/78630811

主要用到的资源类型:

(1) cron
(2) exec
(3) file
(4) user
(5) group
(6) packet
(7) service
(8) notify

模块选项及示例介绍

  • cron
选项:
command:要执行的任务;
ensure:present/absent;
hour:
minute:
monthday:
month:
weekday:
user:以哪个用户的身份运行命令
target:添加为哪个用户的任务,默认为user所定义的用户,两个只能定义一个,否则会出问题
name:cron job的名称

示例:

# 同步时间
cron {'synctime':
     command =>  '/usr/sbin/ntpdate 172.18.0.1 &> /dev/null',
     ensure  =>  present,
     minute  =>  '*/5',
     user    =>  root
}
  • exec
选项:
**command** (*namevar*):要运行的命令;
cwd:The directory from which to run the command.
**creates**:文件路径,仅此路径表示的文件不存在时,command方才执行;
user/group:运行命令的用户身份;
path:The search path used for command execution. Commands must be fully qualified if no path is specified.
onlyif:此属性指定一个命令,此命令正常(退出码为0)运行时,当前command才会运行;
unless:此属性指定一个命令,此命令非正常(退出码为非0)运行时,当前command才会运行;
refresh:重新执行当前command的替代命令;
refreshonly:仅接收到订阅的资源的通知时方才运行;

示例:

# 创建目录
exec {'createdir':
     command =>  'mkdir /tmp/hi',
     path    =>  '/sbin:bin',
     creates =>  '/tmp/hi'  #在目录不存在时执行
}
# 添加用户
exec {'adduser':
     command     =>  'useradd testuser',
     path    =>  '/sbin',
     unless  =>  'id tetsuser' # 当用户不存在时执行,也可用onlyif,当命令执行成功时执行
}
# 当文件修改是做备份处理
file {'redis.conf':
     ensure  =>  file,
     path    =>  '/etc/redis.conf',
     source  =>  '/root/redis.conf',
     owner   =>  redis,
}

exec {'backup':
     command =>  'cp /etc/redis.conf /backup/redis.conf.$(date +%F)',
     path    =>  '/bin:/sbin:/usr/bin',
     refreshonly =>  true,  # 仅当触发订阅时执行
     subscribe   =>  File['redis.conf']  # 订阅file资源,当file修改时执行相应动作
}
  • file
选项:
ensure:Whether the file should exist, and if so what kind of file it should be. Possible values are present, absent, file, directory, and link.
file:类型为普通文件,其内容由content属性生成或复制由source属性指向的文件路径来创建;
link:类型为符号链接文件,必须由target属性指明其链接的目标文件;
directory:类型为目录,可通过source指向的路径复制生成,recurse属性指明是否递归复制(只要想把目录下内容全部复制到指定目录下,不管是文件还是文件和目录);
path(*namevar*):The path to the file to manage,文件路径,可以由title代替;
source:源文件;
content:文件内容;
target:符号链接的目标文件;
owner:属主
group:属组
mode:权限;
atime/ctime/mtime:时间戳;

注意:如果要把文件复制到目录(目录不存在),执行完命令,会把文件内容拷贝到以目录名为文件名的文件里,不会创建目录,所以拷贝文件到目录时要确保目录存在

示例:

file{'test.txt':
    path    => '/tmp/test.txt',
    ensure  => file,
    source  => '/etc/fstab',
}

file{'test.symlink':
    path    => '/tmp/test.symlink',
    ensure  => link,
    target  => '/tmp/test.txt',
    require => File['test.txt'],
}

file{'test.dir':
    path    => '/tmp/test.dir',
    ensure  => directory,
    source  => '/etc/yum.repos.d/',
    recurse => true,
}
  • user
选项:
name:用户名;
uid: UID;
gid:基本组ID;
groups:附加组,不能包含基本组;
comment:注释;
expiry:过期时间 ;
home:自定义家目录路径,但目录得自己建,和managehome一起使用比较好;
shell:默认shell类型;
system:是否为系统用户 ;
ensure:present/absent;
password:加密后的密码串;
managehome: 是否在创建删除用户的时候创建删除家目录,当 ensure => present 创建家目录, 当 ensure => absent 删除家目录. 默认值是 false.有效值可以是 true, false, yes, no.

示例:

写法1:
user {'puser':
     name    =>  puser,
     ensure  =>  present,
     comment =>  "puppet user",
     groups  =>  mygrp,
     require    =>  Group['mygrp']
}

group {'mygrp':
     ensure  =>  present
}

写法2:
user {'puser':
     name    =>  puser,
     ensure  =>  present,
     comment =>  "puppet user",
     groups  =>  mygrp,
} ->

group {'mygrp':
     ensure  =>  present
}

写法3:
user {'puser':
     name    =>  puser,
     ensure  =>  present,
     comment =>  "puppet user",
     groups  =>  mygrp,
}

group {'mygrp':
     ensure  =>  present
}
User['puser'] -> Group['mygrp']
关系元参数:before/require
A before B: B依赖于A,定义在A资源中;
    {
        ...
        before  => Type['B'],
        ...
    }   
B require A: B依赖于A,定义在B资源中;
    {
        ...
        require => Type['A'],
        ...
    }
  • group
选项:
name:组名;可以不写,但title就表示name
gid:GID;
system:是否为系统组,true or false;
ensure:目标状态,present/absent;
members:成员用户;

示例:

group {'mygrp':
     ensure  =>  present
}
  • package
选项:
ensure:installed, present, latest, absent, any version string (implies present)
name:包名;
source:程序包来源,仅对不会自动下载相关程序包的provider有用,例如rpm或dpkg

示例:

package {'redis':
    ensure    =>    latest
}
  • service
选项:
ensure:Whether a service should be running. Valid values are stopped (also called false), running (also called true).
enable:Whether a service should be enabled to start at boot. Valid values are true, false, manual.
name:也可写在title
path:The search path for finding init scripts. Multiple values should be separated by colons or provided as an array. 脚本的搜索路径,默认为/etc/init.d/;
hasrestart:是否支持restart功能,默认false,当想通过通知来触发restart时,可以设定为true,来执行restart命令,如果不设定为true,将调用脚本的stop和start命令来重启
hasstatus:默认true
start:手动定义启动命令;
stop:同上
status:同上
restart:Specify a restart command manually. If left unspecified, the service will be stopped and then started. 通常用于定义reload操作;

示例:

service {'redis':
     ensure  =>  true,
     enable  =>  true,
     hasrestart  =>  true
}
  • notify(Sends an arbitrary message to the agent run-time log)
选项:
message:信息内容
name:信息名称

示例:

notify {'hello':
     message =>  'hello world'
}

另一个示例:

package {'redis':
     name    =>  redis,
     ensure  =>  latest,
}

file {'redis.conf':
     ensure  =>  file,
     path    =>  '/etc/redis.conf',
     source  =>  '/root/redis.conf',
     owner   =>  redis,
     require =>  Package['redis'],
     notify  =>  Service['redis']
}

service {'redis':
     ensure  =>  true,
     enable  =>  true,
     hasrestart  =>  true,
}

猜你喜欢

转载自blog.csdn.net/nange_nice/article/details/78630811