Create multiple instances but install software using user_data or remote_exec only on particular instances

Hi Team,

I have a requirement where we are supposed to create multiple instances[3], i am able to create them using for_each and toset. However, how can i use either the user_data or remote_exec in my code where it will install ansible on one server, jenkins and maven on the other two servers?

I am able to achieve this when I am creating 3 resource blocks individually however how can we simplify the code to avail this?

Refer to the documentation about the each object: The for_each Meta-Argument - Configuration Language | Terraform | HashiCorp Developer

This is how you can set user_data to different final values for particular instances.