0

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.

1 Answer 1

1

This is a duplicate of this StackOverflow question.

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 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 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 module. This will run the task for EVERY host in the playbooks inventory but will run the tasks on other_host.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.