码迷,mamicode.com
首页 > 其他好文 > 详细

puppet杂记(1)

时间:2018-03-20 23:59:59      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:puppet linux 基础 杂记

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,   ##
}

puppet杂记(1)

标签:puppet linux 基础 杂记

原文地址:http://blog.51cto.com/zhongle21/2089225

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!