puppet杂记(1)

puppet agent

puppet agent安装应用

1、安装agent组件

[root@master1 puppet]# yum install -y puppet-3.8.4-1.el7.noarch.rpm facter-2.4.1-1.el7.x86_64.rpm

2、查看帮助

puppet help
2.1 列出所支持的资源类型
[root@master1 ~]# puppet describe --list
2.2 描述packert资源如何使用
[root@master1 ~]# puppet describe package

资源

清单

1、创建清单文件夹

[root@master1 ~]# mkdir manifests
[root@master1 ~]# cd manifests/

2、定义组资源

[root@master1 manifests]# vim test1.pp 

group {'distro':
        gid     => 2000,
        ensure  => present,
}

user{'centos':
        uid     => 2000,
        gid     => 2000,
        shell   => '/bin/bash', 
        home    => '/home/centos',      
        ensure  => present,
} 
2.1 应用清单
[root@master1 manifests]# puppet apply -v test1.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.57 seconds
Info: Applying configuration version '1484681988'
Notice: /Stage[main]/Main/Group[distro]/ensure: created
Notice: /Stage[main]/Main/User[centos]/ensure: created
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.41 seconds
2.2 查看报告状态结果
[root@master1 ~]# cat /var/lib/puppet/state/state.yaml
2.3 查看结果
[root@master1 ~]# tail /etc/group | grep 2000
distro:x:2000:
[root@master1 ~]# tail /etc/passwd | grep 2000
centos:x:2000:2000::/home/centos:/bin/bash
[root@master1 ~]# 

3、file清单【创建文件清单】

3.1 配置文件
[root@master1 manifests]# vim test2.pp

file {'/tmp/mydir':
        ensure => directory,
}
        ensurce => directory,
file {'/tmp/mydir':
        ensurce => directory,
}

file {'/tmp/puppet.file':
        content => 'puppet testing\nsecond line.',
        ensure  => file,
        owner   => 'centos',
        group   => 'distro',
        mode    => '0400',
}

file {'/tmp/fstab.puppet':
        source => '/etc/fstab',
        ensure => file,
}

file {'/tmp/puppet.link':
        ensure => link,
        target => '/tmp/puppet.file',
}
3.2 验证
[root@master1 tmp]# ll /tmp/
total 8
-rw-r--r-- 1 root   root   1065 Jan 18 13:12 fstab.puppet
drwxr-xr-x 2 root   root      6 Jan 18 13:12 mydir
-r-------- 1 centos distro   28 Jan 18 13:12 puppet.file
lrwxrwxrwx 1 root   root     16 Jan 18 13:12 puppet.link -> /tmp/puppet.file
[root@master1 tmp]# 

4、exec清单【】

4.1 配置
exec {'/usr/sbin/modprobe ext4':
        user    => root,
        group   => root,
        refresh => '/usr/sbin/modprobe -r ext4 && /usr/sbin/modprobe ext4',
        timeout => 5,
        tries   => 2,
}
4.2 测试
[root@master1 ~]# puppet apply -v test3.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version '1484724300'
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
Notice: Finished catalog run in 0.30 seconds
4.3 有损坏的方式,创建目录
exec {'/usr/bin/echo hello > /tmp/hello.txt':
        user    => root,
        group   => root,
}
4.4 防止损坏方式
exec {'/usr/bin/echo mageedu > /tmp/hello.txt':
        user    => root,
        group   => root,
        creates => '/tmp/hello.txt',
}

测试:文件存在没有运行
[root@master1 ~]# puppet apply -v test3.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version '1484730095'
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
Notice: Finished catalog run in 0.07 seconds

测试:删除掉文件后,运行了:
[root@master1 ~]# rm /tmp/hello.txt 
rm: remove regular file ‘/tmp/hello.txt’? y
[root@master1 ~]# puppet apply -v test3.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version '1484730156'
Notice: /Stage[main]/Main/Exec[/usr/bin/echo mageedu > /tmp/hello.txt]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
4.5 另一种方式,防止损坏
如果test命令执行失败,才会执行上面的命令:

exec {'/usr/bin/echo mageedu > /tmp/hello2.txt':
        user    => root,
        group   => root,
        unless  => '/usr/bin/test -e /tmp/hello2.txt',
}

测试:第一次文件不存在,执行:
[root@master1 ~]# puppet apply -v test3.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version '1484730808'
Notice: /Stage[main]/Main/Exec[/usr/bin/echo mageedu > /tmp/hello2.txt]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully

测试:第二次,文件存在不执行:
[root@master1 ~]# puppet apply -v test3.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.14 seconds
Info: Applying configuration version '1484730849'
Notice: /Stage[main]/Main/Exec[/usr/sbin/modprobe ext4]/returns: executed successfully
Notice: Finished catalog run in 0.20 seconds

5、notify清单

5.1 配置
[root@master1 ~]# vim test4.pp

notify{"hello there.":}

测试:
[root@master1 ~]# puppet apply -v test4.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.06 seconds
Info: Applying configuration version '1484734291'
Notice: hello there.
Notice: /Stage[main]/Main/Notify[hello there.]/message: defined 'message' as 'hello there.'

6、cron清单

6.1 示例
[root@master1 ~]# vim test5.pp

cron{"sync time":
        command => '/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null',
        minute  => '*/10',

}

测试:
[root@master1 ~]# puppet apply -v test5.pp 
Notice: Compiled catalog for master1.master1.com in environment production in 0.16 seconds
Info: Applying configuration version '1484738321'
Notice: /Stage[main]/Main/Cron[sync time]/ensure: created

*/10 * * * * /usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null

删除计划任务:
cron{"sync time":
        command => '/usr/sbin/ntpdate s2c.time.edu.cn &> /dev/null',
        minute  => '*/10',
        ensure  => absent,   ##
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值