总是通过Ansible在单个主机上运行任务吗? [英] Running a task on a single host always with Ansible?

查看:200
本文介绍了总是通过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.

任务是正确的方法吗?

推荐答案

如果您只需要运行一次,而不是在每个主机上运行,​​则可以使用

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆