You can put this in a single playbook and target the tasks to different hosts in one of the following ways:
1. Define groups in your Ansible inventory file and delegate tasks to specific hosts:
Put your hosts in groups, e.g.:
[www_hosts]
host1
host2
host3
[tmp_hosts]
host2
host4
And setup your playbook to delegate tasks to these groups like this:
- name: Copy the file to www_path
ansible.builtin.copy:
src: "foo1"
dest: "/var/www"
run_once: true
delegate_to: "{{ item }}"
loop: "{{ groups['www_hosts'] }}"
- name: Copy the file to tmp_path
ansible.builtin.copy:
src: "foo1"
dest: "/var/tmp"
run_once: true
delegate_to: "{{ item }}"
loop: "{{ groups['tmp_hosts'] }}"
Don't forget to set the run_once to true otherwise the same tasks will run (and be delegated to each host) on each host you target the playbook.
2. Setup variables as a condition to run tasks on specific hosts:
If you don't want your playbook to be dependent on a specific inventory setup you can also set it up in a variables file or section and run tasks selectively. E.g.:
Set up a vars/main.yml file (or vars-section) with:
www_hosts:
- host1
- host2
- host3
tmp_hosts:
- host2
- host4
Make sure these vars gets loaded into your playbook/role and setup playbook tasks like this:
- name: Copy the file to www_path
ansible.builtin.copy:
src: "foo1"
dest: "/var/www"
when: ansible_fqdn in www_hosts
- name: Copy the file to tmp_path
ansible.builtin.copy:
src: "foo1"
dest: "/var/tmp"
when: ansible_fqdn in tmp_hosts