I have had trouble using that as well, both for enabling and disabling readonly mode. The last time I ended up running Discourse.enable_readonly_mode
in rails.
It’s always an emergency when it happens and I haven’t investigated what the issue might be.
FWIW, in my own Ansible tooling I don’t use read-only mode, but do use Introducing Post Deployment Migration