0

Here, i am trying to print the status of the firewall-cmd --state command , but a fatal error is being thrown.

 name: Check firewall status
        hosts: st

        tasks:
        - name: Check status of firewall
          command: firewall-cmd --state
          register: status

        - name: Print version
          debug:
             msg: "Status = {{ status.stdout }}"

State is "not running" in the remote host. But am not getting the result.

I get the following output

fatal: [borexample.com]: FAILED! => {"changed": true, "cmd": ["firewall-cmd", "--state"], "delta": "0:00:00.189023", "end": "2018-09-16 11:40:17.319482", "msg": "non-zero return code", "rc": 252, "start": "2018-09-16 11:40:17.130459", "stderr": "", "stderr_lines": [], "stdout": "\u001b[91mnot running\u001b[00m", "stdout_lines": ["\u001b[91mnot running\u001b[00m"]}

How should i modify the code so that i get only the state ?

SaAk
  • 75
  • 2
  • 13
  • the task "Check status of firewall" does not return a return code egal to 0 so it throws an error. If you want to ignore the status you can add `ignore_errors: true` aligned with `register`. – Baptiste Mille-Mathias Sep 16 '18 at 16:12

2 Answers2

0

I prefer using failed_when: to control your output rc. More info at Ansible Documentation. But you can also use ìgnore_errors: true

Check error codes in the Firewall-cmd Documentation to see which codes adding to your playbook.

In your scenario could be good doing:

  - name: Check status of firewall
    command: firewall-cmd --state
    register: status
    failed_when:
      - status.rc != 0 
      - status.rc != 252

Even you can go further and use failed_when: false to avoid the command failing.

imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • when i tried to print using debug: msg:{{status.stdout}}, not getting any output. Firewalld is not running, so the status should be 252. If i give {{status.stderr}} then it gets printed though. – SaAk Sep 16 '18 at 18:44
  • "msg": "\u001b[91mnot running\u001b[00m - status.stderr gives this. but status.stdout doesnt print anything – SaAk Sep 16 '18 at 18:52
  • What do you want to do after that? Are you going to manage something else with the stdout? Please add an example to help. BTW: Sorry for the delay in my answer I was on holidays – imjoseangel Sep 29 '18 at 16:55
0

The ignore_errors suggested by Baptiste Mille-Mathias would allow you to continue, but then you would like to "debug" {{ status.stderr }}, as in that ase stdout would be empty.

carrotcakeslayer
  • 809
  • 2
  • 9
  • 33
  • But what if i have to make a comparison based on the return value. From status.stderr, how can that be done – SaAk Sep 16 '18 at 18:54