How to fix a rotation with bones
Enable a complete IK Chain from the top pivot bone to the last moving bone, then, in the Inverse Kinematics bones tab, lock all rotations on the little "Cylinders" bones, and lock all rotations except the axis you need on the big "Cubes" bones (in my example, Z axis, enable axis visualization if needed).