系统类模块
cron模块
远程管理主机的计划任务,相当于crontab命令
服务脚本名称:/etc/init.d/crond
主要设置文件
全局配置文件,位于文件:/etc/crontab
系统默认的设置,位于目录:/etc/cron.*/
用户定义的设置,位于文件:/var/spool/cron/用户名
5 1 * * * echo test 每天1点5分
1 1 */3 * * echo test 每3天1点1分
@reboot echo test 每次系统重启
@hourly echo test 每小时执行一次
[centos@self1-centos7-2 ~]$ ansible-doc -s cron
- name: Manage cron.d and crontab entries
cron:
backup: #修改计划任务时先备份. The location of the backup is returned in the `backup_file'
variable by this module.
cron_file: # If specified, uses this file instead of an individual user's crontab. If this is a relative path, it is interpreted with
respect to `/etc/cron.d'. If it is absolute, it will typically be `/etc/crontab'. Many
linux distros expect (and some require) the filename portion to consist solely of upper-
and lower-case letters, digits, underscores, and hyphens. To use the `cron_file' parameter
you must specify the `user' as well.
day: # 天设定 ( 1-31, *, */2, etc )
disabled: # 让计划任务失效,要同job和时间一起,与已存在的进行对应. Only has effect if `state=present'.
env: # If set, manages a crontab's environment variable. New variables are added on top of crontab. `name' and `value'
parameters are the name and the value of environment variable.
hour: # 小时设定值 ( 0-23, *, */2, etc )
insertafter: # Used with `state=present' and `env'. If specified, the environment variable will be inserted after the declaration of
specified environment variable.
insertbefore: # Used with `state=present' and `env'. If specified, the environment variable will be inserted before the declaration of
specified environment variable.
job: # 要执行的命令, if env is set, the value of environment variable. The command should not contain line breaks.
Required if `state=present'.
minute: # 分钟设定值( 0-59, *, */2, etc )
month: # 月设定值 ( 1-12, *, */2, etc )
name: # 计划任务的名称,在一台机器中,名字应该唯一 Required if `state=absent'. Note that
if name is not set and `state=present', then a new crontab entry will always be created,
regardless of existing ones. This parameter will always be required in future releases.
reboot: # If the job should be run at reboot. This option is deprecated. Users should use special_time.
special_time: # 特殊设定值.@reboot @hourly @yearly=@annually @monthly @weekly @daily
state: # 根据名称修改或删除计划任务 is present or absent.
user: # 设定当前计划任务属于哪个用户. When unset, this parameter defaults to using `root'.
weekday: # 周设定值 ( 0-6 for Sunday-Saturday, *, etc )
示例:
[root@self1-centos7-2 18:38:49 ~]#ansible self1-1 -m cron -a 'name="crontab 3day" minute=1 hour=1 day=*/3 job="echo test"'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"crontab 3day"
]
}
[root@self1-centos7-2 18:38:58 ~]#ansible self1-1 -m cron -a 'name="crontab special" special_time=reboot job="echo test"'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"crontab 3day",
"crontab special"
]
}
[root@self1-centos7-3 cron]# cat root
#Ansible: crontab 3day
1 1 */3 * * echo test
#Ansible: crontab special
@reboot echo test
修改:
[root@self1-centos7-2 18:40:16 ~]#ansible self1-1 -m cron -a 'name="crontab special" special_time=hourly job="echo test" backup=yes'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/tmp/crontabKCZwL6",
"changed": true,
"envs": [],
"jobs": [
"crontab 3day",
"crontab special"
]
}
删除:
[root@self1-centos7-2 18:44:56 ~]#ansible self1-1 -m cron -a 'name="crontab special" state=absent backup=yes'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/tmp/crontabsDiAZe",
"changed": true,
"envs": [],
"jobs": [
"crontab 3day" //这里少了一行
]
}
指定用户
[root@self1-centos7-2 18:46:46 ~]#ansible self1-1 -m cron -a 'user=centos name="crontab special" special_time=hourly job="echo test" backup=yes'
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/tmp/crontabuxSdxt",
"changed": true,
"envs": [],
"jobs": [
"crontab special"
]
}
注释任务
[root@self1-centos7-2 18:57:39 ~]#ansible self1-1 -m cron -a "user=centos name='crontab special' special_time=hourly job='echo test' disabled=yes backup=yes state=present"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/tmp/crontabxmhO1A",
"changed": true,
"envs": [],
"jobs": [
"crontab special"
]
}
service模块
管理主机上的服务,比如启动停止远程主机中的nginx服务
这个服务必须能被BSD init ,OpenRC, SysV(centos6) Solaris SMF, systemd(centos7) upstart(C5)中任一管理
[root@self1-centos7-2 19:01:00 ~]#ansible-doc -s service
- name: Manage services
service:
arguments: # Additional arguments provided on the command line.
enabled: # 是否设置为开机启动
name: # (required) Name of the service.比如nginx
pattern: # If the service does not respond to the status command, name a substring to look for as would be found in the output of
the `ps' command as a stand-in for a status result. If the string is found, the service
will be assumed to be started.
runlevel: # For OpenRC init scripts (e.g. Gentoo) only. The runlevel that this service belongs to.
sleep: # If the service is being `restarted' then sleep this many seconds between the stop and start command. This helps to work
around badly-behaving init scripts that exit immediately after signaling a process to
stop. Not all service managers support sleep, i.e when using systemd this setting will be
ignored.
state: # `started'/`stopped' `restarted' `reloaded' 服务的状态
use: # The service module actually uses system specific modules, normally through auto detection, this setting can force a
specific module. Normally it uses the value of the 'ansible_service_mgr' fact and falls
back to the old 'service' module when none matching is found.
示例:
[root@self1-centos7-2 19:05:45 ~]#ansible self1-1 -m service -a "name=nginx state=started"
[root@self1-centos7-2 19:05:45 ~]#ansible self1-1 -m service -a "name=nginx state=stoped"
[root@self1-centos7-2 19:05:45 ~]#ansible self1-1 -m service -a "name=nginx enabled=yes"