Ansible 2.1
In the playbook, I started a process:
- name: Start Automation Agent, and enable start on boot
service: name=mongodb-mms-automation-agent state=started enabled=yes
From play recap, it appears the process has successfully started.
TASK [install : Start automation agent, and enable start on boot] **************
changed: [server1]
However, when log in to remote host and do a ps
, the process is not running. Checking on process log it had failed some pre-requisite (intended for testing).
How do I write a task in a playbook to confirm the process has successfully started?
You could use register: to register a variable for the result then use debug: var= <your registered variable>.<property you want>
But if starting the service fails, shouldn't ansible report the task
as failed?
Johannes
@Johannes That’s what I thought, but unfortunately Ansible does not verify if it had successfully ran.
Indeed it will. If that is a possibility then I add ignore_errors:true and still register a variable for the result. The variable will then give you a status and msg if the service fails.
Yes, and Ansible would do that, but the problem is that the init script is reporting everything is OK when it isn't.
So you have some options,
- fix the init script or,
- use ansible shell or command to check if the process is running or,
- use the service command a second time and fail it if the second one also report changed.
This is what I do now. Ansible 1.4 has “failed_when”. http://docs.ansible.com/ansible/playbooks_error_handling.html#controlling-what-defines-failure
- name: Confirm Automation Agent is running
command: service mongodb-mms-automation-agent status
register: agent_status
failed_when: “‘NOT’ in agent_status.stdout”
changed_when: False
(Added “changed_when: False” to suppress “changed” status)