how to run play for subgroup when children has same name

47 views
Skip to first unread message

Sameer Modak

unread,
Apr 19, 2024, 8:40:08 AM4/19/24
to Ansible Project
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



========



Todd Lewis

unread,
Apr 19, 2024, 10:58:32 AM4/19/24
to ansible...@googlegroups.com, uto...@gmail.com
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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0e0e0032-661b-4d2e-9fe4-347c08157d7bn%40googlegroups.com.

-- 
Todd

Todd Lewis

unread,
Apr 19, 2024, 11:07:51 AM4/19/24
to Ansible Project
Oops. That second "children:" under "kafka:" shouldn't be there. Otherwise, it's pretty close.
Message has been deleted
Message has been deleted
Message has been deleted

Brian Coca

unread,
Apr 25, 2024, 1:10:29 PM4/25/24
to ansible...@googlegroups.com
See `select` (or `reject`) filter with `match` test, you can use it to
go over group_names.

--
----------
Brian Coca (he/him/yo)

Todd Lewis

unread,
Apr 25, 2024, 4:17:39 PM4/25/24
to ansible...@googlegroups.com, uto...@gmail.com
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.

Todd

On 4/22/24 2:12 PM, Sameer Modak wrote:
That was useful. But  i still  did not quite get how do we pass kafka_dev* in j2 or during runtime like we dont know before hand what group we will be running against.

so in when condition can we pass something like this when group is kafka_dev*

Reply all
Reply to author
Forward
0 new messages