playbook是由一個或多個play組成的列表,ansible能夠直接調用playbook,按照事先定義好的規則在遠程主機一次執行playbook上的操做。ansible的playbook是採用YAML的形式,文件後綴爲.yaml,須要遵循YAML的語法進行定義。web
注意:
playbook一個-name只能有一個模塊被調用,不然會報錯服務器
ansible-playbook --syntax-check user.yml #測試語法是否正確 ansible-playbook -C user.yml #測試執行,並非真正運行 ansible-playbook user.yml #運行playbook
[test] 192.168.189.129 user=test_3調用方法:
\- hosts: test remote_user: root tasks: \- name: add {{ user }} #經過{{ 變量名 }}調用變量 user: name={{ user }} state=present system=yes
[test] 192.168.189.129 [test:vars] user=test_4
\ - hosts: test remote_user: root vars: \ - user: test_5 \ - password: 1234567a tasks: \ - name: add {{ user }} user: name={{ user }} state=present system=yes password={{ password }}
[test] 192.168.189.129 ansible_ssh_pass=1234567a
- hosts: test vars: - service: httpd remote_user: root tasks: - name: install {{ service }} yum: name={{ service }} state=latest - name: install configure file copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: restart httpd - name: start {{ service }} service: name= {{ service }} state=started enabled=yes handlers: - name: restart httpd service: name=httpd state=restarted
- hosts: test remote_user: root tasks: - name: print hello world copy: content='hello world' dest=/tmp/test.log when: facter_ipaddress == '192.168.189.129' #只有當條件知足的時候才執行此tasks
playbook常常須要重複執行某指令,此時可使用迭代,迭代固定用法以下:ssh
- hosts: test remote_user: root tasks: - name: del user user: name={{ item }} state=present #此處固定必須爲item,表示調用迭代對象 with_items: #此處固定爲with_items,下面一次列出迭代對象 - test_2 - test_3 - test_4 - test_5
也能夠採用字典列表的方式來進行item調用:測試
- hosts: all remote_user: root tasks: - name: create groups group: name={{ item }} state=present with_items: - groupx1 - groupx2 - groupx3 - name: create users user: name={{ item.name }} group={{ item.group }} state=present with_items: - {name: 'userx1', group: 'groupx1'} - {name: 'userx2', group: 'groupx2'} - {name: 'userx3', group: 'groupx3'}
template文件可使用變量來代替某些固定的值,這樣能夠針對不一樣的服務器提供不一樣的配置文件。
變量支持以下類型:命令行
- hosts: test vars: - service: httpd remote_user: root tasks: - name: install {{ service }} yum: name={{ service }} state=latest - name: install configure file template: src=/etc/ansible/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #template文件能夠用j2結尾,表明是jinjia2文件 notify: restart httpd - name: start {{ service }} service: name= {{ service }} state=started enabled=yes handlers: - name: restart httpd service: name=httpd state=restarted
能夠對某一個task打上tag標籤,這樣在執行playbook的時候能夠直接指定要執行的tag,就不用總體在把全部的task再執行一遍了rest
- hosts: test vars: - service: httpd remote_user: root tasks: - name: install {{ service }} yum: name={{ service }} state=latest - name: install configure file template: src=/etc/ansible/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: restart httpd tags: conf #此處的conf表示tag名 - name: start {{ service }} service: name= {{ service }} state=started enabled=yes handlers: - name: restart httpd
調動tag的命令:code
ansible-playbook httpd.yml -t conf ansible-playbook httpd.yml --tags conf
roles能夠對多個playbook進行復用,好比說A主機須要安裝httpd,B主機也須要安裝httpd,可是他們又沒有在一個主機組,這個時候正常來講須要寫兩遍playbook,就形成了重複現象。此時能夠採用roles,對兩個主機都指定role爲httpd所在的roles便可進行playbook的複用。
roles有特定的格式,首先須要有一個roles目錄,而後在目錄下面須要有各個role的主目錄,主目錄下面爲各個分目錄:對象
#site.yml文件,下面文件表明給test配置websers何dbsers的role - hosts: test remote_user: root roles: - websers - dbsers
#tasks下main.yml文件 - name: install httpd yum: name=httpd state=latest - name: install config template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #template和copy模塊的src能夠直接調用相對路徑,相對roles/websers/files notify: restart httpd tags: conf - name: start httpd service: name=httpd state=started enabled=yes
#handlers下的main.yml文件 - name: restart httpd service: name=httpd state=restarted