Ansible中的角色使用
生活随笔
收集整理的這篇文章主要介紹了
Ansible中的角色使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ansible roles
#ansible 角色簡介#
* Ansible roles 是為了層次化,結構化的組織Playbook
* roles就是通過分別將變量、文件、任務、模塊及處理器放置于單獨的目錄中,并可以便捷地include它們
* roles一般用于基于主機構建服務的場景中,在企業復雜業務場景中應用的頻率很高
* 以特定的層級目錄結構進行組織的tasks、variables、handlers、templates、files等;相當于函數的調用把各個功能切割成片段來執行。
roles目錄結構
| files | 存放copy或script等模塊調用的函數 |
| tasks | 定義各種task,要有main.yml,其他文件include包含調用 |
| handlers | 定義各種handlers,要有main.yml,其他文件include包含調用 |
| vars | 定義variables,要有main.yml,其他文件include包含調用 |
| templates | 存儲由template模塊調用的模板文本 |
| meta | 定義當前角色的特殊設定及其依賴關系,要有main.yml的文件 |
| defaults | 要有main.yml的文件,用于設定默認變量 |
| tests | 用于測試角色 |
role存放的路徑在配置文件ansible.cfg中定義
roles_path = path/roles? (默認目錄:/etc/ansible/roles)
創建目錄結構
$ ansible-galaxy init apache $ ansible-galaxy list?
?
playbook中使用roles
playbook中使用roles: --- - hosts: server2roles:- role: role1- role: role2var1: value1 ##此處變量會覆蓋roles中的定義變量控制任務執行順序
--- - hosts: server2roles:- role: role1 ##角色任務pre_tasks: ##角色執行前執行的play- tasks1tasks: ##普通任務- tasks2post_tasks: ##在角色和普通任務執行完畢后執行的play- tasks3handlers:ansible—galaxy命令工具
* Ansible Galaxy 是一個免費共享和下載 Ansible 角色的網站,可以幫助我們更好的定義和學習roles。 * ansible-galaxy命令默認與https://galaxy.ansible.com網站API通信,可以查找、下載各種社區開發的 Ansible 角色 * ansible-galaxy在 Ansible 1.4.2 就已經被包含了 * 在galaxy.ansible.com網站查詢roles安裝選擇的角色
#install https://galaxy.ansible.com roles $ansible-galaxy install geerlingguy.nginxinstall local roles
$ vim install_apache_role.yml --- - src: file:///mnt/apache.tar.gzname: apache$ ansible-galaxy install -r install_apache_role.yml練習:
使用roles配置DNSDHCP(DDNS) 花生殼
vim ~/ddns.yml --- - name: ddnshosts: allroles:- role: ~/ansible/dns ...tasks
vim ~/ansible/dns/tasks/main.yml--- - name: install dhcp-server and binddnf:name: "{{item}}"state: presentloop:"{{SOFTWARE}}"notify: set firewalld- name: create dhcpd.conf and named.conftemplate:src: "{{item.src}}"dest: "{{item.dest}}"group: "{{item.group}}"loop:"{{FILES}}"notify: restart server?vars
--- SOFTWARE:- bind- dhcp-serverFILES:- src: named.conf.j2dest: /etc/named.confgroup: named- src: westos.key.j2dest: /etc/westos.keygroup: named- src: westos.com.zone.j2dest: /var/named/westos.com.zonegroup: named- src: named.rfc1912.zones.j2dest: /etc/name.rfc1912.zones.zonesgroup: named- src: dhcpd.conf.j2dest: /etc/dhcp/dhcpd.confgroup: root?templates
handlers
--- - name: restart serverservice:name: "{{item}}"state: restartedenabled: yesloop:- named- dhcpd - name: set firewalldfirewalld:service: "{{item}}"permanent: yesstate: enabledimmediate: yesloop:- dns- dhcp?
?
總結
以上是生活随笔為你收集整理的Ansible中的角色使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Anisble中的任务执行控制
- 下一篇: LVS+Keepalive 实现负载均衡