How to test that a registered variable is not empty?

user3313834 picture user3313834 · Apr 28, 2016 · Viewed 100.6k times · Source

How can I test that stderr is non empty::

- name: Check script
  shell: . {{ venv_name }}/bin/activate && myscritp.py
  args:
    chdir: "{{ home }}"
  sudo_user: "{{ user }}"
  register: test_myscript

- debug: msg='myscritp is Ok'
  when: not test_myscript.stderr

So if there is no error I could read::

  TASK: [deploy | debug msg='critp is Ok] *******
  ok: [vagrant] => {
      "msg": "myscritp is Ok"
  }

In case the stderr is not empty a FATAl error occurs.

Answer

Nasr picture Nasr · Apr 28, 2016

You can check for empty string (when stderr is empty)

- name: Check script
  shell: . {{ venv_name }}/bin/activate && myscritp.py
  args:
    chdir: "{{ home }}"
  sudo_user: "{{ user }}"
  register: test_myscript

- debug: msg='myscritp is Ok'
  when: test_myscript.stderr == ""

If you want to check for fail:

- debug: msg='myscritp has error: {{test_myscript.stderr}}'
  when: test_myscript.stderr != ""

Also look at this stackoverflow question