how to run play for subgroup when children has same name

I have an inventory something like this , each main cluster has children like broker.
Now how do i run any tasks against perticular group.broker. When we pass the group with --limit it does not work it runs on all broker. Also main playbook has hosts:all in it

annsible/grafana-agent-play.yaml -i kafka_inventory.yaml --limit qa_kafka_cluster -u sam -k -b

dev_kafka:

children:

App:

children:

dev_kafka_cluster:

children:

zookeeper:

hosts:

dev1-main-zookpr[01:05].dev

broker:

hosts:

dev1-main-kafka[01:06].dev

schema_registry:

hosts:

dev1-main-kafka[01:06].dev

uc:

children:

qa_kafka:

children:

App:

children:

qa_kafka_cluster:

children:

zookeeper:

hosts:

qa1-main-zookpr[01:03].qa

broker:

hosts:

qa1-main-kafka[01:06].qa

schema_registry:

hosts:

qa1-main-kafka[04:06].qa

It appears you are trying to define two different “broker” groups: one as a child of the “dev_kafka_cluster” and another as a child of “qa_kafka_cluster”.

That isn’t how groups work. There is only one “broker” group. You have added hosts to the “broker” group in two places.

(Also, “App” as a group name should be lower-case letters.)

Let me suggest an alternative inventory/group arrangement that implements this naming scheme:
kafka[{$env}[{app,web,db}[_{zoo,broker,schema}]]]
In this scheme, there isn’t a “broker” group. Instead there are groups named “kafka_dev_app_broker” and “kafka_qa_app_broker” (and eventually a “kafka_prd*” set I imagine).

I call this type of group naming scheme “fully articulated” — any group you care to specify is completely unambiguous. It’s a set of strict hierarchies (in this case there’s only one hierarchy: “kafka”), and hosts appear in a particular hierarchy exactly once. (If you find yourself trying to inject a host in two different places, you either need to rethink your hierarchy’s structure, or maybe you need two hierarchies.)

We have on occasion included an “all” environment (like “dev” and “qa”). If you did that, there would be a corresponding “kafka_all*” group for any “kafka_dev*” or “kafka_qa*” groups. Then you could target all the brokers with “kafka_all_app_broker” instead of “kafka_dev_app_broker**,**kafka_qa_app_broker”. You would have to decide if the benefit is worth the extra text you have to maintain in your inventory.

Your inventory would look like this:

kafka:
  children:
    kafka_dev:
      children:
        kafka_dev_app:
          children:
            kafka_dev_app_zoo:
              hosts:
                dev1-main-zookpr[01:05].dev
            kafka_dev_app_broker:
              hosts:
                dev1-main-kafka[01:06].dev
            kafka_dev_app_schema:
              hosts:
                dev1-main-kafka[01:06].dev
        kafka_dev_web: […] # omitted for brevity,
        kafka_dev_db: […]  # but you get the idea.
  children:
    kafka_qa:
      children:
        kafka_qa_app:
          children:
            kafka_qa_app_zoo:
              hosts:
                qa1-main-zookpr[01:03].qa
            kafka_qa_app_broker:
              hosts:
                qa1-main-kafka[01:06].qa
            kafka_qa_app_schema:
              hosts:
                qa1-main-kafka[04:06].qa
        kafka_qa_web: […] # omitted for brevity,
        kafka_qa_db: […]  # but you get the idea.

Oops. That second “children:” under “kafka:” shouldn’t be there. Otherwise, it’s pretty close.

See `select` (or `reject`) filter with `match` test, you can use it to
go over group_names.

Brian said what to do, but left how to do it as a (very worthwhile) exercise for the reader.
It could look something like

    when: group_names | select('search', '_broker' ) | length > 0  # matches all the broker groups
    when: group_names | select('search', kenv ~ '_app_zoo') | length > 0 # app_zoo hosts in {{ kenv }} environment
    when: "'kafka_dev' in group_names"  # matches all your dev environment hosts
    when: "'kafka_dev_app_zoo' in group_names"  # matches all your dev app zoo hosts

In jinja templates, those become if conditionals:

    {% if group_names | select('search', '_broker' ) | length > 0 %}
    {% if group_names | select('search', kenv ~ '_app_zoo') | length > 0 %}
    {% if 'kafka_dev' in group_names" %}
    {% if 'kafka_dev_app_zoo' in group_names" %}

Sorry to be slow to respond. It’s been crazy busy at work.