ansible學習筆記(一)

最近在學習使用ansible,爲了未來的大規模部署應用做準備,這東西比我之前用過的puppet,saltstack都簡便一點,沒有client端,也不需要額外配置,基本上手技能用,據說在國外的熱門程度目前也超過saltstack了。

下面就開始零星的記錄吧。


確保服務在running狀態

tasks:
  - name: make sure apache is running
    service: name=httpd state=running

寫web server的vhost配置文件可以採用變量的方式

tasks:
  - name: create a virtual host file for {{ vhost }}
    template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
    
    
組合起來看下面兩條示例。notify中的task被稱作爲handlers,它們是單獨定義的,notify指定handlers的name,handlers分列了各個條目的task。
- 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=apache state=restarted


     
     
project的目錄結構示範
site.yml
webservers.yml
fooservers.yml
roles/
  common/
    files/
    templates/
    tasks/
    handlers/
    vars/
    defaults/
    meta/
  webservers/
    files/
    templates/
    tasks/
    handlers/
    vars/
    defaults/
    meta/

在playbook中,你應該考慮這麼寫:
- hosts: webservers
 roles:
    - common
    - webservers


如果play中包含"task"部分,那些"tasks"會在role角色之後被執行。
如果你希望定義一些在role之前或之後執行的任務,你可以這麼寫:

- hosts: webservers

 pre_tasks:
   - shell: echo 'hello'

 roles:
   - { role: some_role }

 tasks:
   - shell: echo 'still busy'

 post_tasks:
   - shell: echo 'goodbye'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章