標籤:ansible 項目 playbook
針對大型項目使用Roles進行編排,更便利
目錄結構編排
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/8C/36/wKiom1hlF-SjFR-QAAHrlrracRc046.png-wh_500x0-wm_3-wmp_4-s_1176155170.png" title="1.png" alt="wKiom1hlF-SjFR-QAAHrlrracRc046.png-wh_50" />
一鍵安裝httpd並提供服務
建立目錄結構
[[email protected] ansible]# mkdir -p roles/httpd/{tasks,files,vars}
[[email protected] ansible]# cd roles/httpd/tasks
[[email protected] roles]#touch httpd/{files/main.yml,tasks/{groupadd.yml,install.yml,main.yml,restart.yml,useradd.yml},vars/main.yml}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/8C/32/wKioL1hlGAPC6d7dAAAWn-l58cw101.png-wh_500x0-wm_3-wmp_4-s_2326024754.png" title="2.png" alt="wKioL1hlGAPC6d7dAAAWn-l58cw101.png-wh_50" />
目錄結構建立好了,接下來開始配置
[[email protected] ~]# cd /root/ansible/roles/httpd/tasks
首先配置main.yml將所有的配置全部包含進來
[[email protected] tasks]# cat main.yml
---
- include: groupadd.yml
- include: useradd.yml
- include: install.yml
- include: restart.yml
配置添加使用者組
[[email protected] tasks]# cat groupadd.yml
---
- name: add group httpd
group: name=web state=present
配置添加使用者
[[email protected] tasks]# cat useradd.yml
---
- name: add group httpd
user: name=httpd groups=web system=yes state=present
配置安裝httpd
[[email protected] tasks]# cat install.yml
---
- name: Install Httpd
yum: name=httpd state=present
配置restart httpd
[[email protected] tasks]# cat restart.yml
---
- name: Restart Httpd
service: name=httpd state=restarted
配置主yml檔案
[[email protected] roles]# pwd
/root/ansible/roles
[[email protected] roles]# cat installhttpd.yml
---
- hosts: test
remote_user: root
roles:
- role: httpd
測試
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/8C/32/wKioL1hlGDGyYvrVAABJ4zKZS1Q115.png-wh_500x0-wm_3-wmp_4-s_3161819666.png" style="float:none;" title="3.png" alt="wKioL1hlGDGyYvrVAABJ4zKZS1Q115.png-wh_50" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8C/36/wKiom1hlGDLxN-uzAABcJ2oXM1U514.png-wh_500x0-wm_3-wmp_4-s_471239153.png" style="float:none;" title="4.png" alt="wKiom1hlGDLxN-uzAABcJ2oXM1U514.png-wh_50" />
提供配置網站檔案
[[email protected] files]# pwd
/root/ansible/roles/httpd/files
[[email protected] files]# cat index.html
<h1> test </h1>
建立copy模組的檔案
[[email protected] tasks]# cat filecp.yml
---
- name: Cp DocumentFile
copy: src=index.html dest=/var/www/html/index.html
將檔案包含至main
[[email protected] tasks]# cat main.yml
---
- include: groupadd.yml
- include: useradd.yml
- include: install.yml
- include: restart.yml
- include: filecp.yml
測試:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8C/32/wKioL1hlGEeyQRC5AABoCL_CzYc798.png-wh_500x0-wm_3-wmp_4-s_1494781897.png" title="5.png" alt="wKioL1hlGEeyQRC5AABoCL_CzYc798.png-wh_50" />
roles還可以引用
例:
[[email protected] roles]# pwd
/root/ansible/roles
[[email protected] roles]# cp httpd/ nginx -rf
[[email protected] roles]# cd nginx/tasks/
[[email protected] tasks]# rm -rf *
[[email protected] roles]# cd nginx/tasks/
[[email protected] tasks]# cat main.yml
---
- include: httpd/tasks/restart.yml
[[email protected] roles]# cp installhttpd.yml installnginx.yml
[[email protected] roles]# cat installnginx.yml
---
- hosts: test
remote_user: root
roles:
- role: nginx
測試:
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/8C/36/wKiom1hlGGGSv4m5AAAxcfFki-8847.png-wh_500x0-wm_3-wmp_4-s_3524004701.png" title="11.png" alt="wKiom1hlGGGSv4m5AAAxcfFki-8847.png-wh_50" />
拓展:
1、一次性執行多個roles
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/8C/36/wKiom1hlGHvxmBGnAAAVX8Llvik818.png-wh_500x0-wm_3-wmp_4-s_3986092922.png" title="22.png" alt="wKiom1hlGHvxmBGnAAAVX8Llvik818.png-wh_50" />
2、對roles進行打標籤,可單獨執行
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/8C/32/wKioL1hlGIizmAwoAAAYgSuYthY638.png-wh_500x0-wm_3-wmp_4-s_1703512455.png" title="33.png" alt="wKioL1hlGIizmAwoAAAYgSuYthY638.png-wh_50" />
測試
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/8C/32/wKioL1hlGJvjcnqmAAAyb2X2HYo500.png-wh_500x0-wm_3-wmp_4-s_3853290365.png" title="44.png" alt="wKioL1hlGJvjcnqmAAAyb2X2HYo500.png-wh_50" />
3、多重標籤
650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/8C/36/wKiom1hlGLKASxGmAAAznxhViSQ830.png-wh_500x0-wm_3-wmp_4-s_1410484183.png" title="555.png" alt="wKiom1hlGLKASxGmAAAznxhViSQ830.png-wh_50" />
執行web項目
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8C/32/wKioL1hlGMLTPfB5AABzXBheCLY179.png-wh_500x0-wm_3-wmp_4-s_4086162261.png" title="121.png" alt="wKioL1hlGMLTPfB5AABzXBheCLY179.png-wh_50" />
本文出自 “Char” 部落格,謝絕轉載!
Ansible的roles功能(五)