11

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).

How do I write a task in a playbook to confirm the process has successfully started?

15

You can check with the failed Jinja2 filter after running your command that checks if the process is running.

Here is an example that uses the output of the command systemctl status apache2 to decide if Apache is running:

- name: Check if Apache is running
  command: systemctl status apache2
  ignore_errors: yes
  changed_when: false
  register: service_apache_status

- name: Report status of Apache
  fail:
    msg: |
      Service apache2 is not running.
      Output of `systemctl status apache2`:
      {{ service_apache_status.stdout }}
      {{ service_apache_status.stderr }}
  when: service_apache_status | failed

If the command of the first task failed, the second task will fail and show why the first task failed.
The return code is stored in service_apache_status.rc.

Example output of a failure:

TASK: [Check if Apache is running] *********************** 
failed: [localhost] => {"changed": false, "cmd": ["systemctl", "status", "apache2"], "delta": "0:00:00.009379", "end": "2016-06-06 15:17:27.827172", "rc": 3, "start": "2016-06-06 15:17:27.817793", "stdout_lines": ["* apache2.service", "   Loaded: not-found (Reason: No such file or directory)", "   Active: inactive (dead)"], "warnings": []}
stdout: * apache2.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)
...ignoring

TASK: [Report status of Apache] ***************************
failed: [localhost] => {"failed": true}
msg: apache2 is not running
systemctl status apache2 output:
* apache2.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

Here is different (albeit possibly less reliable) way, using pgrep, to check if the process is running:

- name: Check if Apache is running
  shell: pgrep apache2
  ignore_errors: yes
  changed_when: false
  register: service_apache_status

- name: Report status of Apache
  fail:
    msg: |
      Service apache2 is not running.
      Return code from `pgrep`:
      {{ service_apache_status.rc }}
  when: service_apache_status.rc != 0
4
  • How does when: service_apache_status | failed works? Does it look for a failed token in service_apache_status?
    – Howard Lee
    Jun 7 '16 at 21:15
  • 2
    @HowardLee: I believe it checks the return code, and if it is not 0, it's considered failed.
    – Deltik
    Jun 7 '16 at 21:17
  • 1
    instead of ps | grep you could try pgrep apache2
    – madeddie
    Mar 29 '19 at 17:12
  • @madeddie: I like it. Answer updated to suggest pgrep!
    – Deltik
    Mar 29 '19 at 22:37
5

This is what I do now:

- 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

failed_when is introduced in 1.4. changed_when: False is used to suppress change status. Read more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.