You can use them both, they complement each other.
Terraform config files declaratively describe the servers/VMs to be created and is meant to be provider agnostic so you can create a mix of cloud servers at AWS, Azure, Digital Ocean, etc etc and local (VMWare), etc. When run it compares the current state of your infrastructure with how you declared you want it to change and it provisions/changes any new resources for you. It’s like CloudFormation but instead of just working with AWS it works with dozens of other providers. IMO it’s very good stuff.
After the infrastructure is created then Ansible (or if you prefer Chef, Puppet, Saltstack, etc) installs, configures, and manages software (such as Nginx, PostgreSQL, Distillery releases, etc) on the newly created servers.