ansible模块cron、copy、user、group推荐

查询模块的参数:

ansible-doc-smoduleName

如 cron 模块

[root@node1ansible]#ansible-doc-scronless436Copyright(C)1984-2009MarkNudelmanlesscomeswithNOWARRANTY,totheextentpermittedbylaw.Forinformationaboutthetermsofredistribution,seethefilenamedREADMEinthelessdistribution.Homepage:http://www.greenwoodsoftware.com/less-name:Managecron.dandcrontabentries.action:cronbackup#Ifset,createabackupofthecrontabbeforeitismodified.Thelocationofthebackupisreturnedinthe`backup'variablebythismocron_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分钟时候执行一个脚本 默认是*

[root@node1ansible]#ansibletestservers-mcron-a'name="testcron","minute=10"job="/bin/bash/root/test.sh"state=present'192.168.100.131|success {"changed":true,"jobs":["testcron,"]192.168.100.132|success {"changed":true,"jobs":["testcron,"][root@node1ansible]#ansibletestservers-mcommand-a'crontab-l'192.168.100.131|success|rc=0 #Ansible:testcron,10****/bin/bash/root/test.sh192.168.100.132|success|rc=0 #Ansible:testcron,10****/bin/bash/root/test.sh

删除上面的计划任务

[root@node1ansible]#ansibletestservers-mcron-a'name="testcron","minute=10"job="/bin/bash/root/test.sh"state=absent'192.168.100.131|success {"changed":true,"jobs":[]192.168.100.132|success {"changed":true,"jobs":[][root@node1ansible]#ansibletestservers-mcommand-a'crontab-l'192.168.100.132|success|rc=0 192.168.100.131|success|rc=0 

2、user 模块

[root@node1ansible]#ansible-doc-suserless436Copyright(C)1984-2009MarkNudelmanlesscomeswithNOWARRANTY,totheextentpermittedbylaw.Forinformationaboutthetermsofredistribution,seethefilenamedREADMEinthelessdistribution.Homepage:http://www.greenwoodsoftware.com/less-name:Manageuseraccountsaction:userappend#If`yes',willonlyaddgroups,notsetthemtojustthelistin`groups'.comment#Optionallysetsthedescription(aka`GECOS')ofuseraccount.createhome#Unlesssetto`no',ahomedirectorywillbemadefortheuserwhentheaccountiscreatedorifthehomedirectorydoesnotexist.expires#Anexpirytimefortheuserinepoch,itwillbeignoredonplatformsthatdonotsupportthis.CurrentlysupportedonLinuxandFreeBSDforce#Whenusedwith`state=absent',behaviorisaswith`userdel--force'.generate_ssh_key#WhethertogenerateaSSHkeyfortheuserinquestion.Thiswill*not*overwriteanexistingSSHkey.group#Optionallysetstheuser'sprimarygroup(takesagroupname).groups#Putstheuserinthiscomma-delimitedlistofgroups.Whensettotheemptystring('groups='),theuserisremovedfromallgroupsexcehome#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.Seetheuserexampleinthegithubexamplesdirectoryforwhatthislookslikremove#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.[root@node1ansible]#

示例:

创建一个用户testuser uid=1000

[root@node1ansible]#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":1000192.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}

删除此用户 连同用户家目录:

[root@node1ansible]#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模块

[root@node1ansible]#ansible-doc-sgroupless436Copyright(C)1984-2009MarkNudelmanlesscomeswithNOWARRANTY,totheextentpermittedbylaw.Forinformationaboutthetermsofredistribution,seethefilenamedREADMEinthelessdistribution.Homepage:http://www.greenwoodsoftware.com/less-name:Addorremovegroupsaction:groupgid#Optional`GID'tosetforthegroup.name=#Nameofthegrouptomanage.state#Whetherthegroupshouldbepresentornotontheremotehost.system#If`yes',indicatesthatthegroupcreatedisasystemgroup.[root@node1ansible]#

示例:

创建一个用户组

[root@node1ansible]#ansibletestservers-mgroup-a'name=testgroupgid=1100state=present'192.168.100.131|success {"changed":true,"gid":1100,"name":"testgroup","state":"present","system":false192.168.100.132|success {"changed":true,"gid":1100,"name":"testgroup","state":"present","system":false}

综合示例:

在主机上创建一个testgroup组,然后创建一个testuser,并将其加入testgroup组

[root@node1ansible]#ansibletestservers-mgroup-a'name=testgroupgid=1100state=present'[root@node1ansible]#ansibletestservers-muser-a'name=testuseruid=1000shell=/bin/bashhome=/opt/testusergroup=testgroupstate=present'[root@node1ansible]#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 模块

[root@node1ansible]#ansible-doc-scopyless436Copyright(C)1984-2009MarkNudelmanlesscomeswithNOWARRANTY,totheextentpermittedbylaw.Forinformationaboutthetermsofredistribution,seethefilenamedREADMEinthelessdistribution.Homepage:http://www.greenwoodsoftware.com/less-name:Copiesfilestoremotelocations.action:copybackup#Createabackupfileincludingthetimestampinformationsoyoucangettheoriginalfilebackifyousomehowclobbereditincorrectly.content#Whenusedinsteadof'src',setsthecontentsofafiledirectlytothespecifiedvalue.dest=#Remoteabsolutepathwherethefileshouldbecopiedto.Ifsrcisadirectory,thismustbeadirectorytoo.directory_mode#Whendoingarecursivecopysetthemodeforthedirectories.Ifthisisnotsetwewillusethesystemdefaults.Themodeisonlysetofollow#Thisflagindicatesthatfilesystemlinks,iftheyexist,shouldbefollowed.force#thedefaultis`yes',whichwillreplacetheremotefilewhencontentsaredifferentthanthesource.If`no',thefilewillonlybetrgroup#nameofthegroupthatshouldownthefile/directory,aswouldbefedto`chown'mode#modethefileordirectoryshouldbe,suchas0644aswouldbefedto`chmod'.Asofversion1.8,themodemaybespecifiedasasymboliowner#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'portionofsrc#Localpathtoafiletocopytotheremoteserver;canbeabsoluteorrelative.Ifpathisadirectory,itiscopiedrecursively.Inthivalidate#Thevalidationcommandtorunbeforecopyingintoplace.Thepathtothefiletovalidateispassedinvia'%s'whichmustbepresenta[root@node1ansible]#

Ps:src 可以为绝对或相对路径 dest 必须为绝对路径

示例:将文件copy到测试主机:

[root@node1ansible]#ansibletestservers-mcopy-a'src=/root/install.logdest=/tmp/install.logowner=testusergroup=testgroup'192.168.100.131|success {"changed":true,"checksum":"7b3626c84bb02d12472c03d2ece878fdc4756c94","dest":"/tmp/install.log","gid":1100,"group":"testgroup","md5sum":"c7d8a01a077940859e773b7770d2e07e","mode":"0644","owner":"testuser","size":9458,"src":"/root/.ansible/tmp/ansible-tmp-1456387213.94-229503410500766/source","state":"file","uid":1000192.168.100.132|success {"changed":true,"checksum":"7b3626c84bb02d12472c03d2ece878fdc4756c94","dest":"/tmp/install.log","gid":1100,"group":"testgroup","md5sum":"c7d8a01a077940859e773b7770d2e07e","mode":"0644","owner":"testuser","size":9458,"src":"/root/.ansible/tmp/ansible-tmp-1456387213.94-186055595812050/source","state":"file","uid":1000}

示例:copy 前先备份

[root@node1ansible]#echo"test" /root/install.log[root@node1ansible]#ansibletestservers-mcopy-a'src=/root/install.logdest=/tmp/install.logowner=testusergroup=testgroupbackup=yes'192.168.100.132|success {"backup_file":"/tmp/install.log.2016-02-25@16:01:26~","changed":true,"checksum":"b5da7af32ad02eb98f77395b28f281a965b4c1f5","dest":"/tmp/install.log","gid":1100,"group":"testgroup","md5sum":"d39956add30a18019cb5ad2381a0cd43","mode":"0644","owner":"testuser","size":9464,"src":"/root/.ansible/tmp/ansible-tmp-1456387285.87-128685659798967/source","state":"file","uid":1000192.168.100.131|success {"backup_file":"/tmp/install.log.2016-02-25@16:01:26~","changed":true,"checksum":"b5da7af32ad02eb98f77395b28f281a965b4c1f5","dest":"/tmp/install.log","gid":1100,"group":"testgroup","md5sum":"d39956add30a18019cb5ad2381a0cd43","mode":"0644","owner":"testuser","size":9464,"src":"/root/.ansible/tmp/ansible-tmp-1456387285.86-134452201968647/source","state":"file","uid":1000[root@node1ansible]#ansibletestservers-mraw-a'ls-lrth/tmp/install*'192.168.100.131|success|rc=0 -rw-r--r--1rootroot9.3K22516:00/tmp/install.log.2016-02-25@16:01:26~-rw-r--r--1testusertestgroup9.3K22516:01/tmp/install.log192.168.100.132|success|rc=0 -rw-r--r--1rootroot9.3K22516:00/tmp/install.log.2016-02-25@16:01:26~-rw-r--r--1testusertestgroup9.3K22516:01/tmp/install.log

示例:将目录copy过去

[root@node1ansible]#treetestdirtestdir├──a│├──e││└──ansible.cfg│├──f│└──g├──b│├──e│├──f│└──g└──c├──ansible.cfg├──e├──f└──g[root@node1ansible]#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"[root@node1ansible]#ansibletestservers-mcommand-a'tree/tmp/testdir'192.168.100.131|success|rc=0 /tmp/testdir|--a|`--e|`--ansible.cfg|--b|`--e|`--hosts`--c`--ansible.cfg5directories,3files192.168.100.132|success|rc=0 /tmp/testdir|--a|`--e|`--ansible.cfg|--b|`--e|`--hosts`--c`--ansible.cfg5directories,3files

发现有文件的目录copy成功,空的目录没有copy过去

怠惰是贫穷的制造厂。

ansible模块cron、copy、user、group推荐

相关文章:

你感兴趣的文章:

标签云: