Ansible task, how to confirm if a process is actually running?

7,815 views
Skip to first unread message

Howard Lee

unread,
Jun 3, 2016, 1:45:02 PM6/3/16
to Ansible Project

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?

Mike Fennemore

unread,
Jun 3, 2016, 2:25:55 PM6/3/16
to Ansible Project
You could use register: to register a variable for the result then use debug: var= <your registered variable>.<property you want>

Johannes Kastl

unread,
Jun 3, 2016, 3:44:22 PM6/3/16
to ansible...@googlegroups.com
On 03.06.16 20:25 Mike Fennemore wrote:
> 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

signature.asc
Message has been deleted

Howard Lee

unread,
Jun 3, 2016, 6:28:31 PM6/3/16
to Ansible Project
@Johannes That's what I thought, but unfortunately Ansible does not verify if it had successfully ran.

Mike Fennemore

unread,
Jun 4, 2016, 5:52:10 AM6/4/16
to ansible...@googlegroups.com

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.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/-LMHCCDAn0w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5751DDFE.1070601%40ojkastl.de.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jun 4, 2016, 9:28:06 AM6/4/16
to ansible...@googlegroups.com
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.

--
Kai Stian Olstad

Howard Lee

unread,
Jun 7, 2016, 5:12:04 PM6/7/16
to Ansible Project
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)
Reply all
Reply to author
Forward
0 new messages