Delegating a playbook to run across multiple hosts

I’m having a newbie issue related to orchestrating actions across hosts. I’m probably doing this completely the wrong way.

Essentially what I want to do is call a playbook from within a playbook, targeting specific hosts.

An example use case is I add an exported dir on an NFS server, then I want to log into the clients I’ve exported it to and mount the new share.
Or, I make some DB change and now need to connect to all the impacted application servers to tell them to reconnect to the DB.
Or, I’m patching servers in a load balanced server farm, so I want to first find the nodes and then iterate a patch/reboot cycle on each sequentially to avoid service downtime.

Basically, within my playbook I’m discovering the list of target hosts, and I figured I could just pass that list to an included playbook like so:

  • name: Call playbook to do something on a list of hosts in sequence
    include: something.yml
    delegate_to: “{{ item }}”
    with_items: “{{ my_target_hosts }}”

That seems to work, but something.yml will inherit all facts. For example “ansible_hostname” from within something.yml will reflect the original host, not the one where it’s running.
Then I should call re-run setup, I suppose? But if I do that, it will, for reasons I don’t understand, skip all tasks after setup re-ran.

Example something.yml when my_target_hosts are host1 and host2 - notice the last two tasks are skipped:

  • debug:
    msg: “i am here”

  • debug:
    msg: “my ansible_hostname is now {{ ansible_hostname }}”

  • name: Call setup again
    setup:

  • debug:
    msg: “ive run setup again and now my ansible_hostname is {{ ansible_hostname }}”

  • debug:
    msg: “i am done”

Results in:

TASK [debug] *******************************************************************
ok: [host1 → host1] => {
“msg”: “i am here”
}

TASK [debug] *******************************************************************
ok: [host1 → host1] => {
“msg”: “my ansible_hostname is now host1”
}

TASK [Call setup again] ********************************************************
ok: [host1 → host1]

TASK [debug] *******************************************************************
ok: [host1 → host1] => {
“msg”: “ive run setup again and now my ansible_hostname is host1”
}

TASK [debug] *******************************************************************
ok: [host1 → host1] => {
“msg”: “i am done”
}

TASK [debug] *******************************************************************
ok: [host1 → host2] => {
“msg”: “i am here”
}

TASK [debug] *******************************************************************
ok: [host1 → host2] => {
“msg”: “my ansible_hostname is now host1”
}

TASK [Call setup again] ********************************************************
ok: [host1 → host2]

TASK [debug] *******************************************************************
skipping: [host1] => {“changed”: false, “skip_reason”: “Conditional check failed”, “skipped”: true}

TASK [debug] *******************************************************************
skipping: [host1] => {“changed”: false, “skip_reason”: “Conditional check failed”, “skipped”: true}

Where’s my brainfart?