puppet杂记(2)

puppet

puppet清单

1、package

1.1 配置
[root@master1 manifests]# vim test6.pp

package{'zsh':
        ensure  =>  latest,
}

package{'jdk':
        ensure  =>  installed,
        source  =>  '/usr/local/src/dk-8u121-linux-x64.rpm',
        provider => rpm,
}
1.2 测试
[root@master1 manifests]# puppet apply -v test6.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 1.01 seconds
Info: Applying configuration version '1513208601'
Notice: /Stage[main]/Main/Package[jdk]/ensure: created
Notice: Finished catalog run in 38.15 seconds
[root@master1 manifests]# ls /usr/java/
default  jdk1.8.0_121  latest
[root@master1 manifests]# 

2、service

2.1 配置
[root@master1 manifests]# vim test7.pp

package{'nginx':
        ensure  =>  latest,
}

service{'nginx':
        ensure  =>  running,
        enable  =>  true,
        hasrestart  =>  true,
        hasstatus   =>  true,
        restart =>  'systemctl reload nginx.service',
}
2.2 测试
[root@master1 manifests]# puppet apply -v test7.pp
Notice: Compiled catalog for master1.master1.com in environment production in 1.60 seconds
Info: Applying configuration version '1513211771'
Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Main/Service[nginx]: Unscheduling refresh on Service[nginx]
Notice: Finished catalog run in 9.56 seconds

[root@master1 manifests]# rpm -q nginx
nginx-1.12.2-1.el7.x86_64

特殊属性:Metaparameters

1、before

1.1 示例,被依赖资源要先执行,before
[root@master1 manifests]# vim test8.pp

group {'linux':
        gid     => 3000,
        ensure  => present,
        before  => User['suse'],
}

user{'suse':
        uid     => 3000,
        gid     => 3000,
        shell   => '/bin/bash',
        home    => '/home/suse',
        ensure  => present,
}    
1.2 测试正常
[root@master1 manifests]# puppet apply -v test8.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.66 seconds
Info: Applying configuration version '1513213099'
Notice: /Stage[main]/Main/Group[linux]/ensure: created
Notice: /Stage[main]/Main/User[suse]/ensure: created
Notice: Finished catalog run in 0.21 seconds
[root@master1 manifests]# 

2、require

2.1 执行条件,需要某种功能事先存在
group {'linux':
        gid     => 3000,
        ensure  => present,
}

user{'suse':
        uid     => 3000,
        gid     => 3000,
        shell   => '/bin/bash',
        home    => '/home/suse',
        ensure  => present,
        require => Group['linux'],
}       

3、链式关系,哪个先,哪个后

group {'linux':
        gid     => 3000,
        ensure  => present,
} ->

user{'suse':
        uid     => 3000,
        gid     => 3000,
        shell   => '/bin/bash',
        home    => '/home/suse',
        ensure  => present,
}

4、

4.1 创建目录,并保存配置文件:
[root@master1 ~]# mkdir -pv /root/modules/nginx/files
mkdir: created directory ‘/root/modules’
mkdir: created directory ‘/root/modules/nginx’
mkdir: created directory ‘/root/modules/nginx/files’
[root@master1 ~]# cp /etc/nginx/nginx.conf /root/modules/nginx/files/

将新配置文件,监听端口改成8080
4.2 puppet配置
package{'nginx':
        ensure  =>  latest,
}

file{'/etc/nginx/nginx.conf':
        ensure  => file,
        source  => '/root/modules/nginx/files/nginx.conf',
        require => Package['nginx'],
}

service{'nginx':
        ensure  =>  running,
        enable  =>  true,
        hasrestart  =>  true,
        hasstatus   =>  true,
        restart =>  'systemctl reload nginx.service',
        require =>  [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
}
4.3 测试
[root@master1 manifests]# puppet apply -v test9.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 1.83 seconds
Info: Applying configuration version '1513220525'
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: /Stage[main]/Main/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 93bc8e01bfd45e7e18b23acc178ae25b
Notice: /Stage[main]/Main/File[/etc/nginx/nginx.conf]/content: content changed '{md5}93bc8e01bfd45e7e18b23acc178ae25b' to '{md5}300d8a8ed16977a5353604e3bbcb8e3b'
Notice: Finished catalog run in 4.52 seconds
4.4 定义重启关系:
[root@master1 manifests]# vim test9.pp 

package{'nginx':
        ensure  =>  latest,
}

file{'/etc/nginx/nginx.conf':
        ensure  => file,
        source  => '/root/modules/nginx/files/nginx.conf',
        require => Package['nginx'],
        notify  => Service['nginx'],    ###
}

service{'nginx':
        ensure  =>  running,
        enable  =>  true,
        hasrestart  =>  true,
        hasstatus   =>  true,
#       restart =>  'systemctl reload nginx.service',
        require =>  [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
}

puppet变量

1、示例

$webserver=nginx
package{$webserver:
        ensure  =>  latest,
}

运行正常
[root@master1 manifests]# puppet apply -v test10.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 1.52 seconds
Info: Applying configuration version '1513262351'
Notice: Finished catalog run in 3.78 seconds
1.1 删除nginx后也能正常安装
[root@master1 manifests]# puppet apply -v test10.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 1.75 seconds
Info: Applying configuration version '1513262455'
Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: FileBucket got a duplicate file {md5}93bc8e01bfd45e7e18b23acc178ae25b
Info: /Stage[main]/Main/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 93bc8e01bfd45e7e18b23acc178ae25b
Notice: /Stage[main]/Main/File[/etc/nginx/nginx.conf]/content: content changed '{md5}93bc8e01bfd45e7e18b23acc178ae25b' to '{md5}9b2cf71797e8ad03e9812f314ad6b198'
Info: /Stage[main]/Main/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Main/Service[nginx]: Unscheduling refresh on Service[nginx]
1.2
[root@master1 manifests]# rpm -q facter
facter-2.4.1-1.el7.x86_64
[root@master1 manifests]# rpm -qi facter
Name        : facter
Epoch       : 1
Version     : 2.4.1
Release     : 1.el7
Architecture: x86_64
Install Date: Tue 17 Jan 2017 06:24:18 PM CST

查看当前主机前puppet请求提交的factor变量
[root@master1 manifests]# facter -p

2、条件语句

2.1 示例
[root@master1 manifests]# vim test11.pp

if $processorcount>1 {
        notice("SMP Host.")
} else {
    notice("Poor Guy.")
}

[root@master1 manifests]# puppet apply -v test11.pp 
Notice: Scope(Class[main]): Poor Guy.
Notice: Compiled catalog for master1.master1.com in environment production in 0.06 seconds
Info: Applying configuration version '1513265603'
Notice: Finished catalog run in 0.04 seconds
2.2 其他示例,表达式
[root@master1 manifests]# vim test12.pp

if $operatingsystem =~ /^(?i-mx:(centos|redhat|fedora|ubuntu))/ {
        notice("Welcome to $1 distribution linux.")`
}

[root@master1 manifests]# puppet apply -v test12.pp 
Notice: Scope(Class[main]): Welcome to CentOS distribution linux.
Notice: Compiled catalog for master1.master1.com in environment production in 0.06 seconds
Info: Applying configuration version '1513270250'
Notice: Finished catalog run in 0.06 seconds

转载于:https://blog.51cto.com/zhongle21/2089226

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值