unable to evaluate conditional

Hello,

I don’t understand where I am doing wrong in this simple tasks:

  • name: Search for bin directory
    stat:
    path: /{{ Directoryname }}/{{ item }}/bin
    register: previous
    with_items: “{{ shr4you_inst.stdout_lines }}”
    when: shr4you_inst|success

  • debug: msg=“previous {{previous}}”

  • name: stop karaf
    shell: chdir={{ previous.stat.path }} ./stop
    async: 5
    poll: 5
    ignore_errors: True
    when: previous.stat.isdir

The last When: condition is ALWAYS running with this error: “The error was: unable to evaluate conditional: previous.stat.isdir”

Here is the logfile:

TASK [4YOU : Search for bin directory] **************************************
ok: [integuno] => (item=SHR_4YOU-hra-1.1.0-SNAPSHOT)

TASK [4YOU : debug] ************************************************************
ok: [integuno] => {
“msg”: “previous {‘msg’: u’All items completed’, ‘changed’: False, ‘results’: [{u’stat’: {u’uid’: 54322, u’exists’: True, u’woth’: False, u’mtime’: 1472557536, u’inode’: 21695026, u’isgid’: False, u’size’: 4096, u’wgrp’: False, u’isuid’: False, u’isreg’: False, u’pw_name’: u’integuno’, u’gid’: 54323, u’ischr’: False, u’wusr’: True, u’xoth’: True, u’rusr’: True, u’nlink’: 2, u’issock’: False, u’rgrp’: True, u’gr_name’: u’hr’, u’path’: u’/integuno/DV70_EX/SHR_4YOU-hra-1.1.0-SNAPSHOT/bin’, u’xusr’: True, u’atime’: 1472557622, u’isdir’: True, u’ctime’: 1472634055, u’isblk’: False, u’xgrp’: True, u’dev’: 64768, u’roth’: True, u’isfifo’: False, u’mode’: u’0755’, u’islnk’: False}, u’changed’: False, ‘_ansible_no_log’: False, ‘_ansible_item_result’: True, ‘item’: u’SHR_4YOU-hra-1.1.0-SNAPSHOT’, ‘invocation’: {‘module_name’: u’stat’, u’module_args’: {u’checksum_algorithm’: u’sha1’, u’mime’: False, u’get_checksum’: True, u’follow’: False, u’path’: u’/integuno/DV70_EX/SHR_4YOU-hra-1.1.0-SNAPSHOT/bin’, u’get_md5’: True}}}]}”
}

TASK [4YOU : stop karaf] *******************************************************
fatal: [integuno]: FAILED! => {“failed”: true, “msg”: “The conditional check ‘previous.stat.isdir’ failed. The error was: unable to evaluate conditional: previous.stat.isdir\n\nThe error appears to have been in ‘/home/fvaltat/4YOU/roles/4YOU/tasks/sauvegarde.yml’: line 18, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: stop karaf\n ^ here\n”}
…ignoring

Thanks for your help,
Regards

if I add 2 debug tasks this way , it seems the previous.stat is not defined anymore.

  • debug: msg=“previous {{previous}}”
  • debug: msg=“previous isdir {{previous.stat.isdir}}”
  • debug: msg=“previous path {{previous.stat.path}}”

TASK [4YOU : debug] ************************************************************
ok: [integuno] => {
“msg”: “previous {‘msg’: u’All items completed’, ‘changed’: False, ‘results’: [{u’stat’: {u’uid’: 54322, u’exists’: True, u’woth’: False, u’mtime’: 1472557536, u’inode’: 21695026, u’isgid’: False, u’size’: 4096, u’wgrp’: False, u’isuid’: False, u’isreg’: False, u’pw_name’: u’integuno’, u’gid’: 54323, u’ischr’: False, u’wusr’: True, u’xoth’: True, u’rusr’: True, u’nlink’: 2, u’issock’: False, u’rgrp’: True, u’gr_name’: u’hr’, u’path’: u’/integuno/DV70_EX/SHR_4YOU-hra-1.1.0-SNAPSHOT/bin’, u’xusr’: True, u’atime’: 1472557622, u’isdir’: True, u’ctime’: 1472634055, u’isblk’: False, u’xgrp’: True, u’dev’: 64768, u’roth’: True, u’isfifo’: False, u’mode’: u’0755’, u’islnk’: False}, u’changed’: False, ‘_ansible_no_log’: False, ‘_ansible_item_result’: True, ‘item’: u’SHR_4YOU-hra-1.1.0-SNAPSHOT’, ‘invocation’: {‘module_name’: u’stat’, u’module_args’: {u’checksum_algorithm’: u’sha1’, u’mime’: False, u’get_checksum’: True, u’follow’: False, u’path’: u’/integuno/DV70_EX/SHR_4YOU-hra-1.1.0-SNAPSHOT/bin’, u’get_md5’: True}}}]}”
}

TASK [4YOU : debug] ************************************************************
ok: [integuno] => {
“msg”: “previous isdir {{previous.stat.isdir}}”
}

TASK [4YOU : debug] ************************************************************
ok: [integuno] => {
“msg”: “previous path {{previous.stat.path}}”

Any idea ?

It very hard to read the output using debug this way, please use this instead
- debug: var=previous

If you do you probably will see what's happening a lot easier.

When you use register with with_items the result back is a list.

https://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop

I finally found out how to solve this but I don’t understand why.
I had to use the following syntax to use the register variable, even if the register variable was not containing an “_” in its name:

  • debug: msg=“previous {{previous}}”
  • debug: msg=“previous isdir {{previous.results[0].stat.isdir}}”
  • debug: msg=“previous path {{previous.results[0].stat.path}}”

ok thank you for the explanation
REgards