puppet基本操作

yum install puppet

如果没有yum源 可以安装epel源

yum install epel-release

puppet apply:

Applies a standalone Puppet manifest to the local system.
puppet apply  [-d|--debug] [-v|--verbose] [-e|--execute] [--noop] 

puppet资源:

资源抽象的纬度(RAL如何抽象资源的?):
类型:具有类似属性的组件,例如package、service、file 、user、group;

puppet describe:
Prints help about Puppet resource types, providers, and metaparameters.
puppet describe [-h|--help] [-s|--short] [-p|--providers] [-l|--list] [-m|--meta] [type]
-l:列出所有资源类型;
-s:显示指定类型的简要帮助信息;
-m:显示指定类型的元参数,一般与-s一同使用;

定义资源的语法:

        type {'title':
                    attribute1  => value1,
                    atrribute2  => value2,
                }
                
    注意:type必须使用小写字符;title是一个字符串,在同一类型中必须惟一;

资源类型:

group:
    Manage groups.
    属性:
        name:组名;
        gid:GID;
        system:是否为系统组,true OR false;
        ensure:目标状态,present/absent;
        members:成员用户;
                    
user:
    Manage users.       
    属性:
        name:用户名;
        uid: UID;
        gid:基本组ID;
        groups:附加组,不能包含基本组;
        comment:注释; 
        expiry:过期时间 ;
        home:家目录; 
        shell:默认shell类型;
        system:是否为系统用户 ;
        ensure:present/absent;
        password:加密后的密码串; 
关系元参数:before/require
        A before B: B依赖于A,定义在A资源中;
                      {
                        ...
                        before  => Type['B'],
                        ...
        B require A: B依赖于A,定义在B资源中;
                    {
                        ...
                        require => Type['A'],
                        ...
                    }
                    
  package:
    Manage packages.    
    属性:
        ensure:installed, present, latest, absent
        name:包名;
        source:程序包来源,仅对不会自动下载相关程序包的provider有用,例如rpm或dpkg;
service:
    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:
        hasstatus:
        start:手动定义启动命令;
        stop:
        status:
        restart:Specify a *restart* command manually.  If left unspecified, the service will be stopped and then started. 通常用于定义reload操作;   
file:
    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:时间戳;
资源引用:
    Type['title']
      类型的首字母必须大写;

例子1 group 创建redis组

vim group.pp
  写入内容
  group{'redis':
    name => 'redis',
    ensure => present,
    gid => 2000,
    system=>true,
}
测试命令 puppet apply  --debug  --verbose  --noop  group.pp
真实运行命令  puppet apply  --debug  --verbose   group.pp

例子2 user 创建redis用户

user{'redis':
    name=>'redis',
    system=>true,
    ensure=>present,
    uid=>2000,
    gid=>2000,
}

例子3 pacakge 安装redis

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

例子4 service 启动redis

service{'redis':
    ensure  => running,
    enable  => true,
    restart => 'systemctl restart redis.service',
}

例子5 file 复制配置文件

file{'test.txt':
    path    => '/etc/redis.conf',
    ensure  => file,
    source  => '/home/puppetfile/redis.conf',
    owner=>'redis',
    group=>'redis'
}

例子6 联合依赖 安装-》修改配置文件-》启动 redis

package{'redis':
    name=>'redis',
    ensure=>installed,
    allow_virtual=>false,
}

file{'redis.conf':
    path    => '/etc/redis.conf',
    ensure  => file,
    source  => '/home/puppetfile/redis.conf',
    owner=>'redis',
    group=>'redis'
}
service{'redis':
    ensure  => running,
    enable  => true,
    restart => 'systemctl restart redis.service',
}
Package['redis']->File['redis.conf']->Service['redis']

你可能感兴趣的:(puppet基本操作)