How to split string twice in ansible playbook hosts section.

Below is how i call my ansible-playbook and pass application names as parameters APP1 & APP2

ansible-playbook -i /web/aes/admin/playbooks/updated.hosts /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e instance_name=APP1,APP2

Playbook:

split is not an ansible filter - you cannot use it like | split(something) - you have to use as a string method which is why instance_name.split(',') works

Thanks, Richard, but I was looking for a specific solution which seems to be tricky.

Wrong. The filter *split* is available since 2.11. See
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#manipulating-strings

>
> ansible-playbook -i /web/aes/admin/playbooks/updated.hosts
> /web/aes/admin/playbooks/split.yml -e ENV=qa -e NODE=cluster -e
> instance_name=APP1,APP2
>
> Playbook:
>
> ---
> - hosts: "{{ [ENV] | product(instance_name.split(',')) | product([NODE]) |
> product(['wladmin_mmsplit'])|map('flatten')|map('join', '_') }}"

Put the logic into the inventory plugin *constructed*. See

ansible-doc -t inventory ansible.builtin.constructed

In the example below, I'll use another inventory plugin *generator*
which you don't need because you obviously have another source of the
inventory. When you try the code below in your environment use this
source instead of 02-generator.yml. See

ansible-doc -t inventory ansible.builtin.generator

I've ignored the case of the parameters.

1) Create the inventory

tree inventory/

inventory/
├── 01-hosts
├── 02-generator.yml
└── 03-constructed.yml

cat inventory/01-hosts

localhost

cat inventory/02-generator.yml

plugin: ansible.builtin.generator
hosts:
  name: "{{ env }}_{{ instance }}_{{ node }}_{{ ext }}"
layers:
  env:
    - dev
    - qa
    - prod
  instance:
    - app1
    - app2
  node:
    - cluster1
    - cluster2
  ext:
    - wladmin_mmsplit

cat inventory/03-constructed.yml

plugin: ansible.builtin.constructed
use_extra_vars: true
compose:
  layer_env: ENV.split(',')
  layer_node: NODE.split(',')
  layer_instance: instance_name.split(',')
groups:
  my_group: inventory_hostname.split('_').0 in layer_env and
            inventory_hostname.split('_').1 in layer_instance and
            inventory_hostname.split('_').2 in layer_node

2) Test inventory

ansible-inventory -i inventory --list --yaml

all:
  children:
    ungrouped:
      hosts:
        dev_app1_cluster1_wladmin_mmsplit: {}
        dev_app1_cluster2_wladmin_mmsplit: {}
        dev_app2_cluster1_wladmin_mmsplit: {}
        dev_app2_cluster2_wladmin_mmsplit: {}
        localhost: {}
        prod_app1_cluster1_wladmin_mmsplit: {}
        prod_app1_cluster2_wladmin_mmsplit: {}
        prod_app2_cluster1_wladmin_mmsplit: {}
        prod_app2_cluster2_wladmin_mmsplit: {}
        qa_app1_cluster1_wladmin_mmsplit: {}
        qa_app1_cluster2_wladmin_mmsplit: {}
        qa_app2_cluster1_wladmin_mmsplit: {}
        qa_app2_cluster2_wladmin_mmsplit: {}

3) Test inventory group *my_group*

ansible-inventory -i inventory --list --yaml -e ENV=qa -e NODE=cluster2 -e instance_name=app1,app2

all:
  children:
    my_group:
      hosts:
        qa_app1_cluster2_wladmin_mmsplit:
          ENV: qa
          NODE: cluster2
          instance_name: app1,app2
          layer_env:
          - qa
          layer_instance:
          - app1
          - app2
          layer_node:
          - cluster2
        qa_app2_cluster2_wladmin_mmsplit:
          ENV: qa
          NODE: cluster2
          instance_name: app1,app2
          layer_env:
          - qa
          layer_instance:
          - app1
          - app2
          layer_node:
          - cluster2
    ungrouped:
      hosts:
        dev_app1_cluster1_wladmin_mmsplit:
    ...

4) Use *my_group* in a playbook

cat pb.yml

- hosts: my_group
  tasks:
    - debug:
        var: ansible_play_hosts_all
      run_once: true

ansible-playbook -i inventory -e ENV=qa -e NODE=cluster2 -e instance_name=app1,app2 pb.yml

PLAY [my_group]