Answers: <p>This is a duplicate of this StackOverflow <a href="https://stackoverflow.com/questions/31912748/how-to-run-a-particular-task-on-specific-host-in-ansible">question</a>.</p>
  <p>But I will combine and summarize all the answers here.</p>
  <ul>
  <li><p>If your &quot;other_host&quot; is in the playbooks inventory then you can use
  the <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html" rel="nofollow noreferrer">when</a> keyword: <code>when: inventory_hostname is &quot;other_host&quot;</code>.
  Which will only run that task once and only for &quot;other_host&quot;.</p>
  </li>
  <li><p>If your playbook inventory does not include &quot;other_host&quot; then you
  can use the <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html" rel="nofollow noreferrer">delegate_to</a> keyword. <code>delegate_to: other_host_ip</code>.
  Not that you have to use the IP or DNS name of the machine unless
  you use <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html#add-host-module" rel="nofollow noreferrer">add_host</a> module. This will run the task for EVERY host
  in the playbooks inventory but will run the tasks on <code>other_host</code>.</p>
  </li>
  </ul>
  
Answers: CC BY-SA 4.0
Answers: 2022-08-13T15:06:09.507Z
Answers: 2022-08-13T15:06:09.507Z
Answers: Levi
Body: <p>I'm writing an ansible playbook to manage backup and I want two different tasks :</p>
  <pre><code>- name: Setup local machine for backup
    cron:
      cron_file: /etc/cron.d/backup
      hour: 4
      minute: 0
      job: /root/do_backup.sh
      state: present
      name: backup
  
  - name: Setup backup server for new machine
    shell:
      cmd: &quot;mkdir /backups/{{inventory_hostname}}&quot;
  </code></pre>
  <p>Is it possible to tell ansible that second task is intended to be executed on another machine of my inventory ?</p>
  <p>I don't want a dedicated playbook, because some later tasks should be executed after the task on backup server.</p>
  
Tags: <ansible><backup>
Title: How to run a task from a playbook to a specific host
UserDisplayName: Lionep
ThreadText: # How to run a task from a playbook to a specific host
  
  (Asked by: username_0 on 2022-08-12)
  
  I'm writing an ansible playbook to manage backup and I want two different tasks :
  
  ```
  - name: Setup local machine for backup
    cron:
      cron_file: /etc/cron.d/backup
      hour: 4
      minute: 0
      job: /root/do_backup.sh
      state: present
      name: backup
  
  - name: Setup backup server for new machine
    shell:
      cmd: "mkdir /backups/{{inventory_hostname}}"
  
  ```
  
  Is it possible to tell ansible that second task is intended to be executed on another machine of my inventory ?
  
  I don't want a dedicated playbook, because some later tasks should be executed after the task on backup server.
  
  **Tags:** ansible, backup
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-13. Score: 2 | Accepted answer
  
  This is a duplicate of this StackOverflow [question](https://stackoverflow.com/questions/31912748/how-to-run-a-particular-task-on-specific-host-in-ansible).
  
  But I will combine and summarize all the answers here.
  
  * If your "other\_host" is in the playbooks inventory then you can use
    the [when](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html) keyword: `when: inventory_hostname is "other_host"`.
    Which will only run that task once and only for "other\_host".
  * If your playbook inventory does not include "other\_host" then you
    can use the [delegate\_to](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html) keyword. `delegate_to: other_host_ip`.
    Not that you have to use the IP or DNS name of the machine unless
    you use [add\_host](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html#add-host-module) module. This will run the task for EVERY host
    in the playbooks inventory but will run the tasks on `other_host`.
  
  
  ---
  
  