AhmFM
AhmFM

Reputation: 1782

ansible iterate on results from multiple hosts to pull specific key value

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':

Upvotes: 0

Views: 213

Answers (1)

Zeitounator
Zeitounator

Reputation: 44605

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.

Upvotes: 1

Related Questions