vivion27 发表于 2018-8-1 12:37:15

puppet常用资源类型与使用方法

通用
  1、puppet describe -l   查看puppet支持的资源类型
  puppet describetypename查看某个资源类型的Parameters,Providers--详细
  puppet describe-stypename-m查看某个资源的参数等--简单
  2、puppet apply[-d|--debug] [-v|--verbose] [-e|--execute] [--noop] <file>
  puppet help apply查看帮助
  puppet apply-v   查看详情
  -d   调试模式
  -l 保存在指定文件中
  --noop干跑,不执行
一、group:
  1、Manage groups
  属性:
  name:组名;
  gid:GID;
  system:是否为系统组,true OR false;
  ensure:目标状态,present/absent;
  members:成员用户;
  2、例子:创建组
  (1) vim group.pp
  group{'mygrp':
  ensure => present
  }
  (2)执行
  puppet apply -v--noopgroup.pp 先看看是否有错
  puppet apply -v group.pp真正执行
二、user
  1、Manage users.
  属性:
  name:用户名;
  uid: UID;
  gid:基本组ID;
  groups:附加组,不能包含基本组;
  comment:注释;
  expiry:过期时间 ;
  home:家目录;
  shell:默认shell类型;
  system:是否为系统用户 ;
  ensure:present/absent;
  password:加密后的密码串;
  2、例子:创建用户
  (1)用户名为user2,不写name。默认是跟title一直,都是user2
  vim user.pp
  user{'user2':
  ensure => present,
  uid => 1021,
  groups => 'redhat',
  }
  (2)执行
  puppet apply-v--noop user.pp
  puppet apply-v   user.pp
三、package
  1、Manage packages.
  属性:
  ensure:installed, present, latest, absent, any version string (implies present)
  name:包名;
  source:程序包来源,仅对不会自动下载相关程序包的provider有用,例如rpm或dpkg;
  provider:指明安装方式;
  2、例子:安装包
  安装memcached包
  package{'memcached':
  ensure => installed,
  }
四、service
  1、Manage running services.
  属性:
  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:
  path:The search path for finding init scripts.Multiple values should be separated by colons or provided as an array. 脚本的搜索路径,默认为/etc/init.d/;
  hasrestart:true |false   false :init 脚本中的restart命令将不能用,使用stop ,start,默认false
  hasstatus:true|false   stasus 命令是否使用
  start:手动定义启动命令;
  stop:
  status:
  restart:Specify a restart command manually.If left unspecified, the service will be stopped and then started. 通常用于定义reload操作;
  2、例子:安装包并启动服务
  package{'memcached':
  ensure => installed,
  }
  service{'memcached':
  ensure => running,
  enable => false,
  }
五、file
  1、Manages files, including their content, ownership, and permissions. 管理文件,包括它们的内容、所有权和权限。
  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:文件路径;
  source:源文件;
  content:文件内容;
  target:符号链接的目标文件;
  owner:属主
  group:属组
  mode:权限;
  atime/ctime/mtime:时间戳;
  2、例子
  (1)例子1:将'/root/manifects/files/redis.conf 的文件复制到/tmp/redis.conf
  file{'/tmp/redis.conf':
  ensure => file,
  source => '/root/manifects/files/redis.conf',
  }
  (2)例子2:基于内容复制
  将hello
  hi内容给/tmp/test.txt
  file{'/tmp/test.txt':
  ensure => file,
  content => &quot;hello\nhi\n&quot;,
  }
六、exec
  1、Executes external commands. Any command in an exec resource must be able to run multiple times without causing harm --- that is, it must be idempotent.
  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:仅接收到订阅的资源的通知时方才运行;
  2、例子

  (1)指定包为redis
  (2)指定复制的文件,并修改复制后文件的所属人owner,所属组root,权限的0640
  (3)除非pidof redis-server不成功才执行上面的命令,重启服务(以守护进程方式启动)
  (4)->   代表依赖关系,~> 代表触发
七、cron
  1、Installs and manages cron jobs.Every cron resource created by Puppet requires a command and at least one periodic attribute (hour, minute, month, monthday, weekday, or special).
  command:要执行的任务;
  ensure:present/absent;
  hour:
  minute:
  monthday:
  month:
  weekday:
  user:以哪个用户的身份运行命令
  target:添加为哪个用户的任务
  name:cron job的名称;
  2、例子:每3分钟同步一次服务器时间
  cron{'timesync':
  command => '/usr/sbin/ntpdate 172.16.0.1 &> /dev/null',
  ensure=> present,
  minute=> '*/3',
  user    => 'root',
  }
八、notify
  1、Sends an arbitrary message to the agent run-time log.向代理运行时日志发送任意消息。
  属性:
  message:信息内容
  name:信息名称;
  核心类型:
  group: 组
  user:用户
  packge:程序包
  service:服务
  file:文件
  exec:执行自定义命令,要求幂等
  cron:周期性任务计划
  notify:通知
  官方地址:
  https://docs.puppet.com/puppet/5.2/cheatsheet_core_types.html#notify/
  2、例子:
  当redis的配置文件改变了,会重启服务
页: [1]
查看完整版本: puppet常用资源类型与使用方法