Ansible 为每个数据库名称运行一次任务 [英] Ansible run task once per database-name

查看:21
本文介绍了Ansible 为每个数据库名称运行一次任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ansible 将多个站点部署到同一台服务器.每个站点都是 ansible hosts 清单中的一个单独的主机",效果非常好.

然而,只有两个数据库:生产和测试.如何确保我的数据库迁移任务每个数据库只运行一次?

我已经阅读了 group_byrun_oncedelegate_to 功能,但我不确定如何组合这些功能.

主机看起来像:

[生产]site1.example.com ansible_ssh_host=webserver.example.comsite2.example.com ansible_ssh_host=webserver.example.com[测试版]beta-site1.example.com ansible_ssh_host=webserver.example.combeta-site2.example.com ansible_ssh_host=webserver.example.com[所有:儿童]生产测试版

当前的剧本是这样的:

---- 主持人:所有- 任务:# ...- 名称:postgres:创建 PostgreSQL 数据库"须藤:是的sudo_user: postgrespostgresql_db: db="{{ DATABASES.default.NAME }}" state=present template=template0 encoding='UTF-8' lc_collat​​e='en_US.UTF-8' lc_ctype='en_US.UTF-8'标签: postgres注册:createdbdelegate_to: "{{ DATABASES.default.HOST|default(inventory_hostname) }}"# ...- 名称:django-post:创建 Django 数据库表(迁移)"django_manage: command=migrate app_path={{ src_dir }} settings={{ item.settings }} virtualenv={{ venv_dir }}with_items: django_projects#run_once: 真标签:- django-post- django-db- 迁移

解决方案

我发现的最好方法是将任务的执行限制到组的第一个主机.因此,您需要将组名和数据库添加到 group_vars 文件中,例如:

group_vars/production

---数据库类型=生产django_projects:- 名称:project_1设置:...- 名称:project_n设置:...

group_vars/beta

---数据库类型=测试版django_projects:- 名称:project_1设置:...- 名称:project_n设置:...

主持人

[生产]site1.example.com ansible_ssh_host=localhost ansible_connection=localsite2.example.com ansible_ssh_host=localhost ansible_connection=local[测试版]beta-site1.example.com ansible_ssh_host=localhost ansible_connection=localbeta-site2.example.com ansible_ssh_host=localhost ansible_connection=local[所有:儿童]生产测试版

并将任务执行限制到与该组匹配的第一个主机:

- name: "django-post: 创建 Django 数据库表(迁移)"django_manage: command=migrate app_path={{ src_dir }} settings={{ item.settings }} virtualenv={{ venv_dir }}with_items: django_projects时间:groups[dbtype][0] ==inventory_hostname标签:- django-post- django-db- 迁移

I'm using ansible to deploy several sites to the same server. Each site is a separate 'host' in the ansible hosts inventory, which works really well.

However, there are only two databases: production and testing. How can I make sure my database-migration task only runs once per database?

I've read into the group_by, run_once and delegate_to features, but I'm not sure how to combine those.

The hosts look something like:

[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

The current playbook looks like this:

---
- 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

解决方案

The best way I found was to restrict the execution of a task to the first host of a group. Therefore you need to add the groupname and the databases to a group_vars file like:

group_vars/production

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

group_vars/beta

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

hosts

[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

and limit the task execution to the first host that matches that group:

- 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

这篇关于Ansible 为每个数据库名称运行一次任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆