# Accessin$ !acs o! ano"er node% OO "osarsF;demo2.eEamle.com;GF;ansibleos!amily;G PP ############################################################################### # =un arbirary command on all "oss as sudo% & ansible all -s -a Sca arlo$messa$esS # =un Ad 'oc module wi" ar$umens% & ansible "os aern -m module -a ;ar$umen1 ar$umen2; F-i inenoryG # =un ad "oc command "a $eneraes one line inu !or eac" oeraion% & ansible my"oss -m command -a usrbin"osname -o # Ad "oc 6Eamle% yum module c"ecks i! "d insalled on demo"os% & ansible demo"os -u deos -b -m yum -a ;name"d saeresen; # Ad "oc 6Eamle% 7ind aailable sace on disks con!i$ured on demo"os% & ansible demo"os -a Sd! -"S # Ad "oc 6Eamle% 7ind aailable !ree memory on demo"os% & ansible demo"os -a S!ree -mS ############################################################################### ## 3laybook/s basics% --<-indicaes AML, oional documen marker - "oss% all <-"os or "os $rou a$ains we will run "e ask /eEamle - "oss% webserer remoeuser% lmaly <-as w"a user will Ansible lo$ in o "e mac"ine/s asks% <-lis o! acions - name% B"aeer you wan o name i <-name o! "e !irs ask yum% name% "d sae% resen <-same as sin$le line ;yum% name"d saeresen become*rue; become% *rue <-ask will be eEecued wi" sudo ... <-oional documen marker indicain$ end o! AML oes% )ace c"aracers used !or indenaion 3ie /T reserers line reurns wi"in srin$ Arrow /> coners line reurns o saces, remoes leadin$ sace in lines ndenaion rules% 6lemens a same leel in "ierarc"y mus "ae same indenaion +"ild elemens mus be indened !ur"er "an arens o rules abou eEac number o! saces o use (ional% nser blank lines !or readabiliy se AML Lin "%yamllin.com o c"eck "e synaE correcness se ;ansible-laybook --synaE-c"eck ; se ;ansible-laybook -+ ; !or dry run /w"a c"an$es would occur i! laybook is eEecued se ;alwaysrun% rue; o eEecue only some asks in c"eck mode /or ;alwaysrun% !alse; !or "e oosie se ;ansible-laybook --se ; o rom eac" ask /yyesnnoceEi and eEecue remainin$ wi"ou askin$ se ;ansible-laybook --sar-a-askSsar "d sericeS o sar eEecuion !rom $ien ask
7or comleE laybooks, use ;include; o include searae !iles in main laybook /include% asksen.yml ############################################################################### ## Uariables% --- # sually commen !ield describin$ w"a i does - "oss% all remoeuser% lmaly asks% - name% )e ariable ;name; se!ac% name% *es mac"ine - name% 3rin ariable ;name; debu$% ms$% ;OO name PP; a n ars block% ars% se!ac% ;*es mac"ine; b 3assed as ar$umens% includears% arseEraar$s.yml # ("er alernaie ways% /1 ass ariable/s in "e +L% & ansible-laybook -i <'()*>, <laybook>.yaml
-e ;namees01;
/2 ass ariable/s o an inenory !ile% a FwebsererG <- all laybooks runnin$ on webserers will be able o re!er o "e domain name ariable ws01.lmaly.io domainnameeEamle1.lmaly.io ws02.lmaly.io domainnameeEamle2.lmaly.io b Fwebserer%arsG <- 'os ariables will oerride $rou ariables in case $e same ariable is used in bo" "senabled*rue
oes% wi"!ile - *akes lis o! !ile namesV ;iem; se o conen o! eac" !ile in seYuence wi"!ile$lob - *akes !ile name $lobbin$ aernV ;iem; se o eac" !ile in direcory "a mac"es aernV in seYuence, nonrecursiely wi"seYuence - eneraes seYuence o! iems in increasin$ numerical orderV +an ake ;sar; and ;end; ar$umens - )uors decimal, ocal, "eEadecimal ine$er alues wi"randomc"oices - *akes lisV ;iem; se o one lis iem a random oe - +ondiionals% 6Yual Less "an reaer "an Less "an or eYual reaer "an or eYual o eYual Uariable eEiss Uariable does no eEis Uariable se o 1, *rue, or yes Uariable se o 0, 7alse, or no Ualue resen in ariable or array A? inenory"osname in $rousF;sa$in$;G (= ansibledisribuion S7edoraS /J ;w"en; saemen eEamle
SOO maEmemory PP J12S SOO minmemory PP < 128S SOO minmemory PP > 2JIS SOO minmemory PP < 2JIS SOO minmemory PP > J12S SOO minmemory PP J12S SOO minmemory PP is de!inedS SOO minmemory PP is no de!inedS SOO aailablememory PPS Sno OO aailablememory PPS SOO users PP in usersFSdbadminsSGS ansiblekernel 9.10.elK.E8II4 and ansibledisribuion S=ed'aS or
UA=A5L6AM6 T caialiQe PP; ouu T oRson PP ouu T oyaml PP ouu T oniceRson PP ouu T oniceyaml PP ouu T !romRson PP ouu T !romyaml PP !oresblockersTsli/;-; PP;
& sudo c demo.yml arlibawEroRecsdemoroRec & sudo c"own -= awE arlibawEroRecsdemoroRec ############################################################################### ## +L% /1 =un a command somew"ere else usin$ Ansible & ansible sa$e% ansible <"os-aern> FoionsG (ions% -a M(?L6A=), --ar$sM(?L6A=) module ar$umens --ask-aul-ass ask !or aul assword -5 )6+(?), --back$round)6+(?) run async"ronously, !ailin$ a!er ` seconds /de!aulA -+, --c"eck don; make any c"an$esV insead, ry o redic some o! "e c"an$es "a may occur -?, --di!! w"en c"an$in$ /small !iles and emlaes, s"ow "e di!!erences in "ose !ilesV works $rea wi" --c"eck -e 6`*=AUA=), --eEra-ars6`*=AUA=) se addiional ariables as keyalue or AMLW)( -! 7(=Z), --!orks7(=Z) seci!y number o! arallel rocesses o use /de!aulJ -", --"el s"ow "is "el messa$e and eEi -i U6*(=, --inenory-!ileU6*(= seci!y inenory "os a" /de!aulecansible"oss or comma searaed "os lis. -l )5)6*, --limi)5)6* !ur"er limi seleced "oss o an addiional aern --lis-"oss ouus a lis o! mac"in$ "ossV does no eEecue any"in$ else -m M(?L6AM6, --module-nameM(?L6AM6 module name o eEecue /de!aulcommand -M M(?L63A*', --module-a"M(?L63A*' seci!y a"/s o module library /de!aulone --new-aul-assword-!ile6BUAL*3A))B(=?7L6 new aul assword !ile !or rekey -o, --one-line condense ouu --ouu(*3*7L6 ouu !ile name !or encry or decryV use - !or sdou -3 3(LL*6=UAL, --oll3(LL*6=UAL se "e oll ineral i! usin$ -5 /de!aul1J --synaE-c"eck er!orm a synaE c"eck on "e laybook, bu do no eEecue i - *=66, --ree*=66 lo$ ouu o "is direcory --aul-assword-!ileUAL*3A))B(=?7L6 aul assword !ile -, --erbose erbose mode /- !or more, - o enable connecion debu$$in$ --ersion s"ow ro$ram;s ersion number and eEi +onnecion (ions% conrol as w"om and "ow o connec o "oss -k, --ask-ass ask !or connecion assword --riae-key3=UA*6Z67L6, --key-!ile3=UA*6Z67L6 use "is !ile o au"enicae "e connecion
-u =6M(*6)6=, --user=6M(*6)6= connec as "is user /de!aulone -c +(6+*(, --connecion+(6+*( connecion ye o use /de!aulsmar -* *M6(*, --imeou*M6(* oerride "e connecion imeou in seconds /de!aul10 --ss"-common-ar$s))'+(MM(A=) seci!y common ar$umens o ass o s!scss" /e.$. 3roEy+ommand --s!-eEra-ar$s)7*36`*=AA=) seci!y eEra ar$umens o ass o s! only /e.$. -!, -l --sc-eEra-ar$s)+36`*=AA=) seci!y eEra ar$umens o ass o sc only /e.$. -l --ss"-eEra-ar$s))'6`*=AA=) seci!y eEra ar$umens o ass o ss" only /e.$. -= 3riile$e 6scalaion (ions% conrol "ow and w"ic" user you become as on ar$e "oss
-s, --sudo
run oeraions wi" sudo /noasswd /derecaed, use become - )?()6=, --sudo-user)?()6= desired sudo user /de!aulroo /derecaed, use become -), --su run oeraions wi" su /derecaed, use become -= ))6=, --su-user))6= run oeraions wi" su as "is user /de!aulroo /derecaed, use become -b, --become run oeraions wi" become /does no imly assword romin$ --become-me"od56+(M6M6*'(? riile$e escalaion me"od o use /de!aulsudo, alid c"oices% F sudo T su T brun T !eEec T runas T doas T dQdo G --become-user56+(M6)6= run oeraions as "is user /de!aulroo --ask-sudo-ass ask !or sudo assword /derecaed, use become --ask-su-ass ask !or su assword /derecaed, use become -Z, --ask-become-ass ask !or riile$e escalaion assword
############################################################################### /2 =un Ansible laybook & ansible-laybook sa$e% ansible-laybook laybook.yml (ions% --ask-aul-ass ask !or aul assword -+, --c"eck don; make any c"an$esV insead, ry o redic some o! "e c"an$es "a may occur -?, --di!! w"en c"an$in$ /small !iles and emlaes, s"ow "e di!!erences in "ose !ilesV works $rea wi" --c"eck -e 6`*=AUA=), --eEra-ars6`*=AUA=) se addiional ariables as keyalue or AMLW)( --!lus"-cac"e clear "e !ac cac"e --!orce-"andlers run "andlers een i! a ask !ails -! 7(=Z), --!orks7(=Z)
seci!y number o! arallel rocesses o use /de!aulJ -", --"el s"ow "is "el messa$e and eEi -i U6*(=, --inenory-!ileU6*(= seci!y inenory "os a" /de!aulecansible"oss or comma searaed "os lis. -l )5)6*, --limi)5)6* !ur"er limi seleced "oss o an addiional aern --lis-"oss ouus a lis o! mac"in$ "ossV does no eEecue any"in$ else --lis-a$s lis all aailable a$s --lis-asks lis all asks "a would be eEecued -M M(?L63A*', --module-a"M(?L63A*' seci!y a"/s o module library /de!aulone --new-aul-assword-!ile6BUAL*3A))B(=?7L6 new aul assword !ile !or rekey --ouu(*3*7L6 ouu !ile name !or encry or decryV use - !or sdou --ski-a$s)Z3*A) only run lays and asks w"ose a$s do no mac" "ese alues --sar-a-ask)*A=*A**A)Z sar "e laybook a "e ask mac"in$ "is name --se one-se-a-a-ime% con!irm eac" ask be!ore runnin$ --synaE-c"eck er!orm a synaE c"eck on "e laybook, bu do no eEecue i - *A), --a$s*A) only run lays and asks a$$ed wi" "ese alues --aul-assword-!ileUAL*3A))B(=?7L6 aul assword !ile -, --erbose erbose mode /- !or more, - o enable connecion debu$$in$ --ersion s"ow ro$ram;s ersion number and eEi +onnecion (ions% conrol as w"om and "ow o connec o "oss
-k, --ask-ass ask !or connecion assword --riae-key3=UA*6Z67L6, --key-!ile3=UA*6Z67L6 use "is !ile o au"enicae "e connecion -u =6M(*6)6=, --user=6M(*6)6= connec as "is user /de!aulone -c +(6+*(, --connecion+(6+*( connecion ye o use /de!aulsmar -* *M6(*, --imeou*M6(* oerride "e connecion imeou in seconds /de!aul10 --ss"-common-ar$s))'+(MM(A=) seci!y common ar$umens o ass o s!scss" /e.$. 3roEy+ommand --s!-eEra-ar$s)7*36`*=AA=) seci!y eEra ar$umens o ass o s! only /e.$. -!, -l --sc-eEra-ar$s)+36`*=AA=) seci!y eEra ar$umens o ass o sc only /e.$. -l --ss"-eEra-ar$s))'6`*=AA=) seci!y eEra ar$umens o ass o ss" only /e.$. -= 3riile$e 6scalaion (ions%
conrol "ow and w"ic" user you become as on ar$e "oss
-s, --sudo
run oeraions wi" sudo /noasswd /derecaed, use become - )?()6=, --sudo-user)?()6= desired sudo user /de!aulroo /derecaed, use become -), --su run oeraions wi" su /derecaed, use become -= ))6=, --su-user))6= run oeraions wi" su as "is user /de!aulroo /derecaed, use become -b, --become run oeraions wi" become /does no imly assword romin$ --become-me"od56+(M6M6*'(? riile$e escalaion me"od o use /de!aulsudo, alid c"oices% F sudo T su T brun T !eEec T runas T doas T dQdo G --become-user56+(M6)6= run oeraions as "is user /de!aulroo --ask-sudo-ass ask !or sudo assword /derecaed, use become --ask-su-ass ask !or su assword /derecaed, use become -Z, --ask-become-ass ask !or riile$e escalaion assword
############################################################################### /9 )e u a remoe coy o! ansible on eac" mana$ed node /clone Ansible con!i$uraion !iles !rom i reosiory & ansible-ull sa$e% ansible-ull - FoionsG (ions% --acce-"os-key adds "e "oskey !or "e reo url i! no already added --ask-aul-ass ask !or aul assword -+ +'6+Z(*, --c"eckou+'6+Z(* branc"a$commi o c"eckou. ?e!auls o be"aior o! reosiory module. -d ?6)*, --direcory?6)* direcory o c"eckou reosiory o -e 6`*=AUA=), --eEra-ars6`*=AUA=) se addiional ariables as keyalue or AMLW)( -!, --!orce run "e laybook een i! "e reosiory could no be udaed --!ull ?o a !ull clone, insead o! a s"allow one. -", --"el s"ow "is "el messa$e and eEi -i U6*(=, --inenory-!ileU6*(= seci!y inenory "os a" /de!aulecansible"oss or comma searaed "os lis. -l )5)6*, --limi)5)6* !ur"er limi seleced "oss o an addiional aern --lis-"oss ouus a lis o! mac"in$ "ossV does no eEecue any"in$ else -m M(?L6AM6, --module-nameM(?L6AM6 =eosiory module name, w"ic" ansible will use o c"eck ou "e reo. ?e!aul is $i. -M M(?L63A*', --module-a"M(?L63A*' seci!y a"/s o module library /de!aulone --new-aul-assword-!ile6BUAL*3A))B(=?7L6
-o, --only-i!-c"an$ed
new aul assword !ile !or rekey
only run "e laybook i! "e reosiory "as been udaed --ouu(*3*7L6 ouu !ile name !or encry or decryV use - !or sdou --ur$e ur$e c"eckou a!er laybook run --ski-a$s)Z3*A) only run lays and asks w"ose a$s do no mac" "ese alues -s )L663, --slee)L663 slee !or random ineral /beween 0 and n number o! seconds be!ore sarin$. *"is is a use!ul way o diserse $i reYuess - *A), --a$s*A) only run lays and asks a$$ed wi" "ese alues - =L, --url=L =L o! "e laybook reosiory --aul-assword-!ileUAL*3A))B(=?7L6 aul assword !ile -, --erbose erbose mode /- !or more, - o enable connecion debu$$in$ --eri!y-commi eri!y 3 si$naure o! c"ecked ou commi, i! i !ails abor runnin$ "e laybook. *"is needs "e corresondin$ U+) module o suor suc" an oeraion --ersion s"ow ro$ram;s ersion number and eEi
+onnecion (ions% conrol as w"om and "ow o connec o "oss
-k, --ask-ass ask !or connecion assword --riae-key3=UA*6Z67L6, --key-!ile3=UA*6Z67L6 use "is !ile o au"enicae "e connecion -u =6M(*6)6=, --user=6M(*6)6= connec as "is user /de!aulone -c +(6+*(, --connecion+(6+*( connecion ye o use /de!aulsmar -* *M6(*, --imeou*M6(* oerride "e connecion imeou in seconds /de!aul10 --ss"-common-ar$s))'+(MM(A=) seci!y common ar$umens o ass o s!scss" /e.$. 3roEy+ommand --s!-eEra-ar$s)7*36`*=AA=) seci!y eEra ar$umens o ass o s! only /e.$. -!, -l --sc-eEra-ar$s)+36`*=AA=) seci!y eEra ar$umens o ass o sc only /e.$. -l --ss"-eEra-ar$s))'6`*=AA=) seci!y eEra ar$umens o ass o ss" only /e.$. -= 3riile$e 6scalaion (ions% conrol "ow and w"ic" user you become as on ar$e "oss --ask-sudo-ass ask !or sudo assword /derecaed, use become --ask-su-ass ask !or su assword /derecaed, use become -Z, --ask-become-ass ask !or riile$e escalaion assword