簡(jiǎn)介
Playbook是由一個(gè)或多個(gè)“play”組成的列表兄猩,可以讓它們聯(lián)同起來按事先編排的機(jī)制執(zhí)行;所謂task無非是調(diào)用ansible的一個(gè)module庄新,而在模塊參數(shù)中可以使用變量偿短;模塊執(zhí)行是冪等的,這就意味著多次執(zhí)行是安全的凯旋,因?yàn)槠浣Y(jié)果均一致呀潭。
特點(diǎn)
- YAML的可讀性好
- YAML和腳本語(yǔ)言的交互性好
- YAML使用實(shí)現(xiàn)語(yǔ)言的數(shù)據(jù)類型
- YAML有一個(gè)一致的信息模型
- YAML易于實(shí)現(xiàn)
- YAML可以基于流來處理
- YAML表達(dá)能力強(qiáng)钉迷,擴(kuò)展性好
核心組件
- Hosts:執(zhí)行的遠(yuǎn)程主機(jī)列表
- Tasks:任務(wù),由模塊定義的操作的列表钠署;
- Varniables:內(nèi)置變量或自定義變量在playbook中調(diào)用
- Templates:模板糠聪,即使用了模板語(yǔ)法的文本文件;
- Handlers:和nogity結(jié)合使用谐鼎,為條件觸發(fā)操作舰蟆,滿足條件方才執(zhí)行,否則不執(zhí)行该面;
- Roles:角色夭苗;
官方實(shí)例:
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
name: httpd
state: latest
- name: write the apache config file
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service:
name: httpd
state: started
handlers:
- name: restart apache
service:
name: httpd
state: restarted
運(yùn)行:
-t Tag 指定運(yùn)行特定的任務(wù)
--skip-tags=SKIP_TAGS 跳過指定的標(biāo)簽
--start-at-task=START_AT 從哪個(gè)任務(wù)后執(zhí)行
檢查語(yǔ)法:--syntac-check
測(cè)試運(yùn)行:--check
handlers 和 notify結(jié)合使用觸發(fā)條件,讓playbook在滿足一定觸發(fā)條件時(shí)才去執(zhí)行某條task。
ERROR! Unexpected Exception, this is probably a bug: unhashable type: 'dict'
如何遇到此類問題隔缀,是因?yàn)閐ict不可哈希题造,所以notify在配置下,使用可哈希數(shù)據(jù)類型即可猾瘸。
變量
- 變量的來源:
- ansible setup facts遠(yuǎn)程主機(jī)的所有變量都可以用
- 自定義變量
- 優(yōu)先級(jí):
- 通過命令行指定變量界赔,優(yōu)先級(jí)最高
- 在/etc/ansible/hosts 定義變量,在主機(jī)組中的主機(jī)單獨(dú)定義
- 在/etc/ansible/hosts 定義變量牵触,針對(duì)主機(jī)組中的所有主機(jī)集中定義變量
- 在Playbook中定義變量(建議使用這種方法)
[WARNING]: Found variable using reserved name: port
如何遇到此類問題淮悼,是因?yàn)閜ort為保留的變量名,更改一下變量名即可揽思!
模版
- 文本文件袜腥,內(nèi)部嵌套有模板語(yǔ)言腳本(使用模板語(yǔ)言編寫)
- Jinja2 是由python編寫的。在我們打算使用基于文本的模板語(yǔ)言時(shí)钉汗,jinja2是很好的解決方案羹令。yaml是寫playbook,jinja2是寫配置文件模板的
- 功能:將模板的文件的變量值轉(zhuǎn)換成對(duì)應(yīng)的本地主機(jī)的確定值损痰。例如:ansible端寫一個(gè)內(nèi)建變量{{ansible_processor_vcpus }}福侈,當(dāng)這個(gè)文件被復(fù)制到對(duì)應(yīng)主機(jī)時(shí)會(huì)自動(dòng)生成對(duì)應(yīng)主機(jī) cpu的顆數(shù)的結(jié)果替換之。
- templates文件必須存放在templates目錄下
- yaml文件需要和templates目錄平級(jí)
- Jinja2語(yǔ)法
字面量:
字符串:使用單引號(hào)或雙引號(hào)卢未;
數(shù)字:整數(shù)肪凛、浮點(diǎn)數(shù);
列表:[item1, item2, ...]
元組:(item1, item2, ...)
字典:{key1:value1, key2:value2, ...}
布爾型:true/false
算術(shù)運(yùn)算:
+, -, *, /, //, %, **
比較操作:
==, !=, >, <, >=, <=
邏輯運(yùn)算:and, or, not
實(shí)例:
[root@node1 templates]# cat nginxconf.j2.bak
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost }}
# listen {{ vhost.listen }}
}
{% endfor %}
worker_processes {{ ansible_processor_vcpus }};
[root@node1 tmp]# cat nginxtemp.yam
- hosts: mageduweb
remote_user: root
vars:
nginx_vhosts:
- web1
- web2
- web3
# nginx_vhosts:
# - listen: 8080
tasks:
- name: template config to remote hosts
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
條件判斷
- name: restart Nginx
service: name=nginx state=restarted
when: ansible_distribution_major_version == "6"
循環(huán)迭代
#基于字符串列表
tasks:
- name: create rsyncd file
copy: src={{ item }} dest=/tmp/{{ item }}
with_items:
- a
- b
- c
- d
*with_itmes 嵌套子變量*
#基于字典列表
- hosts: eagleslab
remote_user: root
tasks:
- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
- { name: 'testuser1' , groups: 'wheel'}
- { name: 'testuser2' , groups: 'root' }
角色
[root@node1 ~]# tree /etc/ansible/roles/
/etc/ansible/roles/
└── http
├── defaults
├── files
├── headlers
├── meta
├── tasks
├── template
└── varsfiles/:存儲(chǔ)由copy或script等模塊調(diào)用的文件辽社;
- tasks/:此目錄中至少應(yīng)該有一個(gè)名為main.yml的文件伟墙,用于定義各task;其它的文件需要由main.yml進(jìn)行“包含”調(diào)用滴铅;
- handlers/:此目錄中至少應(yīng)該有一個(gè)名為main.yml的文件远荠,用于定義各handler;其它的文件需要由main.yml進(jìn)行“包含”調(diào)用失息;
- vars/:此目錄中至少應(yīng)該有一個(gè)名為main.yml的文件譬淳,用于定義各variable档址;其它的文件需要由main.yml進(jìn)行“包含”調(diào)用;
- templates/:存儲(chǔ)由template模塊調(diào)用的模板文本邻梆;
- meta/:此目錄中至少應(yīng)該有一個(gè)名為main.yml的文件守伸,定義當(dāng)前角色的特殊設(shè)定及其依賴關(guān)系;其它的文件需要由main.yml進(jìn)行“包含”調(diào)用浦妄;
- default/:此目錄中至少應(yīng)該有一個(gè)名為main.yml的文件尼摹,用于設(shè)定默認(rèn)變量;
- files目錄:存放由copy或script等模塊調(diào)用的文件剂娄;
調(diào)用roles:
[root@node1 ~]# cat /etc/ansible/roles/site.yml
- hosts: abc
remote_user: root
roles:
- httpd
實(shí)例分析:
[root@node1 ansible]# tree roles/
roles/
├── http
│ ├── defaults
│ │ └── main.yaml
│ ├── files
│ │ └── index.html
│ ├── headlers
│ │ └── main.yaml
│ ├── meta
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── httpd.conf.j2
│ └── vars
│ └── main.yaml
└── site.yaml
[root@node1 roles]# cat site.yaml
- hosts: node2
remote_user: root
# vars:
# httpd_ports: 8080
roles:
- http
[root@node1 http]# cat files/index.html
hello eagleslab!
[root@node1 http]# cat tasks/main.yaml
- name: installed httpd service
yum: name=httpd state=latest
when: ansible_os_family == "RedHat"
- name: start httpd server
service: name=httpd state=restarted
- name: write httpd config
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: config httpd index
copy: src=index.html dest=/var/www/html/index.html
[root@node1 http]# cat templates/httpd.conf.j2 | grep -Ev "^[[:space:]]|^#"
ServerRoot "/etc/httpd"
Listen {{ httpd_ports }}
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
</Directory>
<Directory "/var/www/html">
</Directory>
<IfModule dir_module>
</IfModule>
<Files ".ht*">
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
</IfModule>
<IfModule alias_module>
</IfModule>
<Directory "/var/www/cgi-bin">
</Directory>
<IfModule mime_module>
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
[root@node1 http]# cat headlers/main.yaml
- name: restart httpd
service: name=httpd state=restarted
[root@node1 http]# cat vars/main.yaml
httpd_ports: 8000