--- - hosts: all user: root vars: - name: "dxx" - age: 15 tasks: - name: echo {{name}} shell: echo {{name}} notify: - say hello handlers: - name: say hello shell: echo "say hello"
可在playbook、單個任務中mysql
(1)vars: - name: dxx - age: 14 (2)vars_files: - variables (3)vars_prompt: - name: version prompt: please input version private: no
class.name
class[0].nameweb
- name: copy /aaa action: copy src=/aaa dest=/bbb/aaa - name: copy /aaa copy: src=/aaa dest=/bbb/aaa - name: copy /aaa copy: src: /aaa dest: /bbb/aaa
(1)當任務執行完畢後纔會通知redis
(2)不管通知多少次,最終只會通知一次(????具體是哪一次)sql
(3)沒有調用通知,handler也不會執行shell
work_name="{{ name |default(tom) }}"
正確姿式: with_items: - aa: 11 bb: 22 - aa: 55 bb: 66 with_items: - {aa: 11,bb: 22} - {aa: 55,bb: 66} 錯誤姿式 with_items: aa: 11 bb: 22
(1)做用tomcat
角色容許你將變量、文件、模板、handler、任務放到一個文件夾中,而後包含他們。函數
角色中能夠包含角色(在創建好一個有效的依賴關係後),也能夠傳遞變量給角色編碼
(2)可包含的文件夾,不使用能夠刪除spa
main.yml roles myslq files 存放role須要的文件 tasks 存放playbook的yml文件 templates 存放模板文件 handlers 存放調用任務文件(相似編碼中定義的外部函數) vars 存放role中的變量文件 meta defaults redis files tasks templates handlers vars meta defaults
(3)使用方式rest
--- - hosts: webserver roles: - { role: start_server } - { role: end_server, port: 80} - { role: restart_server, when: port is defined } - hosts: mysql roles: - { role: start_mysql } - hosts: redis tasks: - name: include_role tomcat include_role: name=tomcat port=8080
#when的經常使用語法 when: foo is defined when: foo is not defined when: result|failed when: result|success when: result|skipped vars: trip: true when: trip when: ansible_os_family == 'RedHat' when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian" #若是包含hi when: resut.stdout.find('hi') != -1
當知足某些條件時,部署狀態顯示changed。
當知足某些條件時,部署狀態顯示失敗。
注意:include_role任務中,使用when,不能包含變量(會報條件狀態異常)
解決方案:經過block來保住include_role,給block設置when條件。