0

我想(步骤 1)更改 postgresql 配置文件,(步骤 2)重新启动服务,然后(步骤 3)添加 db 用户。

../roles/postgres/tasks/main.yml

- name: change postgre pg_hba.conf
  template: src=pg_hba.conf.j2 dest=/etc/postgresql/9.4/main/pg_hba.conf 
  notify: restart postgresql service
  tags: pg_hba

- name: set password for postgres
  postgresql_user:
    db: postgres
    user: postgres
    password: postgres

../roles/postgres/handlers/main.yml

---
- name: restart postgresql service
  service: name=postgresql state=restarted enabled='yes'

postgres.yml

- hosts: postgresql_server
  remote_user: ubuntu
  become: true
  become_method: sudo
  become_user: root

  roles:
    - role: postgres

问题:ansible 角色中真正的工作流程是:step1 -> step3 -> step2。

我可以通过将处理程序任务移入来修复它../roles/postgres/tasks/main.yml,但无论配置文件是否更改,它都会重新启动服务。

这种请求的最佳做法是什么?

4

1 回答 1

1

meta: flush_handlers在需要它的任务之前刷新处理程序:

- name: change postgre pg_hba.conf
  template: src=pg_hba.conf.j2 dest=/etc/postgresql/9.4/main/pg_hba.conf 
  notify: restart postgresql service
  tags: pg_hba

- meta: flush_handlers

- name: set password for postgres
  postgresql_user:
    db: postgres
    user: postgres
    password: postgres
于 2017-12-19T02:07:59.320 回答