# How to perform tasks only one host, based on previous output

**URL:** <https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200>\
**Category:** Ansible Project\
**Created:** [December 8, 2016, 12:25pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200 "2016-12-08T12:25:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 8, 2016, 12:25pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/1 "2016-12-08T12:25:16Z")

</div>

Hi,

My goal is to perform tasks only one host based on the output. Searched for `--limit` module to use in playbooks, but it is not present.

A shell script is executed on 3 nodes, which returns a boolean.

```auto
ok: [localMulti1] => {
    "variable": {
        ...
        "stdout": "true",
        ...
    }
}
ok: [localMulti2] => {
    "variable": {
        ...
        "stdout": "false",
        ...
    }
}
ok: [localMulti3] => {
    "variable": {
        ...
        "stdout": "false",
        ...
    }
}

```

How can I perform tasks on only on `localMulti2`? But not statically include the host (inventory\_hostname == ‘…’)

Failing with:

```auto
- shell: echo
  when: variable.stdout == "false" and ...

```

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 9, 2016, 8:16am UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/2 "2016-12-09T08:16:09Z")

</div>

No guru’s who can reply?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 15, 2016, 2:07am UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/3 "2016-12-15T02:07:14Z")

</div>

should be simple enough

- script: myscript  
register: myreturn

- command: othertask  
when: myreturn.stdout|bool

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 15, 2016, 8:01am UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/4 "2016-12-15T08:01:14Z")

</div>

Sorry if my message was unclear. The goal is to perform ‘othertask’ on only ONE host, where the value is false.  
Your script will execute ‘othertask’ on ALL the hosts where the value is false, in this case, TWO hosts.

Also, run\_once will not work because it will only run on the first host, where the boolean is true.

---

<div class="post-metadata">

**Author:** ![Greg\_Langford](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Greg\_Langford](https://forum.ansible.com/u/Greg_Langford)\
**Post date:** [December 15, 2016, 11:18am UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/5 "2016-12-15T11:18:23Z")

</div>

Will the host be the same on each run?

If so you could put a guard round the task when ansible\_inventory\_hostname is equal to the desired host.

when: ansible\_inventory\_hostname == “desired-host-name”

Something like the above? Or maybe you could look into using delegated tasks [http://docs.ansible.com/ansible/playbooks\_delegation.html#delegation](http://docs.ansible.com/ansible/playbooks_delegation.html#delegation)

Kind Regards

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 15, 2016, 12:14pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/6 "2016-12-15T12:14:51Z")

</div>

Host differ each run. The task will be included in a role.  
Is it possible to do something like:

```auto
when: variable.stdout == "false"

```

```auto
Register: myVar

```

```auto

```

```auto
Task: a task

```

```auto
Shell: echo

```

```auto
When: myVar is defined
Delegate_to: "{{ first-host-where_myVar is defined }}"

```

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 15, 2016, 2:58pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/7 "2016-12-15T14:58:40Z")

</div>

Then just fail the ‘true’ host and now you can use run once with confidence only ‘false’ hosts got there.

- script: myscript  
register: myreturn  
failed\_when: not myreturn.stdout|bool

- command: othertask  
run\_once: true

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 15, 2016, 3:32pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/8 "2016-12-15T15:32:21Z")

</div>

That task would still run on only the first host, and not the second.  
Run\_once will only perform the task on the first host…

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 15, 2016, 3:54pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/9 "2016-12-15T15:54:01Z")

</div>

Isn’t that exactly what you wanted? if you want it to run on all ‘false’ hosts just remove the run\_once

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 15, 2016, 4:02pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/10 "2016-12-15T16:02:09Z")

</div>

Let met clarify. This is my output.

```auto
ok: [localMulti1] => {
    "variable": {
        ...
        "stdout": "true",
        ...
    }
}
ok: [localMulti2] => {
    "variable": {
        ...
        "stdout": "false",
        ...
    }
}
ok: [localMulti3] => {
    "variable": {
        ...
        "stdout": "false",
        ...
    }
}

```

How can I configure ansible to perform only a task on the second node, where the stdout is false. Not the third node  
Run\_once will never work, because it will always run it on the second host.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 15, 2016, 4:11pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/11 "2016-12-15T16:11:49Z")

</div>

> How can I configure ansible to perform only a task on the second node, where the stdout is false. Not the third node  
> Run\_once will never work, because it will always run it on the second host.

you realize you are giving contradictory statements?

you want it to run on the 2nd, not the 3rd but run once will never work because it will run on the 2nd???

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 16, 2016, 8:13am UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/12 "2016-12-16T08:13:57Z")

</div>

Hihi, you’re right. My bad, meant run\_once will always run on the 1st node.  
Anyway…

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 16, 2016, 5:17pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/13 "2016-12-16T17:17:50Z")

</div>

not in my example, as the first node is removed from the play.

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 16, 2016, 6:01pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/14 "2016-12-16T18:01:48Z")

</div>

Nodes are excluded from a play when they fail. This is not the desired output.  
I don’t want to fail a task, I want it set-up as it should.

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 16, 2016, 6:12pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/15 "2016-12-16T18:12:19Z")

</div>

Here is a playbook to test it locally.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 16, 2016, 6:51pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/16 "2016-12-16T18:51:59Z")

</div>

You keep changing the goalpost, now you want it to execute on every node? then when: me.stat.exists == False is all you need.

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 16, 2016, 7:20pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/17 "2016-12-16T19:20:49Z")

</div>

That would still perform it only on host 2 and 3.  
Goal: perform task only one node 2 or 3 where the output is, in this case, true.

Thank you so much for your support 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 16, 2016, 7:31pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/18 "2016-12-16T19:31:32Z")

</div>

Then my first suggestion works for that case.

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [December 16, 2016, 7:48pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/19 "2016-12-16T19:48:54Z")

</div>

Not what I desire, because it will perform the task on both 2 and 3.  
I only desire to perform a task on 2 OR 3.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 16, 2016, 7:56pm UTC](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200/20 "2016-12-16T19:56:45Z")

</div>

run\_once prevents that

[Next page](https://forum.ansible.com/t/how-to-perform-tasks-only-one-host-based-on-previous-output/23200.md?page=2)
