1. 程式人生 > >puppet雜記(1)

puppet雜記(1)

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)