I want to make sure I am going about this in the cleanest and easiest way possible. Maybe someone else has done this before or has some better ideas for me :)?
When a service is restarted i.e. "service networking restart", I want to check for stderr and stdout to make sure there were no issues with the ansible push.
------------------------------
Here is the Playbook/Handlers:
---
- hosts: spine1
user: root
gather_facts: false
tasks:
- name: copy interfaces file
copy: src=interfaces dest=/etc/network/interfaces
notify: reload networking
- name: copy quagga file
copy: src=quagga dest=/etc/quagga/Quagga.conf
notify: reload quagga
handlers:
- name: reload networking
shell: "service networking reload"
register: networking_reload
notify: print networking reload error
changed_when: "networking_reload.stderr != ''"
- name: reload quagga
shell: "service quagga reload"
register: quagga_reload
notify: print quagga reload error
changed_when: "quagga_reload.stderr != ''"
- name: print networking reload error
debug: msg="{{networking_reload.stderr}}"
when: networking_reload.stderr is defined
failed_when: "networking_reload.stderr is defined"
- name: print quagga reload error
debug: msg="{{quagga_reload.stderr}}"
when: quagga_reload.stderr is defined
failed_when: "quagga_reload.stderr is defined"
---------------
Tested scenerios:
config file is different with error:
root@wbench:~/consulting/testing# ansible-playbook test.yml --diff
PLAY [spine1] *****************************************************************
TASK: [copy interfaces file] **************************************************
changed: [spine1]
TASK: [copy quagga file] ******************************************************
ok: [spine1]
NOTIFIED: [reload networking] *************************************************
changed: [spine1]
NOTIFIED: [print networking reload error] *************************************
failed: [spine1] => {"failed": true, "failed_when_result": true, "verbose_always": true}
msg: error: /etc/network/interfaces: iface swp52: unsupported keyword (this_should_not_be_here,_it_makes_no_sense._GAH)
FATAL: all hosts have already failed -- aborting
config file is different with no error
root@wbench:~/consulting/testing# ansible-playbook test.yml --diff
PLAY [spine1] *****************************************************************
TASK: [copy interfaces file] **************************************************
changed: [spine1]
TASK: [copy quagga file] ******************************************************
ok: [spine1]
NOTIFIED: [reload networking] *************************************************
ok: [spine1]
config file is the same with error:
(no change to handler isn't called) (only way around this is to ifreload on every run)
config file is the same with no error:
root@wbench:~/consulting/testing# ansible-playbook test.yml --diff
PLAY [spine1] *****************************************************************
TASK: [copy interfaces file] **************************************************
ok: [spine1]
TASK: [copy quagga file] ******************************************************
ok: [spine1]
------------------------------------------------------------------------------------------------------------------------------------
It looks like the service module does not actually return stdout/stderr.
Here is a run using the service module and just printing the returned var:
ok: [spine1] => {
"var": {
"networking_reload": {
"changed": true,
"invocation": {
"module_args": "name=networking state=reloaded",
"module_name": "service"
},
"name": "networking",
"state": "started"
}
}
}