一個數(shù)據(jù)中心有可能存在好多類型的服務(wù)器库快。比如WEB類型、DB類型钥顽、開發(fā)人員使用的開發(fā)類型义屏、QA使用的測試類型等等。如果每個類型的服務(wù)器的初始化行為都不一致蜂大,那要在一個PlayBook中將這些動作完成闽铐,這個PlayBook將變得臃腫、龐大奶浦,且難以后續(xù)維護(hù)和更新兄墅。如果能夠針對每個類型的服務(wù)器單獨編寫PlayBook,最后通過某種方式整合這PlayBook澳叉, 在管理方式上就又會變得簡單隙咸。Ansible中提供了類似的概念,也就是Role成洗。它允許管理員將他們復(fù)雜的PlayBook分解成一個個小的邏輯單元, 以便于維護(hù)和管理五督。
Roles 結(jié)構(gòu)
ROLE 是什么呢
從表面上看,它就是一個目錄瓶殃。目錄的名字也就是role的名字充包。進(jìn)到這個role名字的目錄里,會發(fā)現(xiàn)好多子目錄遥椿。
tasks: 存放 task 任務(wù)
handlers: 存放 handlers 任務(wù)
files: 存放 task 中引用的文件
templages: 存放 task 中引用的模板
meta: 存在 role 的依賴role(這個role 執(zhí)行前基矮,要先執(zhí)行那個role)
vars: 存放 role 的變量
defaults: 存在 role 的默認(rèn)變量
那么一個真正的Role 的目錄結(jié)構(gòu)應(yīng)該如下
user.example/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
role 的名字叫做 user.example。
其中 tasks 修壕、handlers 愈捅、meta遏考、 vars慈鸠、defaults 目錄的入口文件必須是main.yml,不能別配置為其他。
制作一個Role
最終優(yōu)化的PlayBook
---
- name: task control playbook example
hosts: web_servers
wars:
createuser:
- tomcat
- www
- mysql
tasks:
- name: create user
user: name={{ item }} state=present
with_items: "{{ createuser }}"
- name: yum nginx webserver
yum: name=nginx state=present
- name: update nginx main config
copy: src=nginx.conf dest=/etc/nginx/
tags: update
notify: reload nginx server
- name: add virtualhost config
copy: src=www.biudefor.com.conf dest=/etc/nginx/conf.d/
tags: update
notify: reload nginx server
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
tags: update
- name: check nginx running
stat: path=/var/lock/subsys/nginx
register: nginxrunning
tags: update
- name: print nginx syntax
debug: var=nginxsyntax
- name: start nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == false
handlers:
- name: reload nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == true
分解這個PlayBook青团,命名role 的名字為 nginx
nginx/
│
├── files
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
files 文件夾存放文件
存放 www.biudefor.com.conf 配置文件
handlers 文件夾中的main.yml 文件
---
- name: reload nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == true
tasks 文件夾中的 main.yml 文件
- name: create user
user: name={{ item }} state=present
with_items: "{{ createuser }}"
- name: yum nginx webserver
yum: name=nginx state=present
- name: update nginx main config
copy: src=nginx.conf dest=/etc/nginx/
tags: update
notify: reload nginx server
- name: add virtualhost config
copy: src=www.biudefor.com.conf dest=/etc/nginx/conf.d/
tags: update
notify: reload nginx server
- name: check nginx syntax
shell: /usr/sbin/nginx -t
register: nginxsyntax
tags: update
- name: check nginx running
stat: path=/var/lock/subsys/nginx
register: nginxrunning
tags: update
- name: print nginx syntax
debug: var=nginxsyntax
- name: start nginx server
service: name=nginx state=started
when: nginxsyntax.rc == 0 and nginxrunning.stat.exists == false
templates 文件夾存放模板
vars 文件夾中的 main.yml 文件
---
createuser:
- tomcat
- www
- mysql
經(jīng)過以上對PlayBook 的拆分譬巫,就形成了一個nginx 的 ROLE。
回到本章開始的問題督笆,當(dāng)一個數(shù)據(jù)中心存在多種類型的服務(wù)器時芦昔,我們可以針對每個類型去單獨寫一個ROLE,這些ROLE 有可能分給不同的人去開發(fā)娃肿,這樣不但使開發(fā)的邏輯變得簡單咕缎,且開發(fā)效率也隨著人員的增加而提升。
如何在PlayBook中使用 Role
Role 本身不能被直接執(zhí)行料扰,還是需要借助PlayBook進(jìn)行間接的調(diào)用凭豪。
- name: a playbook used role
hosts: all
roles:
- nginx
# ansible-playbook nginx.yml
如何使用 Galaxy
Ansible的galaxy 工具,類似程序員使用的github晒杈。運(yùn)維人員可以將自己編寫的Role通過galaxy這個平臺進(jìn)行分享嫂伞。同樣,我們也可以通過galaxy 這個平臺去獲取一些我們想要的role拯钻。官網(wǎng)為:https://galaxy.ansible.com
而ansible-galaxy 則是一個使用 galaxy 命令行的工具帖努。它使我們不用訪問galaxy 的網(wǎng)站而獲取到需要的內(nèi)容。
接下來我們將通過 ansible-galaxy 這個命令行去學(xué)習(xí)galaxy的使用粪般。
獲取幫助
# ansible-galaxy --help
Usage: ansible-galaxy [delete|import|info|init|install|list|login|remove|search|setup] [--help] [options] ...
Options:
-h, --help show this help message and exit
-v, --verbose verbose mode (-vvv for more, -vvvv to enable connection
debugging)
--version show program's version number and exit
獲取具體某個子指令的幫助
在 ansible-galaxy --help 中可以看到子指令
子指令包含: delete|import|info|init|install|list|login|remove|search|setup
ansible-galaxy delete|import|info|init|install|list|login|remove|search|setup --help
# ansible-galaxy install --help
Usage: ansible-galaxy install [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]
Options:
-f, --force Force overwriting an existing role
-h, --help show this help message and exit
-c, --ignore-certs Ignore SSL certificate validation errors.
-i, --ignore-errors Ignore errors and continue with the next specified
role.
-n, --no-deps Don't download roles listed as dependencies
-r ROLE_FILE, --role-file=ROLE_FILE
A file containing a list of roles to be imported
-p ROLES_PATH, --roles-path=ROLES_PATH
The path to the directory containing your roles. The
default is the roles_path configured in your
ansible.cfg file (/etc/ansible/roles if not
configured)
-s API_SERVER, --server=API_SERVER
The API server destination
-v, --verbose verbose mode (-vvv for more, -vvvv to enable
connection debugging)
--version show program's version number and exit
常用指令
// 在galaxy 上搜索共享的ROLE
# ansible-galaxy search
// 安裝 galaxy 上共享的 ROLE
# ansible-galaxy install
// 列舉已經(jīng)通過 ansible-galaxy 工具安裝的ROLE
# ansible-galaxy list
// 創(chuàng)建一個ROLE 的空目錄架構(gòu), 這樣我們在開發(fā)一個ROLE的時候拼余,就不需要手動創(chuàng)建目錄了。
# ansible-galaxy init --offline
Example
// 創(chuàng)建了名字為testrole的空ROLE目錄結(jié)構(gòu)亩歹,默認(rèn)在執(zhí)行命令的目錄生產(chǎn)姿搜。
# ansible-galaxy init --offline testrole
# tree testrole/
testrole/
├── defaults
│ └── main.yml
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml