Hi@Lakshminarayanan,
Yes, you can do that. But after you launched your ec2 instance, you have to update your Ansible inventory file in the below format.
$ ec2-instance ansible_host=<<ec2-instance-ip>> ansible_user=ec2-user ansible_ssh_private_key_file=/location/of/the/keypair/your-key.pem
After that, you can run any command inside your EC2 instance.
name: Run any command
hosts: ec2-instance
become: yes
become_user: root
tasks: {{ write your commands }}