tread
tread

Reputation: 11098

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

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?

Upvotes: 1

Views: 6919

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56877

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

Upvotes: 4

Related Questions