0

Team,

I am looping over all hosts in specific inventory group to check the key-value responses from the output of stat on a file but am failing to map it. can anyone hint please how to map it?

      - debug:
          var: result
      - debug:
          msg: "FOUND: /etc/cachefilesd.conf exists..."
        when: result.results.stat.exists
        delegate_to: "{{ item }}"
        with_items: "{{ groups['gpu_node'] }}"

output:

   TASK [services-pre-install-checks : debug] 
 ok: [localhost] => {[0m
     "result": {[0m
         "changed": false, [0m
         "msg": "All items completed", [0m
         "results": [[0m
             {[0m
                 "ansible_loop_var": "item", [0m
                 "changed": false, [0m
                 "invocation": {[0m
                     "module_args": {[0m
                         "checksum_algorithm": "sha1", [0m
                          "path": "/etc/cachefilesd.conf"[0m
                     }                 }, 
                 "item": "hostA", [0m
                 "stat": {[0m
                     "atime": 1573005811.023855, [0m
                      exists": true, [0m


                 }[0m
             }, [0m
             {[0m
                 "ansible_loop_var": "item", [0m
                 "changed": false, [0m
                 "failed": false, [0m
                 "invocation": {[0m
                     "module_args": {[0m
                         "checksum_algorithm": "sha1", [0m

                         "path": "/etc/cachefilesd.conf"[0m
                     }[0m
                 }, [0m
                 "item": "hostB", [0m
                 "stat": {[0m
                     "atime": 1573005811.023855, [0m
                      exists": true, [0m



error:

[localhost]: FAILED! => {"msg": "The conditional check 'result.results.stat.exists' failed. The error was: error while evaluating conditional (result.results.stat.exists): 'list object' has no attribute 'stat'\n\nThe error appears to be in '/home/run_ansible_playbook/tasks/main.yml':
aminography
  • 21,986
  • 13
  • 70
  • 74
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • `result.results` is a list and therefore `result.results.stat` is not defined. (e.g. `result.results[0].stat` is defined if you have a result). – Zeitounator Nov 06 '19 at 07:54
  • that was right. Please post as answer and i will mark it. – AhmFM Nov 06 '19 at 08:06

1 Answers1

1

In your above example, result.results is a list.

You are trying to access result.results.stat: this variable is not defined.

But you can access e.g. result.results[0].stat for the first element. You now need to figure out how you want to loop other those results to achieve your goal.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66