O'Reilly logo

Learning Ansible 2 - Second Edition by Fabio Alessandro Locati

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Delegating a task

Sometimes you 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 local host. 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: ansible      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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required