《SaltStack技术入门与实践》—— Job管理

Job管理

本章节参考《SaltStack技术入门与实践》,感谢该书作者: 刘继伟、沈灿、赵舜东

  在SaltStack里面执行任何一个操作都会在Master上产生一个jid号。Minion端会在cache目录下的proc目录创建一个jid为名称的文件,这个文件里面的内容就是此次操作的记录,当操作处理完成后该文件会自动删除。而Master端会记录每次操作的详细信息,这个记录都是存到在Master端cache目录下jobs下。下面通过实例来了解日常job管理。

目前SaltStack提供两种方式来管理job:

  • salt-run
  • module

1、通过salt-run来管理job

首先我们来查看下salt-run对job管理的一些用法:

[root@SaltMaster1(10.182.88.136)]$~:>salt-run -d |grep jobs
jobs.active:    #查看当前运行的jobs
    Return a report on all actively running jobs from a job id centric
        salt-run jobs.active
jobs.exit_success:
        salt-run jobs.exit_success 20160520145827701627
jobs.last_run:
    List all detectable jobs and associated functions
        salt-run jobs.last_run
        salt-run jobs.last_run target=nodename
        salt-run jobs.last_run function='cmd.run'
        salt-run jobs.last_run metadata="{'foo': 'bar'}"
jobs.list_job:        #指定jid查看jobs详细信息
        salt-run jobs.list_job 20130916125524463507
        salt-run jobs.list_job 20130916125524463507 --out=pprint
jobs.list_jobs:       #查看所有jobs信息
    List all detectable jobs and associated functions
        If more than one of the below options are used, only jobs which match
            salt-run jobs.list_jobs search_metadata='{"foo": "bar", "baz": "qux"}'
        Can be passed as a string or a list. Returns jobs which match the
            salt-run jobs.list_jobs search_function='test.*'
            salt-run jobs.list_jobs search_function='["test.*", "pkg.install"]'
                salt-run jobs.list_jobs search_function='test.*,pkg.install'
        Can be passed as a string or a list. Returns jobs which match the
            salt-run jobs.list_jobs search_target='*.mydomain.tld'
            salt-run jobs.list_jobs search_target='["db*", "myminion"]'
                salt-run jobs.list_jobs search_target='db*,myminion'
        module is not installed, this argument will be ignored). Returns jobs
        module is not installed, this argument will be ignored). Returns jobs
        salt-run jobs.list_jobs
        salt-run jobs.list_jobs search_function='test.*' search_target='localhost' search_metadata='{"bar": "foo"}'
        salt-run jobs.list_jobs start_time='2015, Mar 16 19:00' end_time='2015, Mar 18 22:00'
jobs.list_jobs_filter:
    List all detectable jobs and associated functions
        salt-run jobs.list_jobs_filter 50
        salt-run jobs.list_jobs_filter 100 filter_find_job=False
jobs.lookup_jid:    #指定jid查询jobs结果
        salt-run jobs.lookup_jid 20130916125524463507
        salt-run jobs.lookup_jid 20130916125524463507 --out=highstate
jobs.print_job:     #指定jid查询jobs详细信息
        salt-run jobs.print_job 20130916125524463507
    It can also be used to schedule jobs directly on the master, for example:

  这里会显示salt-run关于job操作的所有命令。关于每个参数的解释大家可以通过salt-run -d jobs来查看:

[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' cmd.run 'sleep 100;whoami'
^C
Exiting gracefully on Ctrl-c
This job's jid is: 20180624194553900549
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:

salt-run jobs.lookup_jid 20180624194553900549

  因为等待时间过长我这里直接用Ctrl + C了,这个时候会提示此次任务的jid号。我们可以随时通过salt-run job管理来查看这个job信息。查询这个job的运行结果:

[root@SaltMaster1(10.182.88.136)]$~:>salt-run jobs.list_job 20180624194553900549
Arguments:
    - sleep 100;whoami
Function:
    cmd.run
Minions:
    - 10.182.76.78
Result:
    ----------
StartTime:
    2018, Jun 24 19:45:53.900549
Target:
    10.182.76.78
Target-type:
    glob
User:
    root
jid:
    20180624194553900549

2、通过SaltStack Module来管理job

  在上面已经介绍了通过salt-run可以对日常job进行管理,为什么还要介绍利用Module来管理job呢。因为salt-run对job管理功能比较局限,上面我们也看到了目前salt-run还不支持kill某个job。现在我们就开始介绍使用SaltStack自带的Module来管理job。首先查看相关Module的用法:

[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' sys.doc saltutil |grep job
saltutil.clear_job_cache:      
    Forcibly removes job cache folders and files on a minion.
        salt '*' saltutil.clear_job_cache hours=12
saltutil.find_cached_job:  #查询job cache信息
    Return the data for a specific cached job id. Note this only works if
    cache_jobs has previously been set to True on the minion.
        salt '*' saltutil.find_cached_job <job id>
saltutil.find_job:      #查看job信息
    Return the data for a specific job id that is currently running.
        The job id to search for and return data.
        salt '*' saltutil.find_job <job id>
    Note that the find_job function only returns job information when the job is still running. If
    the job is currently running, the output looks something like this:
        # salt my-minion saltutil.find_job 20160503150049487736
    If the job has already completed, the job cannot be found and therefore the function returns
        # salt my-minion saltutil.find_job 20160503150049487736
saltutil.kill_all_jobs:   
    Sends a kill signal (SIGKILL 9) to all currently running jobs
        salt '*' saltutil.kill_all_jobs
saltutil.kill_job:      #杀掉job(发送SIGTERM 9信号方式)
    Sends a kill signal (SIGKILL 9) to the named salt job's process
        salt '*' saltutil.kill_job <job id>
        salt master_minion saltutil.runner jobs.list_jobs
saltutil.signal_job:     #发送指定信号
    Sends a signal to the named salt job's process
        salt '*' saltutil.signal_job <job id> 15
saltutil.term_all_jobs:
    Sends a termination signal (SIGTERM 15) to all currently running jobs
        salt '*' saltutil.term_all_jobs
saltutil.term_job:      #删掉job(发送SIGTERM 15信号方式)
    Sends a termination signal (SIGTERM 15) to the named salt job's process
        salt '*' saltutil.term_job <job id>

我们接着按照上面的例子来测试:

[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' cmd.run 'sleep 100;whoami'
^C
Exiting gracefully on Ctrl-c
This job's jid is: 20180624195818566512
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:

salt-run jobs.lookup_jid 20180624195818566512

使用Module来管理job:

[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' saltutil.find_job 20180624195818566512
10.182.76.78:
    ----------
    arg:
        - sleep 100;whoami
    fun:
        cmd.run
    jid:
        20180624195818566512
    pid:
        81684
    ret:
    tgt:
        10.182.76.78
    tgt_type:
        glob
    user:
        root

我们还可以直接kill这个job:

[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' saltutil.kill_job 20180624195818566512
10.182.76.78:
[root@SaltMaster1(10.182.88.136)]$~:>salt '10.182.76.78' saltutil.find_job 20180624195818566512
10.182.76.78:
    ----------
原文地址:https://www.cnblogs.com/zuoyang/p/9221534.html