JOB MANAGEMENT
New in version 0.9.7.
Since Salt executes jobs running on many systems, Salt needs to be able to manage jobs running on many systems.
由于Salt在许多系统上运行执行jobs,所以Salt需要能管理在多个系统上运行的jobs。
the minion proc system
Salt Minions maintain a proc directory in the Salt cachedir
. The proc directory maintains files named after the executed job ID. These files contain the information about the current running jobs on the minion and allow for jobs to be looked up. This is located in the proc directory under the cachedir, with a default configuration it is under /var/cache/salt/proc
.
Salt Minions 在Salt cachedir 维护一个proc目录。proc目录维护以执行的作业id命名的文件。这些文件包含关于当前运行的任务的信息,并允许jobs被查出。这位于Cache目录下的proc目录下,默认配置为/var/cache /salt/proc。
functions in the saltutil module
Salt 0.9.7 introduced a few new functions to the saltutil
module for managing jobs. These functions are:
Salt 0.97为管理工作引入了一些新功能。这些功能是:
running
Returns the data of all running jobs that are found in the proc directory.running
返回在PRC目录中找到的所有正在运行的作业的数据。find_job
Returns specific data about a certain job based on job id. find_job 返回基于作业ID的特定作业的特定数据。signal_job
Allows for a given jid to be sent a signal.signal_job允许给定的JID发送信号。
term_job
Sends a termination signal (SIGTERM, 15) to the process controlling the specified job. term_job 将终止信号(SIGTYNT,15)发送到控制指定作业的过程。kill_job
Sends a kill signal (SIGKILL, 9) to the process controlling the specified job. kill_job将杀死信号(SIGKID,9)发送到控制指定作业的进程。
These functions make up the core of the back end used to manage jobs at the minion level.
这些功能构成了后端的核心,用于管理minion级别的作业。
the jobs runner
A convenience runner front end and reporting system has been added as well. The jobs runner contains functions to make viewing data easier and cleaner.
还增加了一个方便的runner前端和报告系统。jobs runner包含使查看数据更容易和更干净的功能。
The jobs runner contains a number of functions...
jobs runner 包含许多函数。。。
active
The active function runs saltutil.running on all minions and formats the return data about all running jobs in a much more usable and compact format. The active function will also compare jobs that have returned and jobs that are still running, making it easier to see what systems have completed a job and what systems are still being waited on.
active函数运行saltutil.running 在所有minions上,并以更加实用和紧凑的格式格式化所有运行的作业的返回数据。active函数还将比较返回的作业和正在运行的作业,从而更容易了解哪些系统已经完成了作业,哪些系统仍在等待。
# salt-run jobs.active
LOOKUP_JID
When jobs are executed the return data is sent back to the master and cached. By default it is cached for 24 hours, but this can be configured via the keep_jobs
option in the master configuration. Using the lookup_jid runner will display the same return data that the initial job invocation with the salt command would display.
当jobs被执行时,返回数据被发送回主机并被缓存。默认情况下,它被缓存了24个小时,但是这可以通过主配置中的 keep_jobs
选项来配置。使用 lookup_jid转接器将显示与salt命令将显示的初始作业调用相同的返回数据。
# salt-run jobs.lookup_jid <job id number>
LIST_JOBS
Before finding a historic job, it may be required to find the job id. list_jobs
will parse the cached execution data and display all of the job data for jobs that have already, or partially returned.
在找到历史job之前,可能需要找到作业ID。list_jobs 将解析缓存的执行数据,并显示已经或部分返回的作业的所有作业数据。
# salt-run jobs.list_jobs
scheduling jobs 调度作业
Scheduling can be enabled by multiple methods: 可以通过多种方法启用调度:
schedule
option in either the master or minion config files. These require the master or minion application to be restarted in order for the schedule to be implemented.在master或minion配置配置文件中调度选项。这些要求master或minion应用程序重新启动,以便实现计划。- Minion pillar data. Schedule is implemented by refreshing the minion's pillar data, for example by using
saltutil.refresh_pillar
.Minion pillar 数据。时间表是通过刷新minions的pillar数据来实现的,例如通过使用saltutil.refresh_pillar。 - The
schedule state
orschedule module
注意: The scheduler executes different functions on the master and minions. When running on the master the functions reference runner functions, when running on the minion the functions specify execution functions. 调度器对master和minion执行不同的功能。当在master上运行函数引用转接器函数时,当在minion上运行时,函数指定执行函数。
A scheduled run has no output on the minion unless the config is set to info level or higher. Refer to minion-logging-settings
.
除非配置被设置为信息级别或更高,否则调度运行在minion上没有输出。参考 minion-logging-settings
。
States are executed on the minion, as all states are. You can pass positional arguments and provide a YAML dict of named arguments.
States是在minion上执行的,就像所有的states一样。您可以传递位置参数,并提供命名参数的YAML dict。
schedule: job1: function: state.sls seconds: 3600 args: - httpd kwargs: test: True
This will schedule the command: state.sls httpd test=True
every 3600 seconds (every hour).
这将调度命令:state.sls httpd test=True 每3600秒(每小时)。
schedule: job1: function: state.sls seconds: 3600 args: - httpd kwargs: test: True splay: 15
This will schedule the command: state.sls httpd test=True
every 3600 seconds (every hour) splaying the time between 0 and 15 seconds.
这将调度命令:stest.sls htpd test=True 每3600秒(每小时),消耗时间在0到15秒之间。
schedule: job1: function: state.sls seconds: 3600 args: - httpd kwargs: test: True splay: start: 10 end: 15
This will schedule the command: state.sls httpd test=True
every 3600 seconds (every hour) splaying the time between 10 and 15 seconds.
这将调度命令:stest.sls htpd test=True 每3600秒(每小时),消耗时间在10到15秒之间。
SCHEDULE BY DATE AND TIME
New in version 2014.7.0.
Frequency of jobs can also be specified using date strings supported by the Python dateutil
library. This requires the Python dateutil
library to be installed.
jobs的频率也可以使用Python dateutil库支持的日期字符串来指定。这需要安装Python dateutil
库。
schedule: job1: function: state.sls args: - httpd kwargs: test: True when: 5:00pm
This will schedule the command: state.sls httpd test=True
at 5:00 PM minion localtime.
schedule: job1: function: state.sls args: - httpd kwargs: test: True when: - Monday 5:00pm - Tuesday 3:00pm - Wednesday 5:00pm - Thursday 3:00pm - Friday 5:00pm
This will schedule the command: state.sls httpd test=True
at 5:00 PM on Monday, Wednesday and Friday, and 3:00 PM on Tuesday and Thursday.
schedule: job1: function: state.sls args: - httpd kwargs: test: True when: - 'tea time'
whens: tea time: 1:40pm deployment time: Friday 5:00pm
The Salt scheduler also allows custom phrases to be used for the when parameter. These whens can be stored as either pillar values or grain values.
Salt调度器也允许自定义短语用于参数。这些可以存储为pillar值或者grain值。
schedule: job1: function: state.sls seconds: 3600 args: - httpd kwargs: test: True range: start: 8:00am end: 5:00pm
This will schedule the command: state.sls httpd test=True
every 3600 seconds (every hour) between the hours of 8:00 AM and 5:00 PM. The range parameter must be a dictionary with the date strings using the dateutil
format.
这将调度命令:stest.sls htpd test=True ,每3600秒(每小时)之间的时间上午8:00和下午5:00。范围参数必须是使用dateutil
格式的带有日期字符串的字典。
schedule: job1: function: state.sls seconds: 3600 args: - httpd kwargs: test: True range: invert: True start: 8:00am end: 5:00pm
Using the invert option for range, this will schedule the command state.sls httpd test=True
every 3600 seconds (every hour) until the current time is between the hours of 8:00 AM and 5:00 PM. The range parameter must be a dictionary with the date strings using the dateutil
format.
使用范围内反转选项,这将调度命令state.sls httpd test=True 每3600秒(每小时),当前时间是在上午8:00和下午5:00之间的时间。范围参数必须是使用dateutil格式的带有日期字符串的字典。
schedule: job1: function: pkg.install kwargs: pkgs: [{'bar': '>1.2.3'}] refresh: true once: '2016-01-07T14:30:00'
This will schedule the function pkg.install
to be executed once at the specified time. The schedule entry job1
will not be removed after the job completes, therefore use schedule.delete
to manually remove it afterwards.
这将安排在指定的时间执行一次pkg安装的功能。在作业完成后,日程条目job1将不被删除,因此使用schedule.delete手动删除它。
The default date format is ISO 8601 but can be overridden by also specifying the once_fmt
option, like this:
默认日期格式是ISO 8601, 但也可以通过指定once_fmt选项来重写,如下所示:
schedule: job1: function: test.ping once: 2015-04-22T20:21:00 once_fmt: '%Y-%m-%dT%H:%M:%S'
maximum parallel jobs running
New in version 2014.7.0.
The scheduler also supports ensuring that there are no more than N copies of a particular routine running. Use this for jobs that may be long-running and could step on each other or pile up in case of infrastructure outage.
调度器还支持确保不超过一个特定例程的N个副本运行。使用这项工作,可能是长期运行,并可以相互踩踏或堆积在基础设施停电的情况下。
The default for maxrunning
is 1.
默认maxrunning
是1.
schedule:
long_running_job:
function: big_file_transfer
jid_include: True
maxrunning: 1
cron-like schedule
New in version 2014.7.0.
schedule: job1: function: state.sls cron: '*/15 * * * *' args: - httpd kwargs: test: True
The scheduler also supports scheduling jobs using a cron like format. This requires the Python croniter
library.
调度器还支持使用类似cron格式的调度作业。这需要python croniter库。
job data return
New in version 2015.5.0.
By default, data about jobs runs from the Salt scheduler is returned to the master. Setting the return_job
parameter to False will prevent the data from being sent back to the Salt master.
默认情况下,有关从salt调度器运行的作业的数据返回到master。设置return_job参数为False,将防止数据被发送回Salt master。
schedule:
job1:
function: scheduled_job_function
return_job: False
job metadata
New in version 2015.5.0.
It can be useful to include specific data to differentiate a job from other jobs. Using the metadata parameter special values can be associated with a scheduled job. These values are not used in the execution of the job, but can be used to search for specific jobs later if combined with the return_job
parameter. The metadata parameter must be specified as a dictionary, othewise it will be ignored.
将特定的数据用于区分作业和其他作业可能是有用的。使用元数据参数特殊值可以与计划的作业相关联。这些值不用于作业的执行,但如果与 return_job
作业参数结合,则可以用于以后搜索特定作业。元数据参数必须指定为字典,否则将被忽略。
schedule:
job1:
function: scheduled_job_function
metadata:
foo: bar
run on start
New in version 2015.5.0.
By default, any job scheduled based on the startup time of the minion will run the scheduled job when the minion starts up. Sometimes this is not the desired situation. Using the run_on_start
parameter set to False
will cause the scheduler to skip this first run and wait until the next scheduled run:
默认情况下,根据minion的启动时间安排的任何作业将在minion启动时运行预定作业。有时这不是期望的情况。使用run_on_start参数设置为false将导致调度器跳过第一次运行并等待下一次预定运行:
schedule: job1: function: state.sls seconds: 3600 run_on_start: False args: - httpd kwargs: test: True
until and after
New in version 2015.8.0.
schedule: job1: function: state.sls seconds: 15 until: '12/31/2015 11:59pm' args: - httpd kwargs: test: True
Using the until argument, the Salt scheduler allows you to specify an end time for a scheduled job. If this argument is specified, jobs will not run once the specified time has passed. Time should be specified in a format supported by the dateutil
library. This requires the Python dateutil
library to be installed.
使用"until"参数,Salt调度器允许您为计划的作业指定结束时间。如果指定了此参数,则在指定的时间过去后,作业将不运行。应该以dateutil
库支持的格式指定时间。这需要安装Python dateutil
库。
New in version 2015.8.0.
schedule: job1: function: state.sls seconds: 15 after: '12/31/2015 11:59pm' args: - httpd kwargs: test: True
Using the after argument, the Salt scheduler allows you to specify an start time for a scheduled job. If this argument is specified, jobs will not run until the specified time has passed. Time should be specified in a format supported by the dateutil
library. This requires the Python dateutil
library to be installed.
使用"after"参数,Salt调度器允许指定计划作业的开始时间。如果指定了此参数,则作业将在指定时间之前运行。应该以dateutil
库支持的格式指定时间。这需要安装Python dateutil
库。
scheduling states
schedule: log-loadavg: function: cmd.run seconds: 3660 args: - 'logger -t salt < /proc/loadavg' kwargs: stateful: False shell: /bin/sh
scheduling highstates
To set up a highstate to run on a minion every 60 minutes set this in the minion config or pillar:
要在每分钟60分钟内设置一个highstate 运行在一个minion上,将它设置在minion配置或pillar中:
schedule:
highstate:
function: state.highstate
minutes: 60
Time intervals can be specified as seconds, minutes, hours, or days.
时间间隔可以指定为秒、分钟、小时或天。
scheduling runners
Runner executions can also be specified on the master within the master configuration file:
也可以在master配置文件内指定运行程序执行器:
schedule: run_my_orch: function: state.orchestrate hours: 6 splay: 600 args: - orchestration.my_orch
The above configuration is analogous to running salt-run state.orch orchestration.my_orch
every 6 hours.
上述配置类似于每6小时运行 salt-run state.orch orchestration.my_orch
运行状态。
scheduler with returner
The scheduler is also useful for tasks like gathering monitoring data about a minion, this schedule option will gather status data and send it to a MySQL returner database:
调度器对于收集监视数据的任务也是有用的,这个调度选项将收集状态数据并将其发送到MySQL返回者数据库:
schedule: uptime: function: status.uptime seconds: 60 returner: mysql meminfo: function: status.meminfo minutes: 5 returner: mysql
Since specifying the returner repeatedly can be tiresome, the schedule_returner
option is available to specify one or a list of global returners to be used by the minions when scheduling.
由于重复指定返回者可能是令人厌烦的,因此调度程序schedule_returner
选项可用于指定一个或一个全局调度者列表,以便在调度时由minions使用。