在这里,我试图打印firewall-cmd --state命令的状态,但是抛出致命错误。
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 }}"
状态在远程主机中“未运行”。但我没有得到结果。
我得到以下输出
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"]}
我应该如何修改代码,以便我只获得状态?
我更喜欢使用failed_when:
来控制你的输出rc。更多信息在Ansible Documentation。但你也可以使用ìgnore_errors: true
检查Firewall-cmd Documentation中的错误代码,看看哪些代码添加到您的剧本中。
在你的场景中可能做得很好:
- name: Check status of firewall
command: firewall-cmd --state
register: status
failed_when:
- status.rc != 0
- status.rc != 252
即使你可以更进一步使用failed_when: false
来避免命令失败。
Baptiste Mille-Mathias建议的ignore_errors将允许你继续,但是你想要“调试”{{status.stderr}},因为ase标准输出将是空的。