To what extent were mathematicians in previous centuries aware of the lack of rigour in their methods? By modern standards, much of pre-modern mathematics isn't rigorous. Famous examples include Euler's solution to the Basel problem or literally anything involving sets before Cantor and Russel came along, when a "set" was simply a handwaving notion of "all things that have some property", later found to be quite problematic.

To what extent were the mathematicians of the past aware of these shortcomings? Did they even feel mathematics needed to be a rigorous subject, or was there an idea that anything goes as long as it works?

I feel this question is not just of historical interest. Modern mathematics is usually judged (by modern mathematics) to be rigorous, but we have no reason to believe we are able to assess our methods correctly unless our predecessors were capable of assessing theirs.
Lagrange launched a contest at the end of the 18th century whose goal was to clarify the notions of infinity and infinitesimal. There was no clear winner, but Lazare Carnot submitted an entry which eventually became a popular book. This is only one episodes illustrating the fact that rigor has indeed been a concern historically. 

A century earlier, there was a debate at the French academy focusing on similar issues, opposing Rolle and Varignon. 

In the 19th century, Cauchy lists rigor as one of the objectives of his approach to analysis.

So to answer your question, mathematicians in previous centuries were aware of lack of rigor, but in most cases attributed it to their predecessors only. This seems to be the case today, as well.
It is a little known fact that much of Bernhardt Riemann's work had remained un-rigorous until Hilbert's arrival on the mathematical scene some fifty years after Riemann's death.  Riemann was aware of his shortcomings, but many before him were not.  Did you know that complex numbers were not properly accepted until the nineteenth century?  The historian D.T. Whiteside considered Newton's first proposition in the Principia to be problemmatic.  According to Michael Atiyah, it took hundreds of years after Newton's death for calculus to be made properly rigorous.  It is worth pointing out that in Newton's day, Euclidean geometry was considered to be more rigorous than the new-fangled algebraic approach introduced by Descartes (according to historian of mathematics S. Hollingdale).  Also, Euler was ignorant of the fact that he had made some completely incorrect deductions concerning alternating series.  Of course, we know from the correspondence of G.H. Hardy that the great Ramanujan did not possess properly rigorous proofs for several of his 4000+ theorems.  In the nineteenth century there was at least one mathematician who had erroneously believed during his lifetime he had correctly proved a special case of Fermat's last theorem.  According to Felix Klein, even the great Gauss did not appear to be to concerned about being rigorous when he was involved with more practically inclined investigations.  But Gauss was well aware of this.