Reputation:
Below is the sample ansible playbook I use to check the service status using service_facts module and it worked well. Here I need support to define the service name as variable, but when I define it is giving error, VARIABLE NOT DEFINED:
---
- name: Check service status
hosts: all
gather_facts: no
tasks:
- name: Service Facts
service_facts:
- debug:
var: ansible_facts.services['firewalld.service']['status']
I need "firewalld.service" to be declared under vars section below tasks, tried various options but it is not giving the expected output. I tried below option but it is not working.
---
- name: Check service status
hosts: all
gather_facts: no
vars:
- SERVICE: firewalld.service
tasks:
- name: Service Facts
service_facts:
- debug:
var: ansible_facts.services[SERVICE]['status']
Upvotes: -1
Views: 5694
Reputation: 11
You can run some tasks using conditionals, for example:
- name: Get service_facts
service_facts:
- name: Open some port
firewalld:
port: "{{ some_port }}/tcp"
permanent: yes
immediate: yes
offline: no
state: enabled
when:
- ansible_facts.services['firewalld.service'].state == 'running'
- ansible_facts.services['firewalld.service'].status == 'enabled'
Upvotes: 1
Reputation: 64
To answer your main question: vars
expects a dict, not a list:
---
- name: Check service status
hosts: all
gather_facts: no
vars:
SERVICE: firewalld.service
tasks:
- name: Service Facts
service_facts:
- debug:
msg: "{{ services[SERVICE]['status'] }}"
Additionally you need to use debug:msg
as others have already mentioned.
Upvotes: 0