2. Find the range of real number $a$ such that for any real number $x$ and any $\theta \in\left[0, \frac{\pi}{2}\right]$, the inequality $(x+3+2 \sin \theta \cos \theta)^{2}+(x+a \sin \theta+a \cos \theta)^{2} \geqslant \frac{1}{8}$ always holds.
By Cauchy-Schwarz inequality, we have $(x+3+2 \sin \theta \cos \theta)^{2}+(x+a \sin \theta+a \cos \theta)^{2}$
$$
\begin{array}{l}
\geqslant \frac{1}{2}(3+2 \sin \theta \cos \theta-a \sin \theta-a \cos \theta)^{2} \\
=\frac{1}{2}\left[(\sin \theta+\cos \theta)^{2}-a(\sin \theta+\cos \theta)+2\right]^{2} \xlongequal{t=\sin \theta+\cos \theta \in[1, \sqrt{2}]} \frac{1}{2}\left(t^{2}-a t+2\right)^{2} \\
\Rightarrow\left(t^{2}-a t+2\right)^{2} \geqslant \frac{1}{4} \Rightarrow t^{2}-a t+2 \geqslant \frac{1}{2} \text { or } t^{2}-a t+2 \leqslant-\frac{1}{2}
\end{array}
$$
$\Rightarrow a \leqslant t+\frac{3}{2 t}$ or $a \geqslant t+\frac{5}{2 t}$ for all $t \in[1, \sqrt{2}]$,
Since $t+\frac{3}{2 t} \geqslant 2 \sqrt{\frac{3}{2}}=\sqrt{6}$, equality holds when $t=\frac{\sqrt{6}}{2}$, and $y=t+\frac{5}{2 t}$ is monotonically decreasing on $t \in[1, \sqrt{2}]$ $\Rightarrow t+\frac{5}{2 t} \leqslant \frac{7}{2}$, equality holds when $t=1$.
Therefore, the range of real number $a$ is $a \leqslant \sqrt{6}$ or $a \geqslant \frac{7}{2}$.