firewall playbook using when module

I am trying to create a playbook to check if the firewall is running, if so add the rsyslog port. If the firewall is not running, id like to skip the port adding step. Please share some docs. I can’t seem to find any. So far I’ve came up with this…

- name: ensure that firewalld is running

service:

name: “syslog”

state: started

enabled: yes

register: firewalld_status

- debug: msg=“firewalld is running”

when: firewalld_status.stdout.find(“running”) != -1

You say you want to add a port to the firewall if it’s running. But your playbook logic currently first makes sure that the firewall is running (state=started). So the subsequent conditional tasks for when firewalld is NOT running will never be executed.

You have to make a clear decision what you want to do. Either go for ensuring firewalld is running and add that port.
Or only check if it’s running and if it is, add the port.

You can use
https://docs.ansible.com/ansible/latest/modules/service_facts_module.html for this.

BTW I don’t understand what the rsyslog service is needed for… the task name (“Try to restart 1514 if not started” ??) doesn’t match what that task does (ensuring rsyslog is started and enabled).
It also uses a variable called port_check which is unknown, which leads me to believe there are other prior tasks involved?

Dick