What happened was I was passing in a long string of different host-name with comma as the delimiter from Terraform in my "user_data". I needed to parse this long string and split the host-names into different group by matching with a few keywords.
For e.g. , hostnames="web01.xxx/com,web02.xxx.com,app01.yyy.com". My Ansible script will split this, loop it and assign the Group value.
Below is my sample code:
- add_host:
name: "{{item}}"
group: >-
{% set groupy = "default" -%}
{% if 'web' in item -%}
{% set groupy = "web" -%}
{% elif 'app' in item -%}
{% set groupy = "app" -%}
{% endif -%}
{{ groupy }}
with_items: "{{hostnames.split(',')}}"