Ansible每个数据库名称运行一次任务

时间:2015-02-10 09:04:37

标签: ansible ansible-playbook

我正在使用ansible将多个站点部署到同一台服务器。每个网站都是ansible hosts广告资源中的一个单独的“主机”,效果非常好。

但是,只有两个数据库:生产和测试。 如何确保每个数据库只运行一次数据库迁移任务?

我已阅读group_byrun_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

2 个答案:

答案 0 :(得分:2)

所以,下面将说明为什么我说"每组一次"通常不受支持。当然,我很乐意看到一种更清洁的开箱即用的方式,如果每组一次,我会告诉我#34;并不是你追求的目标。

主机:

[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

示例playbook将在每个组(两个真实组)上对 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']

答案 1 :(得分:2)

我发现的最好方法是将任务的执行限制在组的第一个主机上。因此,您需要将groupname和数据库添加到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