天天看點

自動化運維之Puppet

系統自動安裝工具 PXE 一個PXE隻能安裝同一種系統

灰階釋出 舊版本不删除 出現問題復原

puppet 有agent ansible 無agent,ansible沒有angent 基于ssh傳輸 不安全,ansible定義角色 和 puppet定義子產品是一樣的 相當于函數 實作代碼重用,puppet的agent 接收server端指令 并完成相應的管理功能 并把執行結果傳回給server

子產品是由資源清單組成

#列出Puppet支援的所有資源類型
[root@bogon ~]#  puppet describe -l
           
#檢視某資源較長的描述 
[root@bogon ~]#  puppet describe package
           
# <user group>
[root@bogon mainfests]# mkdir mainfests
[root@bogon mainfests]# vim test1.pp 
    group {'disto':
            gid     => ,
            ensure  => present
    }

    user {'cent':
            uid     => ,
            gid     => ,
            shell   => '/bin/bash',
            home    => '/home/cent',
            ensure  => 'present'
    }
[root@bogon mainfests]# puppet apply -v test1.pp 

[root@bogon mainfests]# tail /etc/group
[root@bogon mainfests]# tail /etc/passwd
           
# <file>
[root@bogon mainfests]# vim test2.pp
    file {'/tmp/mydir':
            ensure  => directory

    }
    file {'/tmp/puppet.file':
            content => 'haha,woshipuppet',
            ensure  => file,
            owner   => 'cent',
            group   => 'disto',
            mode    => '0400'
    }
    file {'/tmp/puppet.link':
            target  => '/tmp/puppet.file',
            ensure  => link
    }
    file {'/tmp/fstab.puppet':
            source  => '/etc/fstab',
            ensure  => file
[root@bogon mainfests]# puppet apply -v test2.pp
[root@bogon mainfests]# ll /tmp
           
# <exec>
[root@bogon mainfests]# vim test3.pp
    exec {'/usr/sbin/modprobe ext4':
            user    => root,
            group   => root,
            refresh => '/usr/sbin/modprobe -r ext4 && /usr/sbin/modprobe ext4',
            timeout => ,
            tries   => 
    }
    exec {'/bin/echo "xuuxe" > /tmp/xuxue.txt':
            user    => root,
            group   => root,
            creates => '/tmp/xuxue.txt' #該路徑檔案不存在時 則執行echo指令
    }
    exec {'/bin/echo chun > /tmp/chun.txt':
            user    => root,
            group   => root,
            unless  => '/usr/bin/test -e /tmp/heh.txt'#該指令執行失敗 則執行echo指令

    }
[root@bogon mainfests]# puppet apply -v test3.pp
           
# <notify>
[root@bogon mainfests]# vim test4.pp
    notify{'hi notify':
    }
[root@bogon mainfests]# puppet apply -v test4.pp
           
# <cron>
[root@bogon mainfests]# vim test5.pp
    cron {'sync time':
            command => '/usr/sbin/ntpdate 192.168.1.17 &> /dev/null',
            minute  => '*/2'
    }
[root@bogon mainfests]# puppet apply -v test5.pp
[root@bogon mainfests]# crontab -l
           
# <package>
[root@bogon mainfests]# vim test6.pp
package {'zsh':
        ensure  => latest
}
package {'jdk':
        ensure  => installed,
        source  => '/usr/local/src/jdk-8u25-linux-x64.rpm',
        provider        => rpm

}
[root@bogon mainfests]# puppet apply -v test6.pp
           
# <service>
[root@bogon mainfests]# vim test7.pp
    package {'nginx':
            ensure  => latest
    }
    service {'nginx':
            ensure  => running,
            enable  => true,
            hasrestart      => true,
            hasstatus       => true,
            restart => 'systemtctl reload nginx.service'
    }
[root@bogon mainfests]# puppet apply -v test7.pp
           
#依賴關系
    資源引用
        Type['title']
    依賴關系
        被依賴資源中使用:before
        依賴其它資源的資源:require
        ->:鍊式依賴
           

[root@bogon mainfests]# vim test8.pp
    group {'linux':
            gid     => ,
            ensure  => present,
            #before         => User['suse']
    }#->

    user {'suse':
            uid     => ,
            gid     => ,
            shell   => '/bin/bash',
            home    => '/home/suse',
            ensure  => 'present',
            #require        => Group['linux']
    }
    #  依賴關系  before  => User['suse']等同于->等同于require => Group['linux']
[root@bogon mainfests]# puppet apply -v test8.pp
           
#通知關系
    資源引用
        Type['title']
    依賴關系
        被依賴資源中使用:notify
        監聽其它資源的資源:subscribe
        ~>:鍊式通知
           

[root@bogon mainfests]# setenforce 0
[root@bogon mainfests]# mkdir /root/modules/nginx/files -pv
[root@bogon mainfests]# cp /etc/nginx/nginx.conf /root/modules/nginx/files
[root@bogon mainfests]# vim /root/modules/nginx/files/nginx.conf 
        #修改端39行口号測試   listen       808 default_server;

[root@bogon mainfests]# 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,
            require => [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
    }

[root@bogon mainfests]# puppet apply -v test9.pp
[root@bogon mainfests]# ss -tnl
           
#條件判斷
[root@bogon mainfests]# vim test11.pp
    if $processorcount> {
            notice ('SMP HOST')
    }else {
            notice ('poor guy')
    }
[root@bogon mainfests]# puppet apply -v test11.pp
           
[root@bogon mainfests]# vim test12.pp
    if $operatingsystem =~/^(?i-mx:(centos|redhat|fedora|ubuntu))/ {
            notice("welcome $1 hahhah")
    }
[root@bogon mainfests]# puppet apply -v test12.pp
           

繼續閱讀