The train from Ankara to Tehran runs once a week. I was unable to get a ticket from outside Turkey and it seems it is fully booked for weeks. What are the chances that a place in the sleeping cars becomes available on the morning of departure and in such case, can I get on the train at the last minute without buying a ticket in advance?
I don't know how you could check their availability because as I know there is not online booking option for those lines or if there is, it must be very weak and unreliable. Let me know if you checked in Internet or an agency.
Another point, there are also buses going to Tehran from Turkey, mainly from Istanbul but I'm sure there are some from Ankara. Why not using them instead, if the trains are really full. They go faster than train however some parts of the road are freaky (you are the only car in the street and as long as eyes work there is still road to go). I took the path from Istanbul to Tehran (twice, both in winter), it took ~36 hours to reach Tehran. I can say it was memorable but I'm not sure if I try it again. :)