Running a task on a single host always with Ansible?

◇◆丶佛笑我妖孽 提交于 2020-01-03 03:12:07

问题


I am writing a task to download a database dump from a specific location. It will always be run on the same host.

So I am including the task as follows in the main playbook:

tasks:
  include: tasks/dl-db.yml

The content of the task is:

---
   - name: Fetch the Database
     fetch: src=/home/ubuntu/mydb.sql.gz dest=/tmp/mydb.sql.bz fail_on_missing=yes

But I want it to fetch from a single specific host not all hosts.

Is a task the right approach for this?


回答1:


If all you need to happen is that it's only run once rather than on every host you can instead use run_once like so:

---
   - name: Fetch the Database
     run_once: true
     fetch: src=/home/ubuntu/mydb.sql.gz dest=/tmp/mydb.sql.bz fail_on_missing=yes

This will then be run from the first host that runs the task. You can further limit this with delegate_to if you want to specifically target a specific host:

---
   - name: Fetch the Database
     run_once: true
     delegate_to: node1
     fetch: src=/home/ubuntu/mydb.sql.gz dest=/tmp/mydb.sql.bz fail_on_missing=yes


来源:https://stackoverflow.com/questions/33052201/running-a-task-on-a-single-host-always-with-ansible

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