Ansible when first host in group

Published on:

Here is how you run a task on just the first host in a group. We have multiple kafka servers, but we only need to run the task to create new topics on the first one. And we want that task to be a part of our playbook that deploys and configures all the kafka servers.

# some task that creates kafka topics
when: inventory_hostname == groups["{{some_var}}-kafka"][0]