Do the grouping $a^{2}b+2a^{2}+ab+2a+b+2=\left(a^{2}b+2a^{2}\right)+\left(ab+2a\right)+\left(b+2\right)$, and factor out $a^{2},a,1$ in each of the groups respectively.
$$a^{2}\left(b+2\right)+a\left(b+2\right)+b+2$$
Factor out common term $b+2$ by using distributive property. Polynomial $a^{2}+a+1$ is not factored since it does not have any rational roots.