I have a task that checks the redis service status on the host list below
- hosts: 192.168.0.1, 192.168.0.2, 192.168.0.3
tasks:
- command:
cmd: service redis-server status
register: result
- debug:
var: result
After checking I need to access hosts where service does not exist.
And they should be accessible as variable to proceed with them in the next tasks.
Can someone please help?
2
Answers
Finally found the solution that perfectly matches.
Similar to Ansible facts it is also possible to gather
service_facts
. In exampleIf you like to perform tasks after on a service of which you don’t the status, with Conditionals you can check the state.
If the service is not installed at that time, the specific variable (key) would not be defined. You would perform Conditionals based on variables then
Also it is recommend to use the Ansible
service
module to perform tasks on the serviceinstead of using the
command
module.Further services related Q&A
service_facts
module in an Ansible playbook?ansible_facts
?