咱們完成一個任務,例如安裝部署一個httpd服務,咱們須要多個模塊(一個模塊也能夠稱之爲task)提供功能來完成。而playbook就是組織多個task的容器,他的實質就是一個文件,有着特定的組織格式,它採用的語法格式是YAML(Yet Another Markup Language)。YAML語法可以簡單的表示散列表,字典等數據結構。具體請參考YAML詳細語法html
參考:https://blog.51cto.com/13589448/2068546web
ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check # 檢查可是不作真正改變
-f FORKS, --forks=FORKS # 指定併發
--list-hosts # 列出符合條件的主機
--syntax-check # 語法校驗
-e EXTRA_VARS, --extra-vars=EXTRA_VARS # 傳值
應用redis
- hosts: webservers # 用戶組
remote_user: root # 遠程用戶
tasks:
- name: install httpd # 任務名稱 yum: name=httpd state=present # 模塊 安裝 - name: install configure file copy: src=httpd.conf dest=/etc/httpd/conf/ - name: start httpd service service: name=httpd state=started
hosts: web tasks: - name:偷看姑娘 dong: 偷看姑娘 when: 站着 - name: 偷看姑娘 dong: 偷看姑娘 when: 趴着
- hosts: web tasks: - name: content copy: content="xxxxxxxx" dest=/tmp/x.txt when: data=="3" - name: content copy: content="yyyyyyyy" dest=/tmp/x.txt when: data=="4" ansible-playbook -e data=3 p6.yml
使用playbook同時建立ann1,ann2, ann3用戶數據結構
- hosts: web tasks: - name: createuser user: name={{item}} with_items: - ann1 - ann2 - ann3
- hosts: web tasks: - name: createuser user: name={{item}} with_items: - ann32 - ann33 - ann34 - name: creategroup group: name={{item}} with_items: - ben23 - ben24 - ben25
需求併發
建立用戶組,ben33,ben34 , ben35spa
建立用戶,ann53 ,附加組是ben33 , ann54 附加組是ben34, an55 附加組是ben35rest
- hosts: web tasks: - name: creategroup group: name={{item}} with_items: - ben33 - ben34 - ben35 - name: createuser user: name={{item.user}} groups={{item.group}} with_items: - {"user":ann53,"group":ben33} - {"user":ann54,"group":ben34} - {"user":ann55,"group":ben35}
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf tags: copyfile - name: startredis service: name=redis state=restarted ansible-playbook -t copyfile p12.yml 建立一個tempaltes目錄用來放文件
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/root/playbook/redis.conf tags: copyfile - name: startredis service: name=redis state=restarted ansible-playbook -t copyfile p12.yml
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf tags: copyfile notify: restartredis - name: startredis service: name=redis state=started handlers: - name: restartredis service: name=redis state=restarted