A test of CP invariance in Higgs boson production via vector-boson fusion in the decay mode is presented. This test uses the Optimal Observable method and is carried out using 36.1\,\mathrm{fb}^{-1} of \sqrt{s}=13\,\mathrm{TeV} proton--proton collision data collected by the ATLAS experiment at the LHC. Contributions from CP-violating interactions between the Higgs boson and electroweak gauge bosons are described by an effective field theory, in which the parameter governs the strength of CP violation. No sign of CP violation is observed in the distributions of the Optimal Observable, and is constrained to the interval [, ] at the 68% confidence level (CL), compared to an expected interval of based upon the Standard Model prediction. No constraints can be set on at 95% CL, while an expected 95% CL interval of for the Standard Model hypothesis was expected.