如何用ansible cron查看crontable任务

Ansible Cron 模块讲解, - 推酷
Ansible Cron 模块讲解,
[root@localhost ~]# ansible 127.0.0.1 -m cron -a &name='Test cron' minute=*/10 hour=1,3,5,7,9 month=10 user=root job='ntpdate us.pool.ntp.org'&
127.0.0.1 | success && {
&&&&&changed&: true,
&&&&&jobs&: [
&&&&&&&&&Test cron&
Cron - 运行管理任务计划和 crontab 条目,可以新建、删除 crontab 条目。
点击(此处)折叠或打开
[root@localhost ~]# ansible-doc cron
## 打印 Cron 模块帮助信息。
name&&&&&&&& 任务计划的描述信息。
cron_file&&&&如果指定此文件,将替换远程主机上的该用户的任务计划。
minute&&&&&& 分( 0-59 ,* ,*/2 )
hour&&&&&&&& 时( 0-23 ,* ,*/2 )
day&&&&&&&&&&日( 1-31 ,* ,*/2 )
month&&&&&&&&月( 1-12 ,* ,*/2 )
weekday&&&&&&周( 0-6 或 1-7 ,* )
job&&&&&&&&&&要执行的命令,state 要等于 present 。
backup&&&&&& 是否先备份再创建新的任务计划。
user&&&&&&&& 以哪个用户新建任务计划,默认 root 。
state&&&&&& (present/absent) 指定任务计划是创建,还是删除。
special_time&&&&(reboot/yearly/annually/monthly/weekly/daily/hourly)
Special time specification nickname. (added in Ansible 1.3)
点击(此处)折叠或打开
[root@localhost ~]# ansible 127.0.0.1 -m cron -a &name='Test cron' minute=*/10 hour=1,3,5,7,9 month=10 user=root job='ntpdate us.pool.ntp.org'&
127.0.0.1 | success && {
&changed&: true,
&Test cron&
## 以 root 用户身份创建,10 月份的每天 1,3,5,7,9 点,每 10 分钟执行 ntpdate us.pool.ntp.org 。
点击(此处)折叠或打开
[root@localhost ~]# crontab -l
#Ansible: Test cron
*/10 1,3,5,7,9 * 10 * ntpdate us.pool.ntp.org
点击(此处)折叠或打开
[root@localhost ~]# ansible 127.0.0.1 -m cron -a &name='Test special_time' special_time=reboot job='echo reboot server'&
127.0.0.1 | success && {
&changed&: true,
&Test cron&,
&Test special_time&
## 每次重启系统时,执行 Echo reboot server 。
点击(此处)折叠或打开
[root@localhost ~]# crontab -l
#Ansible: Test cron
*/10 1,3,5,7,9 * 10 * ntpdate us.pool.ntp.org
#Ansible: Test special_time
@reboot echo reboot server
点击(此处)折叠或打开
[root@localhost ~]# ansible 127.0.0.1 -m cron -a &name='Test cron' state=absent&
127.0.0.1 | success && {
&changed&: true,
&Test special_time&
## 删除创建的第一个任务计划。
点击(此处)折叠或打开
[root@localhost ~]# crontab -l
#Ansible: Test special_time
@reboot echo reboot server
点击(此处)折叠或打开
[root@localhost ~]# ansible 127.0.0.1 -m cron -a &name='Test backup' minute=59 hour=23 job='reboot' backup=yes&
127.0.0.1 | success && {
&backup_file&: &/tmp/crontabpIQCt2&,
&changed&: true,
&Test special_time&,
&Test backup&
## 测试 cron 的 backup 参数,可以看到将原任务计划备份到了 /tmp/crontabpIQCt2 这个文件中。
点击(此处)折叠或打开
[root@localhost ~]# cat /tmp/crontabpIQCt2
#Ansible: Test special_time
@reboot echo reboot server
http://blog.chinaunix.net/uid--id-5068485.html
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致Linuxcron计划任务日志跟踪
场景: 需要在的cron中启动一个计划任务,跑的是一个shell脚本,脚本中大致意思是切换至
abc用户执行一个php文件(例如: /data/soft/auto.php),那么现在问题来了,脚本的内
#!/bin/bash
sudo -u abc /data/soft/auto .php
echo ' &&& auto success !!! &
你会神奇的发现,系统的/var/log/cron日志中已经显示auto success,但是这个程序依然
是没有执行,你怎么知道没执行呢?因为程序有日志,日志没开始刷.
那么下一步该怎么来查找程序未执行的原因?思路是每次执行cron的时候必定会产生记录的文件
根据系统版本查找文件的位置:
操作系统版本4/5 查看 /var/spool/clientqueue/
操作系统版本6 查看 /var/spool/postfix/maildrop/
系统的postfix停掉,没停掉就直接敲mail命令看
进入对应的目录后可以使用 ls -lt|head 命令来截取最后执行的10个cron日志文件,根据时间来判定看
哪个文件,里面必定有报错.根据报错来修改脚本或者系统属性.
我遇到过的两种情况:
1. sudo系统默认警告需要tty,解决办法:将/etc/sudoers文件中Defaults requiretty注释掉.
2. 变量路径引起的,加载你所需要的变量或者定义.
好久没有写,这个问题值得记录,相信很多人还不知道这个排错方式,这也是刚从别人那学的.
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'Perin丶 的BLOG
用户名:Perin丶
文章数:12
评论数:10
访问量:2001
注册日期:
阅读量:5863
阅读量:12276
阅读量:375115
阅读量:1068506
51CTO推荐博文
常用模块使用首先在使用模块之前我们可以查看一下模块有多少,不过大多数用不到。下面主要看几个常用模块[root@soso&~]#&ansible-doc&-l&|&wc&-l
2621、setup##用来查看远程主机的基本信息[root@soso&~]#&ansible&test&-m&setup2、ping##用来测试远程主机的运行状态[root@soso&~]#&ansible&test&-m&ping3、file##设置文件属性[root@soso&~]#&ansible-doc&-s&file
-&name:&Sets&attributes&of&files
action:&file
force&&&&&&&&&&&&&&&&&&#&需要在两种情况下创建软连接,一种是源文件不存在,但之后建立的情况下;另一种是目标软连接已存在,需要先取消之前的软链,然后创建新的软链:有两个选项:yes|no
group&&&&&&&&&&&&&&&&&&#&定义文件/目录的所属组
mode&&&&&&&&&&&&&&&&&&&#&定义文件/目录的权限
owner&&&&&&&&&&&&&&&&&&#&定义文件/目录的所属主
path=&&&&&&&&&&&&&&&&&&#&必选项,定义文件/目录的路径
recurse&&&&&&&&&&&&&&&&#&递归设置文件的属性,只对目录有效
src&&&&&&&&&&&&&&&&&&&&#&要被链接的源文件路径,只应用于state=link的情况有效
state&&&&&&&&&&&&&&&&&&#&状态
&&&&&&&&directory&&&&&&&&#如果目录不存在则创建目录
&&&&&&&&file&&&&&&&&&&&&&&&&#即使文件不存在,也不会被创建
&&&&&&&&link&&&&&&&&&&&&&&&&#创建软连接
&&&&&&&&hard&&&&&&&&&&&&&&&&#创建硬链接
&&&&&&&&touch&&&&&&&&&&&&&&&#如果文件不存在,则会创建一个新的文件;如果文件或目录存在,则更新其最后的修改时间
&&&&&&&&absent&&&&&&&&&&&&&&#删除目录、文件或者取消链接文件
(END)&&&&将远程主机/etc/passwd 文件链接到 /root 目录下:[root@soso&~]#&ls
Python-2.7.5&&shell&&soft
[root@soso&~]#&ansible&test&-m&file&-a&"src=/etc/passwd&dest=/root/passwd&state=link"&&&&&&&&&&&&&&&&
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"dest":&"/root/passwd",&
&&&&"gid":&0,&
&&&&"group":&"root",&
&&&&"mode":&"0777",&
&&&&"owner":&"root",&
&&&&"size":&11,&
&&&&"src":&"/etc/passwd",&
&&&&"state":&"link",&
&&&&"uid":&0
[root@soso&~]#&ll
drwxr-xr-x&18&96&Nov&26&11:11&Python-2.7.5
lrwxrwxrwx&&1&root&root&&&11&Dec&16&15:35&passwd&-&&/etc/passwd
drwxr-xr-x&&3&root&root&4096&Dec&&7&15:06&shell
drwxr-xr-x&12&root&root&4096&Dec&16&15:32&soft&&&&删除创建的软连接[root@soso&~]#&ansible&test&-m&file&-a&'path=/root/passwd&state=absent'&&&&&&&&&&&
192.168.1.2&&|&success&&&&{
&&&&"changed":&true,&
&&&&"path":&"/root/passwd",&
&&&&"state":&"absent"
[root@soso&~]#&ll
drwxr-xr-x&18&96&Nov&26&11:11&Python-2.7.5
drwxr-xr-x&&3&root&root&4096&Dec&&7&15:06&shell
drwxr-xr-x&12&root&root&4096&Dec&16&15:32&soft&&&&创建文件[root@soso&~]#&ansible&test&-m&file&-a&'path=/root/1&state=touch'
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"dest":&"/root/1",&
&&&&"gid":&0,&
&&&&"group":&"root",&
&&&&"mode":&"0644",&
&&&&"owner":&"root",&
&&&&"size":&0,&
&&&&"state":&"file",&
&&&&"uid":&0
[root@soso&~]#&ls
1&&Python-2.7.5&&shell&&soft4、cope##复制本地文件到远程主机,进行统一部署用法:&&&&backup & & & & &# 用于复制时,是否备份远程主机上的目标文件&&&&content & & & & # 用于替代 ‘src’ ,可以直接设定指定文件的值& & dest= & & & & & # 必选项,要将源文件复制到远程主机的绝对路径,如果源文件是一个目录,那么远程路径也必须是一个目录&&&&directory_mode & & & & # 递归设定目录的权限,默认为系统默认权限&&&&force & & & & & & & & &# 强制覆盖。如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖;如果设置为no,则只有当目录主机的目录位置不存在该文件时,才复制。默认为yes&&&&group & & & & & & & & &# 定义文件/目录的所属组&&&&mode & & & & & & & & & # 定义文件/目录的权限&&&&owner & & & & & & &&&&&# 定义文件/目录的所属主&&&&src & & & & & & & &&&&&# 要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用 '/' 结尾,则只复制目录里的内容,如果没有使用 '/' 来结尾,则包含目录在内的整个内容全部复制。实例:讲本地文件复制到远程主机[root@soso&~]#&&ansible&test&-m&copy&-a&"src=/root/1&dest=/tmp/1&&mode=600&"&&&&&&&&&&&&&&&&&&&&&&&&&
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"checksum":&"da39a3ee5e6b4b0d3255bfefafd80709",&
&&&&"dest":&"/tmp/1",&
&&&&"gid":&0,&
&&&&"group":&"root",&
&&&&"md5sum":&"d41d8cd98f00b204e9800998ecf8427e",&
&&&&"mode":&"0600",&
&&&&"owner":&"root",&
&&&&"size":&0,&
&&&&"src":&"/root/.ansible/tmp/ansible-tmp-.51-88/source",&
&&&&"state":&"file",&
&&&&"uid":&0
[root@soso&~]#&ll&/tmp/1&
-rw-------&1&root&root&0&Dec&16&15:50&/tmp/15、command##远程主机上执行命令creates:一个文件名,当该文件存在,则该命令不执行free_form:要执行的linux指令chdir:在执行指令之前,先切换到该目录removes:一个文件名,当该文件不存在,则该选项不执行executable:切换shell来执行指令,该执行路径必须是一个绝对路径[root@soso&~]#&ansible&test&-m&command&-a&'ls&/home'
192.168.1.2|&success&|&rc=0&&&
zabbix实例:使用 creates 参数,判断一个文件是否存在,存在的话,就跳过后面的执行命令[root@soso&~]#&ansible&test&-m&command&-a&'creates=/tmp/1&ls&-l&/etc/passwd'&&&&&&&&&&&&&&
192.168.1.2&&|&success&|&rc=0&&&
skipped,&since&/tmp/1&exists
[root@soso&~]#&ansible&test&-m&command&-a&'creates=/tmp/2&ls&-l&/etc/passwd'&
192.168.1.2&|&success&|&rc=0&&&
-rw-r--r--&1&root&root&1882&Dec&14&20:11&/etc/passwd6、shell##与command不同的是,此模块可以支持命令管道,同时还有另一个模块也具备此功能:raw实例1:[root@soso&~]#&ansible&test&-m&shell&-a&'cat&/etc/passwd|&grep&root'
192.168.1.2&&|&success&|&rc=0&&&
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin7、synchronize使用rsync同步文件。使用rsync 模块,系统必须安装rsync 包,否则无法使用这个模块用法:&&&&archive & & & & & & & &# 是否采用归档模式同步,即以源文件相同属性同步到目标地址&&&&checksum & & & & & & & # 是否效验&&&&compress & & & & & & & #&&&&&copy_links & & & & & & # 同步的时候是否复制连接&&&&delete & & & & & & & & # 删除源中没有而目标存在的文件&&&&dest= & & & & & & & & &# 目标地址&&&&dest_port & & & & & & &# 目标接受的端口&&&&dirs & & & & & & & & & # 以非递归的方式传输目录&&&&existing_only & & & & &# Skip creating new files on receiver.&&&&group & & & & & & & & &# Preserve group&&&&links & & & & & & & & &# Copy symlinks as symlinks.&&&&mode & & & & & & & & & # 模式,rsync 同步的方式 PUSH\PULL&&&&recursive & & & & & & &# 是否递归 yes/no&&&&rsync_opts & & & & & & # 使用rsync 的参数&&&&rsync_path & & & & & & # 服务的路径(源码编译时需指定)&&&&rsync_timeout & & & & &# Specify a --timeout for the rsync command in seconds.&&&&set_remote_user & & & &# put user@ for the remote paths. If you have a custom ssh &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&config to define the remote user for&&&&src=\'#\'"
& & & & & & & & # 源,同步的数据源检查机器上是否装过rsync:&&[root@soso&~]#which&rsync没有装过的主机可以远程安装rsync包:&[root@soso&~]#&&ansible&test&-m&yum&-a&'name=rsync&state=latest'实例:1、将ansible 服务端/root/ 下的hello 文件同步到test的/tmp:[root@soso&~]#&ansible&test&-m&&synchronize&-a&'src=/root/hello&dest=/tmp/'
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"cmd":&"rsync&--delay-updates&-F&--compress&--archive&--rsh&'ssh&&-S&none&-o&StrictHostKeyChecking=no'&--out-format='&&CHANGED&&%i&%n%L'&\"/root/hello\"&\"root@121.42.141.231:/tmp/\"",&
&&&&"msg":&"&f+++++++++&hello\n",&
&&&&"rc":&0,&
&&&&"stdout_lines":&[
&&&&&&&&"&f+++++++++&hello"
[root@soso&~]#&ls&/tmp/hello&
/tmp/hello8、croncron 模块,用于管理计划任务用法:&&&&backup & & & & & & & & # 在对远程主机上的原计划任务修改之前做备份(也就是先备份再修改)&&&&cron_file & & & & & & &# 如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划&&&&day & & & & & & & & & &# 天(1-31,*,*/2, ……)&&&&hour & & & & & & & & & # 小时( 0-23, *, */2, ……)&&&&job & & & & & & & & & &# T要执行的任务,依赖于state=present&&&&minute & & & & & & & & # 分钟(0-59,*,*/2,……)&&&&month & & & & & & & & &# 月(1-12,*,*/2,……)&&&&name & & & & & & & & & # 该任务的描述&&&&reboot & & & & & & & & # If the job should be run at reboot. This option&&&&special_time & & & & & #指定什么时候执行,参数:&&&&&&&&reboot,yearly,annually,monthly,weekly,daily,hourly& & &&&&&&state & & & & & & & & &# 确认该任务计划是创建还是删除&&&&user & & & & & & & & & #以哪个用户的身份执行&&&&weekday & & & & & & & &#周(0-7,*,……)实例:1、给test 主机创建一个计划任务:[root@soso&~]#&&ansible&test&-m&cron&-a&"name='echo&'&hour=2&user=root&job='echo&1&&&&/root/hello'&"&&&&&&&&&&&&
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"jobs":&[
&&&&&&&&"echo&"
[root@soso&~]#&ansible&test&-m&shell&-a&"crontab&-l"&&&&
192.168.1.2&|&success&|&rc=0&&&
*&*&*&*&1&sh&/root/shell/apache_log.sh&
#Ansible:&echo
*&2&*&*&*&echo&1&&&&/root/hello2、删除刚才创建的计划任务[root@soso&~]#&&ansible&test&-m&cron&-a&"name='echo'&hour=2&user=root&job='echo&1&&&&/root/hello'&state=absent"
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"jobs":&[]
[root@soso&~]#&ansible&test&-m&shell&-a&"crontab&-l"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&192.168.1.2&|&success&|&rc=0&&&
*&*&*&*&1&sh&/root/shell/apache_log.sh9、service&&&&service 模块作用于一些服务,比如对某些服务器的启动、重启、停止、重载等的管理&&&&arguments & & & & & & &# 给命令行提供一些选项&&&&enabled & & & & & & & &# 是否开机启动 &yes|no&&&&name= & & & & & & & & &# 必选项,服务名称&&&&pattern & & & & & & & &# 定义一个模式,如果通过status指令来查看服务的状态时,没有响应,就会通过ps指令在进程中根据该模式进行查找,如果匹配到,则认为该服务依然在运行&&&&runlevel & & & & & & & # 运行级别&&&&sleep & & & & & & & & &# 如果执行了restarted,在则stop和start之间沉睡几秒钟&&&&state & & & & & & & & &# 对当前服务执行启动,停止、重启、重新加载等操作&&&&&&&& & & &&(started,stopped,restarted,reloaded)实例:实例:远程启动http[root@soso&~]#&ss&-nl
State&&&&&&Recv-Q&Send-Q&&&&&&&&Local&Address:Port&&&&&&&&&&Peer&Address:Port&
LISTEN&&&&&0&&&&&&80&&&&&&&&&&&&&&&&&&&&&&&&*:3306&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:111&&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:22&&&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:43131&&&&&&&&&&&&&&&&&&&&*:*&&&&&
[root@soso&~]#&ansible&test&-m&service&-a&"name=httpd&state=started&enabled=yes"&
192.168.1.2&|&success&&&&{
&&&&"changed":&true,&
&&&&"enabled":&true,&
&&&&"name":&"httpd",&
&&&&"state":&"started"
[root@soso&~]#&ss&-nl
State&&&&&&Recv-Q&Send-Q&&&&&&&&Local&Address:Port&&&&&&&&&&Peer&Address:Port&
LISTEN&&&&&0&&&&&&80&&&&&&&&&&&&&&&&&&&&&&&&*:3306&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:111&&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:80&&&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:22&&&&&&&&&&&&&&&&&&&&&&&*:*&&&&&
LISTEN&&&&&0&&&&&&128&&&&&&&&&&&&&&&&&&&&&&&*:43131&&&&&&&&&&&&&&&&&&&&*:*其他常用的模块还有 &yum & user &filesystem mount &本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)今天看啥 热点:
ansible模块cron、copy、user、group
查询模块的参数:
ansible-doc-smoduleName
如 cron 模块
[[email protected]]#ansible-doc-scron
Copyright(C)MarkNudelman
lesscomeswithNOWARRANTY,totheextentpermittedbylaw.
Forinformationaboutthetermsofredistribution,
seethefilenamedREADMEinthelessdistribution.
Homepage:/less
-name:Managecron.dandcrontabentries.
action:cron
backup#Ifset,createabackupofthecrontabbeforeitismodified.Thelocationofthebackupisreturnedinthe`backup'variablebythismo
cron_file#Ifspecified,usesthisfileincron.dinsteadofanindividualuser'scrontab.
day#Dayofthemonththejobshouldrun(1-31,*,*/2,etc)
hour#Hourwhenthejobshouldrun(0-23,*,*/2,etc)
job#Thecommandtoexecute.Requiredifstate=present.
minute#Minutewhenthejobshouldrun(0-59,*,*/2,etc)
month#Monthoftheyearthejobshouldrun(1-12,*,*/2,etc)
name=#Descriptionofacrontabentry.
reboot#Ifthejobshouldberunatreboot.Thisoptionisdeprecated.Usersshouldusespecial_time.
special_time#Specialtimespecificationnickname.
state#Whethertoensurethejobispresentorabsent.
user#Thespecificuserwhosecrontabshouldbemodified.
weekday#Dayoftheweekthatthejobshouldrun(0-6forSunday-Saturday,*,etc)
1、cron 模块
示例:每小时的10分钟时候执行一个脚本 默认是*
[[email protected]]#ansibletestservers-mcron-a'name=&testcron&,&minute=10&job=&/bin/bash/root/test.sh&state=present'
192.168.100.131|success&&{
&changed&:true,
&testcron,&
192.168.100.132|success&&{
&changed&:true,
&testcron,&
[[email protected]]#ansibletestservers-mcommand-a'crontab-l'
192.168.100.131|success|rc=0&&
#Ansible:testcron,
10****/bin/bash/root/test.sh
192.168.100.132|success|rc=0&&
#Ansible:testcron,
10****/bin/bash/root/test.sh
删除上面的计划任务
[[email protected]]#ansibletestservers-mcron-a'name=&testcron&,&minute=10&job=&/bin/bash/root/test.sh&state=absent'
192.168.100.131|success&&{
&changed&:true,
192.168.100.132|success&&{
&changed&:true,
[[email protected]]#ansibletestservers-mcommand-a'crontab-l'
192.168.100.132|success|rc=0&&
192.168.100.131|success|rc=0&&
2、user 模块
[[email protected]]#ansible-doc-suser
Copyright(C)MarkNudelman
lesscomeswithNOWARRANTY,totheextentpermittedbylaw.
Forinformationaboutthetermsofredistribution,
seethefilenamedREADMEinthelessdistribution.
Homepage:/less
-name:Manageuseraccounts
action:user
append#If`yes',willonlyaddgroups,notsetthemtojustthelistin`groups'.
comment#Optionallysetsthedescription(aka`GECOS')ofuseraccount.
createhome#Unlesssetto`no',ahomedirectorywillbemadefortheuserwhentheaccountiscreatedorifthehomedirectorydoesnotexist.
expires#Anexpirytimefortheuserinepoch,itwillbeignoredonplatformsthatdonotsupportthis.CurrentlysupportedonLinuxandFreeBSD
force#Whenusedwith`state=absent',behaviorisaswith`userdel--force'.
generate_ssh_key#WhethertogenerateaSSHkeyfortheuserinquestion.Thiswill*not*overwriteanexistingSSHkey.
group#Optionallysetstheuser'sprimarygroup(takesagroupname).
groups#Putstheuserinthiscomma-delimitedlistofgroups.Whensettotheemptystring('groups='),theuserisremovedfromallgroupsexce
home#Optionallysettheuser'shomedirectory.
login_class#Optionallysetstheuser'sloginclassforFreeBSD,OpenBSDandNetBSDsystems.
move_home#Ifsetto`yes'whenusedwith`home=',attempttomovetheuser'shomedirectorytothespecifieddirectoryifitisn'ttherealready.
name=#Nameoftheusertocreate,removeormodify.
non_unique#Optionallywhenusedwiththe-uoption,thisoptionallowstochangetheuserIDtoanon-uniquevalue.
password#Optionallysettheuser'spasswordtothiscryptedvalue.Seetheuserexampleinthegithubexamplesdirectoryforwhatthislookslik
remove#Whenusedwith`state=absent',behaviorisaswith`userdel--remove'.
shell#Optionallysettheuser'sshell.
ssh_key_bits#OptionallyspecifynumberofbitsinSSHkeytocreate.
ssh_key_comment#OptionallydefinethecommentfortheSSHkey.
ssh_key_file#OptionallyspecifytheSSHkeyfilename.Ifthisisarelativefilenamethenitwillberelativetotheuser'shomedirectory.
ssh_key_passphrase#SetapassphrasefortheSSHkey.Ifnopassphraseisprovided,theSSHkeywilldefaulttohavingnopassphrase.
ssh_key_type#OptionallyspecifythetypeofSSHkeytogenerate.AvailableSSHkeytypeswilldependonimplementationpresentontargethost.
state#Whethertheaccountshouldexistornot,takingactionifthestateisdifferentfromwhatisstated.
system#Whencreatinganaccount,settingthisto`yes'makestheuserasystemaccount.Thissettingcannotbechangedonexistingusers.
uid#Optionallysetsthe`UID'oftheuser.
update_password#`always'willupdatepasswordsiftheydiffer.`on_create'willonlysetthepasswordfornewlycreatedusers.
[[email protected]]#
创建一个用户testuser uid=1000
[[email protected]]#ansibletestservers-muser-a'name=testuseruid=1000shell=/bin/bashhome=/opt/testuserstate=present'
192.168.100.131|success&&{
&changed&:true,
&comment&:&&,
&createhome&:true,
&group&:1000,
&home&:&/opt/testuser&,
&name&:&testuser&,
&shell&:&/bin/bash&,
&state&:&present&,
&system&:false,
&uid&:1000
192.168.100.132|success&&{
&changed&:true,
&comment&:&&,
&createhome&:true,
&group&:1000,
&home&:&/opt/testuser&,
&name&:&testuser&,
&shell&:&/bin/bash&,
&state&:&present&,
&system&:false,
&uid&:1000
删除此用户 连同用户家目录:
[[email protected]]#ansibletestservers-muser-a'name=testuserremove=yesstate=absent'
192.168.100.131|success&&{
&changed&:true,
&force&:false,
&name&:&testuser&,
&remove&:true,
&state&:&absent&
192.168.100.132|success&&{
&changed&:true,
&force&:false,
&name&:&testuser&,
&remove&:true,
&state&:&absent&
3、group模块
[[email protected]]#ansible-doc-sgroup
Copyright(C)MarkNudelman
lesscomeswithNOWARRANTY,totheextentpermittedbylaw.
Forinformationaboutthetermsofredistribution,
seethefilenamedREADMEinthelessdistribution.
Homepage:/less
-name:Addorremovegroups
action:group
gid#Optional`GID'tosetforthegroup.
name=#Nameofthegrouptomanage.
state#Whetherthegroupshouldbepresentornotontheremotehost.
system#If`yes',indicatesthatthegroupcreatedisasystemgroup.
[[email protected]]#
创建一个用户组
[[email protected]]#ansibletestservers-mgroup-a'name=testgroupgid=1100state=present'
192.168.100.131|success&&{
&changed&:true,
&gid&:1100,
&name&:&testgroup&,
&state&:&present&,
&system&:false
192.168.100.132|success&&{
&changed&:true,
&gid&:1100,
&name&:&testgroup&,
&state&:&present&,
&system&:false
综合示例:
在主机上创建一个testgroup组,然后创建一个testuser,并将其加入testgroup组
[[email protected]]#ansibletestservers-mgroup-a'name=testgroupgid=1100state=present'
[[email protected]]#ansibletestservers-muser-a'name=testuseruid=1000shell=/bin/bashhome=/opt/testusergroup=testgroupstate=present'
[[email protected]]#ansibletestservers-mcommand-a'idtestuser'
192.168.100.132|success|rc=0&&
uid=1000(testuser)gid=1100(testgroup)groups=1100(testgroup)
192.168.100.131|success|rc=0&&
uid=1000(testuser)gid=1100(testgroup)groups=1100(testgroup)
4、copy 模块
[[email protected]]#ansible-doc-scopy
Copyright(C)MarkNudelman
lesscomeswithNOWARRANTY,totheextentpermittedbylaw.
Forinformationaboutthetermsofredistribution,
seethefilenamedREADMEinthelessdistribution.
Homepage:/less
-name:Copiesfilestoremotelocations.
action:copy
backup#Createabackupfileincludingthetimestampinformationsoyoucangettheoriginalfilebackifyousomehowclobbereditincorrectly.
content#Whenusedinsteadof'src',setsthecontentsofafiledirectlytothespecifiedvalue.
dest=#Remoteabsolutepathwherethefileshouldbecopiedto.Ifsrcisadirectory,thismustbeadirectorytoo.
directory_mode#Whendoingarecursivecopysetthemodeforthedirectories.Ifthisisnotsetwewillusethesystemdefaults.Themodeisonlyseto
follow#Thisflagindicatesthatfilesystemlinks,iftheyexist,shouldbefollowed.
force#thedefaultis`yes',whichwillreplacetheremotefilewhencontentsaredifferentthanthesource.If`no',thefilewillonlybetr
group#nameofthegroupthatshouldownthefile/directory,aswouldbefedto`chown'
mode#modethefileordirectoryshouldbe,suchas0644aswouldbefedto`chmod'.Asofversion1.8,themodemaybespecifiedasasymboli
owner#nameoftheuserthatshouldownthefile/directory,aswouldbefedto`chown'
selevel#levelpartoftheSELinuxfilecontext.ThisistheMLS/MCSattribute,sometimesknownasthe`range'.`_default'featureworksasfor`
serole#rolepartofSELinuxfilecontext,`_default'featureworksasfor`seuser'.
setype#typepartofSELinuxfilecontext,`_default'featureworksasfor`seuser'.
seuser#userpartofSELinuxfilecontext.Willdefaulttosystempolicy,ifapplicable.Ifsetto`_default',itwillusethe`user'portionof
src#Localpathtoafiletoccanbeabsoluteorrelative.Ifpathisadirectory,itiscopiedrecursively.Inthi
validate#Thevalidationcommandtorunbeforecopyingintoplace.Thepathtothefiletovalidateispassedinvia'%s'whichmustbepresenta
[[email protected]]#
Ps:src 可以为绝对或相对路径 dest 必须为绝对路径
示例:将文件copy到测试主机:
[[email protected]]#ansibletestservers-mcopy-a'src=/root/install.logdest=/tmp/install.logowner=testusergroup=testgroup'
192.168.100.131|success&&{
&changed&:true,
&checksum&:&7bdece878fdc4756c94&,
&dest&:&/tmp/install.log&,
&gid&:1100,
&group&:&testgroup&,
&md5sum&:&c7d8a01a3be&,
&mode&:&0644&,
&owner&:&testuser&,
&size&:9458,
&src&:&/root/.ansible/tmp/ansible-tmp-.94-766/source&,
&state&:&file&,
&uid&:1000
192.168.100.132|success&&{
&changed&:true,
&checksum&:&7bdece878fdc4756c94&,
&dest&:&/tmp/install.log&,
&gid&:1100,
&group&:&testgroup&,
&md5sum&:&c7d8a01a3be&,
&mode&:&0644&,
&owner&:&testuser&,
&size&:9458,
&src&:&/root/.ansible/tmp/ansible-tmp-.94-050/source&,
&state&:&file&,
&uid&:1000
示例:copy 前先备份
[[email protected]]#echo&test&&&/root/install.log
[[email protected]]#ansibletestservers-mcopy-a'src=/root/install.logdest=/tmp/install.logowner=testusergroup=testgroupbackup=yes'
192.168.100.132|success&&{
&backup_file&:&[email protected]:01:26~&,
&changed&:true,
&checksum&:&b5da7af32ad02eb98fa965b4c1f5&,
&dest&:&/tmp/install.log&,
&gid&:1100,
&group&:&testgroup&,
&md5sum&:&d39956add30a181a0cd43&,
&mode&:&0644&,
&owner&:&testuser&,
&size&:9464,
&src&:&/root/.ansible/tmp/ansible-tmp-.87-967/source&,
&state&:&file&,
&uid&:1000
192.168.100.131|success&&{
&backup_file&:&[email protected]:01:26~&,
&changed&:true,
&checksum&:&b5da7af32ad02eb98fa965b4c1f5&,
&dest&:&/tmp/install.log&,
&gid&:1100,
&group&:&testgroup&,
&md5sum&:&d39956add30a181a0cd43&,
&mode&:&0644&,
&owner&:&testuser&,
&size&:9464,
&src&:&/root/.ansible/tmp/ansible-tmp-.86-647/source&,
&state&:&file&,
&uid&:1000
[[email protected]]#ansibletestservers-mraw-a'ls-lrth/tmp/install*'
192.168.100.131|success|rc=0&&
-rw-r--r--1rootroot9.3K22516:[email protected]:01:26~
-rw-r--r--1testusertestgroup9.3K22516:01/tmp/install.log
192.168.100.132|success|rc=0&&
-rw-r--r--1rootroot9.3K22516:[email protected]:01:26~
-rw-r--r--1testusertestgroup9.3K22516:01/tmp/install.log
示例:将目录copy过去
[[email protected]]#treetestdir
││└──ansible.cfg
├──ansible.cfg
[[email protected]]#ansibletestservers-mcopy-a'src=/etc/ansible/testdirdest=/tmp/owner=testusergroup=testgroupbackup=yes'
192.168.100.131|success&&{
&changed&:true,
&dest&:&/tmp/&,
&src&:&/etc/ansible/testdir&
192.168.100.132|success&&{
&changed&:true,
&dest&:&/tmp/&,
&src&:&/etc/ansible/testdir&
[[email protected]]#ansibletestservers-mcommand-a'tree/tmp/testdir'
192.168.100.131|success|rc=0&&
/tmp/testdir
|`--ansible.cfg
`--ansible.cfg
5directories,3files
192.168.100.132|success|rc=0&&
/tmp/testdir
|`--ansible.cfg
`--ansible.cfg
5directories,3files
发现有文件的目录copy成功,空的目录没有copy过去
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&&&&&&&&&&&
疑难问题最近更新

我要回帖

更多关于 ansible 查看日志 的文章

 

随机推荐