Off-the-shelf electromagnet with deterministic pole
To change the polarity of the magnetic field, all you have to do is change the polarity of the input current.

However, what you've shown there is a 'pot' magnet, with both poles on the same face. If you want to set up repulsion between two magnets, you'd do better with solenoids, that have a pole at each end. The geometry will be easier, as pot magnets really need to be axially aligned and have similar sizes to repel. Otherwise you end up attracting the metal of the outer shell, regardless of what the inner pole is doing. With a pole at each end, you can use very different sizes, use in conjunction with permanent magnets, use off axis, it's much easier.