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

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.

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:

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

No guru’s who can reply?

should be simple enough

  • script: myscript
    register: myreturn

  • command: othertask
    when: myreturn.stdout|bool

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.

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

Kind Regards

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

when: variable.stdout == "false"
Register: myVar

Task: a task
Shell: echo
When: myVar is defined
Delegate_to: "{{ first-host-where_myVar is defined }}"

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

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…

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

Let met clarify. This is my output.

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.

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???

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

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

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.

Here is a playbook to test it locally.

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

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 :slight_smile:

Then my first suggestion works for that case.

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.

run_once prevents that