2015-02-10 62 views
3

我使用ansible將多個站點部署到同一臺服務器。每個站點都是一個單獨的「主機」,位於安全的hosts庫存中,這非常適用。Ansible每個數據庫名稱運行一次任務

但是,只有兩個數據庫:生產和測試。 如何確保我的數據庫遷移任務只能在每個數據庫上運行一次?

我已讀入group_by,run_oncedelegate_to的功能,但我不確定如何組合這些功能。

的主機看起來像:

[production] 
site1.example.com  ansible_ssh_host=webserver.example.com 
site2.example.com  ansible_ssh_host=webserver.example.com 

[beta] 
beta-site1.example.com ansible_ssh_host=webserver.example.com 
beta-site2.example.com ansible_ssh_host=webserver.example.com 

[all:children] 
production 
beta 

目前的劇本是這樣的:

--- 
- hosts: all 
- tasks: 

    # ... 

    - name: "postgres: Create PostgreSQL database" 
    sudo: yes 
    sudo_user: postgres 
    postgresql_db: db="{{ DATABASES.default.NAME }}" state=present template=template0 encoding='UTF-8' lc_collate='en_US.UTF-8' lc_ctype='en_US.UTF-8' 
    tags: postgres 
    register: createdb 
    delegate_to: "{{ DATABASES.default.HOST|default(inventory_hostname) }}" 

    # ... 

    - name: "django-post: Create Django database tables (migrate)" 
    django_manage: command=migrate app_path={{ src_dir }} settings={{ item.settings }} virtualenv={{ venv_dir }} 
    with_items: django_projects 
    #run_once: true 
    tags: 
    - django-post 
    - django-db 
    - migrate 
+0

一般來說,「一旦每組」不支持的語義,並試圖讓一切都變成一個單一的玩可能比它的價值更麻煩。只是想澄清一下,你希望第一個任務在每個主機上運行一次,第二個任務只有兩次,每個組中有一次? – 2015-02-10 17:54:51

回答

2

所以,下面將說明爲什麼我說「一旦每組」一般不支持。當然,我很樂意看到一種更清晰的開箱即用的方式,並且讓我知道「每組一次」是不是你所追求的。

主持人:

[production] 
site1.example.com  ansible_ssh_host=localhost ansible_connection=local 
site2.example.com  ansible_ssh_host=localhost ansible_connection=local 

[beta] 
beta-site1.example.com ansible_ssh_host=localhost ansible_connection=local 
beta-site2.example.com ansible_ssh_host=localhost ansible_connection=local 

[beta:vars] 
dbhost=beta-site1.example.com 

[production:vars] 
dbhost=site1.example.com 

[all:children] 
production 
beta 

示例劇本哪會做的東西DBHOST每組一次(兩個實集團):

--- 
- hosts: all 
    tasks: 
    - name: "do this once per group" 
    sudo: yes 
    delegate_to: localhost 
    debug: 
     msg: "do something on {{hostvars[groups[item.key].0]['dbhost']}} for {{item}}" 
    register: create_db 
    run_once: yes 
    with_dict: groups 
    when: item.key not in ['all', 'ungrouped'] 
2

我發現最好的辦法是限制將任務執行到組的第一個主機。因此,你需要添加組名和數據庫的group_vars文件,如:

group_vars /生產

--- 
dbtype=production 
django_projects: 
    - name: project_1 
     settings: ... 
    - name: project_n 
     settings: ... 

group_vars /測試

--- 
dbtype=beta 
django_projects: 
    - name: project_1 
     settings: ... 
    - name: project_n 
     settings: ... 

主機

[production] 
site1.example.com  ansible_ssh_host=localhost ansible_connection=local 
site2.example.com  ansible_ssh_host=localhost ansible_connection=local 

[beta] 
beta-site1.example.com ansible_ssh_host=localhost ansible_connection=local 
beta-site2.example.com ansible_ssh_host=localhost ansible_connection=local 


[all:children] 
production 
beta 

,並限制任務執行到第一個匹配的主機組:

- name: "django-post: Create Django database tables (migrate)" 
    django_manage: command=migrate app_path={{ src_dir }} settings={{ item.settings }} virtualenv={{ venv_dir }} 
    with_items: django_projects 
    when: groups[dbtype][0] == inventory_hostname 
    tags: 
    - django-post 
    - django-db 
    - migrate 
相關問題