The simple answer is "yes" you can use Ansible for monitoring configuration, but you will have to do some extra work. As Vasily stated in their answer, Ansible does not have a built in triggering mechanism, so you need something to trigger the convergence of state. This can be done in several ways, the easiest being probably ansible-pull
. As the docs state, ansible-pull
... inverts the default push architecture of ansible into a pull architecture, which has near-limitless scaling potential.
The setup playbook can be tuned to change the cron frequency, logging locations, and parameters to ansible-pull. This is useful both for extreme scale-out as well as periodic remediation.
emphasis added.
Ansible is idempotent, so it does know about the state of target machines, contrary to what Vasily's answer states - it just doesn't persist it in the same way as Terraform does.
Your real problem does not sound like the question you are posing ("Can Ansible be used to ensure the state of a machine?"), but rather "how can I detect configuration drift and trigger a remediation?"
If you think of this as a monitoring problem, you can reframe the question as "how should I monitor my services in production?". Monitoring implies alarms on changes in state, which trigger actions to remediate. In your case, you would define an alarm on the health check of the service, when that is triggered, execute the playbook which converges to the desired state.
Since you have already invested time in writing the Ansible playbook, it would make sense to re-use it. The simplest would be to use that same playbook to invoke a cron which runs ansible-pull
every 5 minutes or so, depending on your particular case.