unable to evaluate conditional

86 views
Skip to first unread message

fanvalt

unread,
Aug 31, 2016, 5:43:51 AM8/31/16
to Ansible Project
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

fanvalt

unread,
Aug 31, 2016, 6:29:06 AM8/31/16
to Ansible Project
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 ?

Kai Stian Olstad

unread,
Aug 31, 2016, 6:50:55 AM8/31/16
to ansible...@googlegroups.com
On 31. aug. 2016 11:43, fanvalt wrote:
> 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}}"

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

--
Kai Stian Olstad

fanvalt

unread,
Aug 31, 2016, 6:52:27 AM8/31/16
to Ansible Project
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}}"

Le mercredi 31 août 2016 11:43:51 UTC+2, fanvalt a écrit :

fanvalt

unread,
Sep 1, 2016, 4:55:26 AM9/1/16
to Ansible Project, ansible-pr...@olstad.com
ok thank you for the explanation
REgards
Reply all
Reply to author
Forward
0 new messages