Using dynamic variables with Ansible pull

I have a role that pulls in the correct variable depending on the type of server (eg web vs perl). If I set the variable within an inventory file, I can’t get my task to access the variable in question. I think my issue is the FQDN isn’t being used.

Inventory file

[web_servers]
myweb001.phl.domain.local
myweb004.phl.domain.local
myweb005.phl.domain.local

[perl_servers]
myperl011.phl.domain.local
myperl001.phl.domain.local
myperl010.phl.domain.local

[web_servers:vars]
server_type=web
some_random_variable=20

[perl_servers:vars]
server_type=perl
some_random_variable=40

task

- hosts: localhost

tasks:
  - debug: var={{ server_type }}

# output

`

PLAY [localhost]    ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'server_type' is undefined"}

NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @test.retry

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

`

I have a role that pulls in the correct variable depending on the type of
server (eg web vs perl). If I set the variable within an inventory file, I
can't get my task to access the variable in question. I think my issue is
the FQDN isn't being used.

# Inventory file

[web_servers]
myweb001.phl.domain.local
myweb004.phl.domain.local
myweb005.phl.domain.local

[perl_servers]
myperl011.phl.domain.local
myperl001.phl.domain.local
myperl010.phl.domain.local

[web_servers:vars]
server_type=web
some_random_variable=20

[perl_servers:vars]
server_type=perl
some_random_variable=40

# task

- hosts: localhost

Your playbook is working against localhost.

tasks:
  - debug: var={{ server_type }}

# output

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'server_type' is undefined"}

Since you inventory file does not define server_type for localhost this is as aspected.