Find a smooth function $f \in C^\infty$ such that $A=f^{-1}(0)$ and $B=f^{-1}(1)$ The problem is:

Let $A$ and $B$ disjoint closed subsets of a smooth manifold $M$. Show that there exist a function $f \in C^\infty(M)$ such that $A=f^{-1}(0)$ and $B=f^{-1}(1)$.

I thought this problem could be solved by using the Smooth Urysohn's Lemma, but can't see how to guarantee that $0 < f(x) < 1$ for all $x\in M\backslash (A\cup B)$. Tips?
 A: I originally misread the question and thought you wanted $A\subset f^{-1}(0)$ and $B\subset f^{-1}(1)$. This can be arranged, as I suggested, with a simple partition of unity argument:
Let $\{\phi_i\}$ be a partition of unity subordinate to the open cover $\mathscr U = \{X-A,X-B\}$ of $X$. Let $$\phi_A = \sum_{i: \,\text{supp }\phi_i\subset X-B} \phi_i \qquad\text{and}\qquad \phi_B = \sum_{i:\, \text{supp }\phi_i\subset X-A} \phi_i.$$
Note that $\phi_B(x)=0$ when $x\in A$, $\phi_B(x)=1$ when $x\in B$ (since $\phi_A+\phi_B=1$), and, of course, $0\le\phi_B\le 1$. Thus, the function $f=\phi_B$ will do.
To get the full result you seek we need to be a bit trickier. As @zhw suggested, it will suffice to find a smooth function $g$ with $g^{-1}(0)=A$ and $g\ge 0$. For then we similarly find a smooth function $h$ with $h^{-1}(0)=B$ and $h\ge 0$ and take $f=g/(g+h)$.
Here's a sketch of how you can obtain $g$. Let $\{U_i\}$ be a countable open cover of $M-A$ by sets diffeomorphic to an open ball with the property that sub-balls $U'_i\subset \overline{U'_i}\subset U_i$ also cover $M-A$. Let $g_i\colon M\to [0,1]$ be a smooth function with $g_i>0$ on $U'_i$ and $g_i=0$ on $M-U'_i$. Then the partial derivatives of $g_1,g_2,\dots,g_i$ of all orders $\le i$ are bounded (in magnitude) above on $M$, say, by $k_i>0$. Define
$$g = \sum_i \frac{g_i}{k_i2^i},$$
and check that $g\in C^\infty$  and $g^{-1}(0)=A$.
A: Partial answer: Suppose you can find $f,g\in C^\infty(M)$ such that $f=0$ on $A,$ $f>0$ elsewhere, and $g=0$ on $B,$ $g>0$ elsewhere. Then $f/(f+g) = 0$ on $A,$ $f/(f+g) = 1$ on $B,$ and $0<f/(f+g)<1$ elsewhere.
