Ansible中的角色使用
生活随笔
收集整理的這篇文章主要介紹了
Ansible中的角色使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ansible roles
#ansible 角色簡介#
* Ansible roles 是為了層次化,結(jié)構(gòu)化的組織Playbook
* roles就是通過分別將變量、文件、任務(wù)、模塊及處理器放置于單獨的目錄中,并可以便捷地include它們
* roles一般用于基于主機(jī)構(gòu)建服務(wù)的場景中,在企業(yè)復(fù)雜業(yè)務(wù)場景中應(yīng)用的頻率很高
* 以特定的層級目錄結(jié)構(gòu)進(jìn)行組織的tasks、variables、handlers、templates、files等;相當(dāng)于函數(shù)的調(diào)用把各個功能切割成片段來執(zhí)行。
roles目錄結(jié)構(gòu)
| files | 存放copy或script等模塊調(diào)用的函數(shù) |
| tasks | 定義各種task,要有main.yml,其他文件include包含調(diào)用 |
| handlers | 定義各種handlers,要有main.yml,其他文件include包含調(diào)用 |
| vars | 定義variables,要有main.yml,其他文件include包含調(diào)用 |
| templates | 存儲由template模塊調(diào)用的模板文本 |
| meta | 定義當(dāng)前角色的特殊設(shè)定及其依賴關(guān)系,要有main.yml的文件 |
| defaults | 要有main.yml的文件,用于設(shè)定默認(rèn)變量 |
| tests | 用于測試角色 |
role存放的路徑在配置文件ansible.cfg中定義
roles_path = path/roles? (默認(rèn)目錄:/etc/ansible/roles)
創(chuàng)建目錄結(jié)構(gòu)
$ ansible-galaxy init apache $ ansible-galaxy list?
?
playbook中使用roles
playbook中使用roles: --- - hosts: server2roles:- role: role1- role: role2var1: value1 ##此處變量會覆蓋roles中的定義變量控制任務(wù)執(zhí)行順序
--- - hosts: server2roles:- role: role1 ##角色任務(wù)pre_tasks: ##角色執(zhí)行前執(zhí)行的play- tasks1tasks: ##普通任務(wù)- tasks2post_tasks: ##在角色和普通任務(wù)執(zhí)行完畢后執(zhí)行的play- tasks3handlers:ansible—galaxy命令工具
* Ansible Galaxy 是一個免費共享和下載 Ansible 角色的網(wǎng)站,可以幫助我們更好的定義和學(xué)習(xí)roles。 * ansible-galaxy命令默認(rèn)與https://galaxy.ansible.com網(wǎng)站API通信,可以查找、下載各種社區(qū)開發(fā)的 Ansible 角色 * ansible-galaxy在 Ansible 1.4.2 就已經(jīng)被包含了 * 在galaxy.ansible.com網(wǎng)站查詢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練習(xí):
使用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?
?
總結(jié)
以上是生活随笔為你收集整理的Ansible中的角色使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Anisble中的任务执行控制
- 下一篇: LVS+Keepalive 实现负载均衡