ansible yml语法
playbook使用:ansible-playbook test.yaml
????playbook是由一個或多個“play”組成的列表。play的主要功能在于將事先歸并為一組的主機裝扮成事先通過ansible中的task定義好的角色。從根本上來講,所謂task無非是調用ansible的一個module。將多個play組織在一個playbook中,即可以讓它們聯同起來按事先編排的機制同唱一臺大戲
#這個是你選擇的主機 -?hosts:?webservers #這個是變量vars:http_port:?80max_clients:?200 #遠端的執行權限remote_user:?roottasks: #利用yum模塊來操作-?name:?ensure?apache?is?at?the?latest?versionyum:?pkg=httpd?state=latest-?name:?write?the?apache?config?filetemplate:?src=/srv/httpd.j2?dest=/etc/httpd.conf #觸發重啟服務器notify:-?restart?apache-?name:?ensure?apache?is?runningservice:?name=httpd?state=started #這里的restart?apache?和上面的觸發是配對的。這就是handlers的作用。相當于taghandlers:-?name:?restart?apacheservice:?name=httpd?state=restarted1、HOSTS和Users
????playbook中的每一個play的目的都是為了讓某個或某些主機以某個指定的用戶身份執行任務。
????hosts用于指定要執行指定任務的主機,其可以是一個或多個由冒號分隔主機組;
????remote_user則用于指定遠程主機上的執行任務的用戶。如上面示例中的
-hosts:?webnodesremote_user:?root????不過,remote_user也可用于各task中。也可以通過指定其通過sudo的方式在遠程主機上執行任務,其可用于play全局或某任務;此外,甚至可以在sudo時使用sudo_user指定sudo時切換的用戶。
-?hosts:?webnodesremote_user:?roottasks:-?name:?test?connectionping:remote_user:?rootsudo:?yes2、任務列表和cation
????play的主體部分是task list。task list中的各任務按次序逐個在hosts中指定的所有主機上執行,即在所有主機上完成第一個任務后再開始第二個。在運行自下而下某playbook時,如果中途發生錯誤,所有已執行任務都將回滾,因此,在更正playbook后重新執行一次即可。
????task的目的是使用指定的參數執行模塊,而在模塊參數中可以使用變量。模塊執行是冪等的,這意味著多次執行是安全的,因為其結果均一致。
????每個task都應該有其name,用于playbook的執行結果輸出,建議其內容盡可能清晰地描述任務執行步驟。如果未提供name,則action的結果將用于輸出。
????定義task的可以使用“action: module options”或“module: options”的格式,推薦使用后者以實現向后兼容。如果action一行的內容過多,也中使用在行首使用幾個空白字符進行換行。
tasks:-?name:?make?sure?apache?is?runningservice:?name=httpd?state=running????在眾多模塊中,只有command和shell模塊僅需要給定一個列表而無需使用“key=value”格式,例如:
tasks:-?name:?disable?selinuxcommand:?/sbin/setenforce?0????如果命令或腳本的退出碼不為零,可以使用如下方式替代:
tasks:-?name:?run?this?command?and?ignore?the?resultshell:?/usr/bin/somecommand?||?/bin/true????或者使用ignore_errors來忽略錯誤信息:
tasks:-?name:?run?this?command?and?ignore?the?resultshell:?/usr/bin/somecommandignore_errors:?True3、handlers
????用于當關注的資源發生變化時采取一定的操作。
????“notify”這個action可用于在每個play的最后被觸發,這樣可以避免多次有改變發生時每次都執行指定的操作,取而代之,僅在所有的變化發生完成后一次性地執行指定操作。在notify中列出的操作稱為handler,也即notify中調用handler中定義的操作。
-?name:?template?configuration?filetemplate:?src=template.j2?dest=/etc/foo.confnotify:-?restart?memcached-?restart?apache????handler是task列表,這些task與前述的task并沒有本質上的不同。
handlers:-?name:?restart?memcachedservice:??name=memcached?state=restarted-?name:?restart?apacheservice:?name=apache?state=restarted例子
heartbeat.yaml-?hosts:?hbhostsremote_user:?roottasks:-?name:?ensure?heartbeat?latest?versionyum:?name=heartbeat?state=present-?name:?authkeys?configure?filecopy:?src=/root/hb_conf/authkeys?dest=/etc/ha.d/authkeys-?name:?authkeys?mode?600file:?path=/etc/ha.d/authkeys?mode=600notify:-?restart?heartbeat-?name:?ha.cf?configure?filecopy:?src=/root/hb_conf/ha.cf?dest=/etc/ha.d/ha.cfnotify:-?restart?heartbeathandlers:-?name:?restart?heartbeatservice:?name=heartbeat?state=restarted參考博文:
http://nmshuishui.blog.51cto.com/1850554/1573941
轉載于:https://blog.51cto.com/12899802/1948420
總結
以上是生活随笔為你收集整理的ansible yml语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于Unity中物理检测的准备
- 下一篇: MySQL中优化sql语句查询常用的30