ansible概述
- 應用代碼自動化部署
- 系統(tǒng)管理配置自動化
- 支持持續(xù)交付自動化
- 支持云编饺、大數(shù)據(jù)(openstack、aws鹤啡、cloudstack梗顺、vmware)環(huán)境
- 批量任務執(zhí)行可以寫成腳本,不必分發(fā)到遠程就可以執(zhí)行
- 支持sudo
Paste_Image.png
Paste_Image.png
Playbook簡介
-hosts: //被管理的主機組
user:root //遠程執(zhí)行操作的用戶
vars: //變量
- motd_warning:'variable'
tasks: //任務
-name:setup a MOTD
copy:dest=/etc/motdcontent = "{{ motd_warning }}"
notify:say something
handlers: //由task的notify觸發(fā)的處理動作
-name:say something
- Target section 定義將要執(zhí)行playbook的遠程主機組
hosts:定義遠程的主機組
user:執(zhí)行該任務組的用戶
remote_user:與user相同
sudo:如果設(shè)置為yes蝠嘉,執(zhí)行該任務組的用戶在執(zhí)行任務的時候最疆,獲取root權(quán)限
sudo_user:如果你設(shè)置user為tom,sudo為yes蚤告,sudo_user為jerry努酸,則tom用戶則會獲取jerry用戶的權(quán)限
connection:通過什么方式連接到遠程主機,默認為ssh
gather_facts:除非你明確說明不需要在遠程主機上執(zhí)行setup模塊杜恰,否則默認會自動執(zhí)行获诈。如果你確實不需要setup模塊所傳遞過來的變量,你可以啟用該選項
- Variable section
定義playbook運行時需要使用的變量
vars 直接在playbook中定義變量
vars_files: #在文件中定義變量心褐,var_files指定包含變量的文件位置
- variables
vars_prompt: #用于實現(xiàn)用戶輸入作為變量的值
-name:variable_name 自定義變量名舔涎,可以在文件中使用
prompt: please input xxx 提示信息
private:yes 交互輸入不顯示
- Task section
定義將要在遠程主機上執(zhí)行的任務列表
tasks:
#第一種方法
- name: install apache
action: yum name=httpd state=installed
#第二種方法
- name: configure apache
copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf
#第三種方法
- name: restart apache
service:
name: httpd
state: restarted
- Handler section
定義task執(zhí)行完成以后需要調(diào)用的任務
tasks:
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handlers:
- name: restart memcached
service: name= memcached state=restarted
- name: restart apache
service: name=httpd state=restarted
playbook的roles和include
完成復雜任務時,通常需要把多個playbook進行組合逗爹,少量用include即可完成亡嫌,如果playbook較多嚎于,引入roles對playbook進行有效組織十分必要
include包含
include.yml文件內(nèi)容
- include xxx1.yml
- include xxx2.yml
- hosts:mfs_node
vars_file:
- vars.yml
- vars1.yml
tasks:
- include: task.yml
handlers:
- include: handler.yml
task.yml文件內(nèi)容
- name :xxx
shell:xxx
notify:touch a file
handler.yml文件內(nèi)容
- name:touch a file
shell: xxx
roles目錄結(jié)構(gòu)
group_vars/ //可定義整組角色都可用的變量文件,也可以單獨定義某個角色變量文件挟冠,文件名對應hosts內(nèi)的角色名稱
hosts
main.yml //入口文件
roles/
role1/
vars/
tasks/
handlers/
role2/
role3/
...
tools/
main.yml文件內(nèi)容類似這樣
- hosts: xxx1
roles:
- role:
- hosts: xxx2
roles:
- role:
- role: