Categories

Sep 19, 2019 in Ansible

Q: Ansible delegate_to

1 Answer

Oct 11, 2019
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: localhost

        register: local_processes_number

      - name: Print local running processes

        debug:

          msg: '{{ local_processes_number.stdout }}'
Click here to read more about Ansible
Click here to read more about Insurance

Related questions

Madanswer
Aug 24, 2019 in Ansible
Aug 24, 2019 in Ansible
Aug 24, 2019 in Ansible
...