Ansible delegate and run_once

荒凉一梦 提交于 2019-12-01 09:45:13

问题


i write one specific roles for local and dev environment that will drop and recreate the database from first server in dbserver group which mostly used as the master database.

group_vars/dbserver

[dbserver]
vagrant1 # master db
vagrant2 # slave db

after that, if i need to drop the database and also create the database again, basically i just need that command to be run on the first server in the group.

- name: drop database
  mysql_db: name={{ targetdbname }} state=absent
  when: targetdeploydb == "new"
  delegate_to: "{{ item }}"
  with_items: "{{ groups.dbserver }}"
  run_once: true

- name: create database
  mysql_db: name={{ targetdbname }} state=present
  when: targetdeploydb == "new"
  delegate_to: "{{ item }}"
  with_items: "{{ groups.dbserver }}"
  when: targetdeploydb == "new"
  run_once: true

Here is the log when i run the playbook

TASK [laravel : drop database] *************************************************
changed: [vagrant1 -> vagrant1] => (item=vagrant1)
changed: [vagrant1 -> vagrant2] => (item=vagrant2)

TASK [laravel : create database] ***********************************************
changed: [vagrant1 -> vagrant1] => (item=vagrant1)
changed: [vagrant1 -> vagrant2] => (item=vagrant2)

Another way that i can think of is by using the master db hostname directly on delegate_to but it means i need to create another variable. in the other hand, i think it would be better to reduce the number of variable and make it more dynamic. Please advise


回答1:


If you need to delegate your task to the first server only and run it once no matter how many servers in the current play, use:

- name: drop database
  mysql_db: name={{ targetdbname }} state=absent
  when: targetdeploydb == "new"
  delegate_to: "{{ groups['dbserver'] | first }}"
  run_once: true


来源:https://stackoverflow.com/questions/39297727/ansible-delegate-and-run-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!