始终使用 Ansible 在单个主机上运行任务? [英] Running a task on a single host always with Ansible?
问题描述
我正在编写一个从特定位置下载数据库转储的任务.它将始终在同一主机上运行.
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
任务内容为:
---
- 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?
推荐答案
如果您只需要运行一次而不是在每个主机上运行,您可以改用 run_once
像这样:
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
这将从运行该任务的第一台主机运行.如果您想专门针对特定主机,您可以使用 delegate_to
进一步限制:
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
这篇关于始终使用 Ansible 在单个主机上运行任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!