Sometimes, you will want to execute an action on a different system. This could be, for instance, a database node while you are deploying something on an application server node or to the localhost. To do so, you can just add the delegate_to: HOST property to your task and it will be run on the proper node. Let's rework the previous example to achieve this:
---
- hosts: database
remote_user: vagrant
tasks:
- name: Count processes running on the remote system
shell: ps | wc -l
register: remote_processes_number
- name: Print remote running processes
debug:
msg: '{{ remote_processes_number.stdout }}'
- name: Count processes running on the local system
shell: ps | wc -l
delegate_to: localhost
register: local_processes_number
- name: Print local running processes
debug:
msg...