美文网首页
puppet自动化之资源定义大收尾

puppet自动化之资源定义大收尾

作者: 尘曦的雨 | 来源:发表于2017-09-24 20:06 被阅读24次

    exec命令管控资源

    
    
    [root@centos7 ~]# cat exec.pp   执行密等的命令
    exec{'mktemp':
        command => 'mktemp -d /tmp/dir.XXX',
        path => '/bin:/sbin:/usr/bin:/usr/sbin',
    }
    [root@centos7 ~]# cat exec.pp 
    exec{'mktemp':
        command => 'mktemp -d /tmp/dir.XXX',
        path => '/bin:/sbin:/usr/bin:/usr/sbin',
    }
    exec{'mkdir':
        command => 'mkdir /tmp/mydir',
        path => '/bin:/sbin:/usr/bin:/usr/sbin',
    #   create => '/tmp/mydir',
        unless => 'test -d /tmp/mydir',  条件是执行命令
    }
    [root@centos7 ~]# puppet apply -v exec.pp 第一次执行两个资源都执行
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.04 s
    econdsInfo: Applying configuration version '1506281057'
    Notice: /Stage[main]/Main/Exec[mktemp]/returns: executed successfully两个都执行
    Notice: /Stage[main]/Main/Exec[mkdir]/returns: executed successfully
    Notice: Finished catalog run in 0.08 seconds
    [root@centos7 ~]# puppet apply -v exec.pp  第二次执行
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.04 seconds
    Info: Applying configuration version '1506281512'
    Notice: /Stage[main]/Main/Exec[mktemp]/returns: executed successfully
    Notice: Finished catalog run in 0.08 seconds
    
    

    cron 计划任务资源定义

    Installs and manages cron jobs.  Every cron resource created by Puppet requires a command and at least one periodic attribute (hour, minute, month, monthday, weekday, or special).
                
                command:要执行的任务;
                ensure:present/absent;
                hour:
                minute:
                monthday:
                month:
                weekday:
                user:以哪个用户的身份运行命令
                target:添加为哪个用户的任务
                name:cron job的名称;
                
                示例:
    [root@centos7 ~]# vim  ch.pp 
                    cron{'timesync':
                        command => '/usr/sbin/ntpdate 172.16.0.1 &> /dev/null',
                        ensure  => present,
                        minute  => '*/3',
                        user    => 'root',
                    }           
    [root@centos7 ~]# puppet apply -v --noop ch.pp 
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.07 seconds
    Info: Applying configuration version '1506282122'
    Notice: /Stage[main]/Main/Cron[timesync]/ensure: current_value absent, should be present (noop)
    Notice: Class[Main]: Would have triggered 'refresh' from 1 events
    Notice: Stage[main]: Would have triggered 'refresh' from 1 events
    Notice: Finished catalog run in 0.17 seconds
    [root@centos7 ~]# puppet apply -v ch.pp 
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.05 seconds
    Info: Applying configuration version '1506282266'
    Notice: /Stage[main]/Main/Cron[timesync]/ensure: created
    Notice: Finished catalog run in 0.08 seconds
    
    [root@centos7 ~]# crontab -l
    # HEADER: This file was autogenerated at 2017-09-25 03:44:27 +0800 by puppet.
    # HEADER: While it can still be managed manually, it is definitely not recommended
    .# HEADER: Note particularly that the comments starting with 'Puppet Name' should
    # HEADER: not be deleted, as doing so could cause duplicate cron jobs.
    # Puppet Name: timesync
    */3 * * * * /usr/sbin/ntpdate 172.16.0.1 &> /dev/null
    [root@centos7 ~]# cat ch.pp 
    cron{'timesync':
                        command => '/usr/sbin/ntpdate 172.16.0.1 &> /dev/null',
                        ensure  => absent,  删除计划任务的定义
                        minute  => '*/3',
                        user    => 'root',
                    }
    
    [root@centos7 ~]# puppet apply -v ch.pp 
    Notice: Compiled catalog for centos7.3-chenxi3 in environment production in 0.05 s
    econdsInfo: Applying configuration version '1506253646'
    Notice: /Stage[main]/Main/Cron[timesync]/ensure: removed
    Notice: Finished catalog run in 0.08 seconds
    
    
    
    

    相关文章

      网友评论

          本文标题:puppet自动化之资源定义大收尾

          本文链接:https://www.haomeiwen.com/subject/crnkextx.html