The CP properties of the coupling between the Higgs boson and the top quark are investigated using 139 fb−1 of proton–proton collision data recorded by the ATLAS experiment at the LHC at a centre-of-mass energy of s√=13𝑠=13 TeV. The CP structure of the top quark–Higgs boson Yukawa coupling is probed in events with a Higgs boson decaying into a pair of b-quarks and produced in association with either a pair of top quarks, tt¯H𝑡𝑡¯𝐻, or a single top quark, tH. Events containing one or two electrons or muons are used for the measurement. Multivariate techniques are used to select regions enriched in tt¯H𝑡𝑡¯𝐻 and tH events, where dedicated CP-sensitive observables are exploited. In an extension of the Standard Model (SM) with a CP-odd admixture in the top–Higgs Yukawa coupling, the mixing angle between CP-even and CP-odd couplings is measured to be α=11∘+52∘−73∘𝛼=11−73∘∘+52∘, compatible with the SM prediction corresponding to α=0𝛼=0.