Ansible run task once per database-name

2019-05-11 00:26发布

问题:

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

回答1:

So, the below will illustrate why I say "once per group" is generally not supported. Naturally, I would love to see a cleaner out-of-the-box way, and do let me know if "once per group" isn't what you're after.

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

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

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

[all:children]
production
beta

Example playbook which will do something on the dbhost once per group (the two real groups):

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

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