1
votes

enter image description hereTeam,

I have my task that pulls hosts from a json_query output stdoud_lines.

Then i am looping over each hostname and performing an ssh connection plus shell commands on each of that host.

Finally, to list the output of each host, i am debugging out the results.[i] for every host. is there a way i can loop on results[i] where i = my dynamic variable having host entry? I want to loop on debug. any hint please?

task: "ssh and run shell command and manually list each host result"

      - name: "RAID mount check for fscache on GPU Nodes"
        shell: ssh -F {{ ssh_cfg_path.stdout }} {{ item.node_name }}.{{ ssh_host }} "df -kh /raid/"
        ignore_errors: no
        register: raid_info
        failed_when: raid_info.rc != 0
        with_items: "{{ gpu_nodes }}"

      - name: raid_info result
        debug:
          var: raid_info

      - name: raid_info results0_stdout_lines
        debug:
          var: raid_info.results[0].stdout_lines
      - name: raid_info results1_stdout_lines
        debug:
          var: raid_info.results[1].stdout_lines

output:


    TASK [team-services-pre-install-checks : raid_info results0_stdout_lines] ****
    Friday 25 October 2019  17:03:07 +0000 (0:00:00.041)       0:00:25.292 ******** 
    ok: [localhost] => {
        "raid_info.results[0].stdout_lines": [
            "Filesystem      Size  Used Avail Use% Mounted on", 
            "/dev/sdb1       7.0T  175G  6.5T   3% /raid"
        ]
    }

    TASK [team-services-pre-install-checks : raid_info results1_stdout_lines] ****
    Friday 25 October 2019  17:03:07 +0000 (0:00:00.040)       0:00:25.333 ******** 
    ok: [localhost] => {
        "raid_info.results[1].stdout_lines": [
            "Filesystem      Size  Used Avail Use% Mounted on", 
            "/dev/sdb1       7.0T  176G  6.5T   3% /raid"
        ]
    }

Below approach does not work or lists any output.


      - name: raid_info results loop over all hosts output/result
        debug:
          var: raid_info.results[{{ item }}].stdout_lines
        with_items: "{{ raid_info }}"

I think i need to use hostvars but am new and not sure how to put up.

1

1 Answers

3
votes

It looks like you were close, and just need to adjust your logic a little here. The with_items is going to loop through a list of whatever you feed it, so if you move more of your variable structure into the with_items list like this:

- name: raid_info results loop over all hosts output/result
  debug:
    msg: "{{ item.stdout }}"
  with_items: "{{ raid_info.results }}"
  loop_control: 
    label: "{{ item.item.node_name }}"

That will iterate over the indexed list that is the results created by the with_items of the previous task.