The motivation DOES make a difference to the karma of an action. The classic discussion about karma stresses motivation.
Karma as action denotes an act of mental volition (cetanā), and the bodily and verbal actions that stem from it.
(Classical Indian Buddhist Philosophy, by John Powers)
This is almost literally from Vasubandhu's Abhidharma-kośa (4th century CE).
So the mental volition (that is - motivation) is the whole point. That makes sense: everything in Buddhism is about consciousness first.
So if you lie to protect jews from the Nazis, to use a modern example - that is not necessarily negative karma. If you had taken lay vows, as a Tibetan Buddhist you would probably do some purification ritual to be sure, but since the motivation is to help someone & you are also preventing people from harming (the nazis) - on balance it is better to lie in such circumstances.
Your example is a bit less clear. I do think it is better to stay silent than to actively lie about your faith. If you have taken lay vows, you have at the very least taken on the attempt not to lie. Staying silent does not break your vow.
The vow to not-lie is also sometimes explained as follows:
Do not distort fact
... Sometimes the use of words can make something seem acceptable, when it could have been said very differently and be totally unacceptable. This is not a lie, but a distortion of fact.
(Essays on Karma, by Katinka Hesselink)