We find a class of cylindrically symmetric, static electrovacuum spacetimes generated by a nonhomogeneous magnetic field and involving the cosmological constant and one additional parameter, which determine uniquely the strength of the magnetic field. We provide a simple model of a source producing the field.