Execute Ansible task on different hosts

Question:

I have a created a role, where I have defined all ansible tasks. Also I have host A and host B in the inventory. Is it possible to execute 90% of task on host A and 10% of task on host B? My Ansible controller is host C.

Answer:

The first way I can think to do this is with conditionals based on inventory.

Create a group in inventory for A, and a group for B. I will call these group_A and group_B

Then use conditionals on your tasks

Depending on how many tasks you have, it may be too much to put a conditional on every task, so you can use conditionals on includes, like so:

file structure:

main.yml:

A_tasks.yml:

B_tasks.yml:

Leave a Reply