0

Team, i need to run a task on all nodes by pulling nodes using with_items then using with_nested I need to perferm check on each of the node pulled in with_item. any hint?

I cannot run this from playbook. I have to run it from tasks file inside role.

      - name: verify if kernel modules exists nested loop
        stat:
          path: /lib/modules/{{ kernel_version }}/kernel/{{ item.dir }}/{{ item.module_name }}
          checksum_algorithm: sha1
        register: res
        failed_when: res.stat.checksum != item.sha1
        with_nested:
          - { dir: fs/fscache, module_name: fscache.ko, sha1: "{{ checksum.fscache }}"  }
          - { dir: fs/cachefiles, module_name: cachefiles.ko, sha1: "{{ checksum.cachefiles }}" }
          - { dir: fs/isofs, module_name: isofs.ko, sha1: "{{ checksum.isofs }}" }
          - { dir: drivers/target , module_name: target_core_user.ko, sha1: "{{ checksum.target_core_user }}" }
          - { dir: drivers/target/loopback , module_name: tcm_loop.ko, sha1: "{{ checksum.tcm_loop }}" }
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}"

output:

ERROR! duplicate loop in task: nested

The error appears to be in '/k8s/baremetal/roles/test-services-pre-install-checks/tasks/main.yml': line 166, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


      - name: verify if kernel modules exists nested loop
        ^ here 
AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

1

You cannot put 2 different loops on the same task, however it is still possible to achieve what you want using a single loop statement. Take a look at this example (using a debug statement for simplicity):

    - debug:
        msg: "Server {{ item.0 }}: dir={{ item.1.dir }} module_name={{ item.1.module_name }} checksum={{ item.1.sha1 }}"
      loop: "{{ groups['kube-gpu-node'] | product(dicts) | list }}"
      vars:
        dicts:
          - { dir: fs/fscache, module_name: fscache.ko, sha1: "{{ checksum.fscache }}"  }
          - { dir: fs/cachefiles, module_name: cachefiles.ko, sha1: "{{ checksum.cachefiles }}" }
          - { dir: fs/isofs, module_name: isofs.ko, sha1: "{{ checksum.isofs }}" }
          - { dir: drivers/target , module_name: target_core_user.ko, sha1: "{{ checksum.target_core_user }}" }
          - { dir: drivers/target/loopback , module_name: tcm_loop.ko, sha1: "{{ checksum.tcm_loop }}" }

This loops over all hosts in groups['kube-gpu-node'] and assigns the value to item.0. For each host (i.e. for each "item.0") it loops over all elements of the second list. In this case I have created a task variable called dicts, for readability. These dictionaries are accessible through item.1, meaning you can access the keys/values with variable like item.1.sha1.

Be sure to always check the latest documentation on loops - it is quite good: https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

Edit: Adding original task with modifications

- name: verify if kernel modules exists nested loop
  stat:
    path: /lib/modules/{{ kernel_version }}/kernel/{{ item.1.dir }}/{{ item.1.module_name }}
    checksum_algorithm: sha1
  register: res
  failed_when: res.stat.checksum != item.1.sha1
  delegate_to: "{{ item.0 }}"
  loop: "{{ groups['kube-gpu-node'] | product(dicts) | list }}"
  vars:
    dicts:
      - { dir: fs/fscache, module_name: fscache.ko, sha1: "{{ checksum.fscache }}"  }
      - { dir: fs/cachefiles, module_name: cachefiles.ko, sha1: "{{ checksum.cachefiles }}" }
      - { dir: fs/isofs, module_name: isofs.ko, sha1: "{{ checksum.isofs }}" }
      - { dir: drivers/target , module_name: target_core_user.ko, sha1: "{{ checksum.target_core_user }}" }
      - { dir: drivers/target/loopback , module_name: tcm_loop.ko, sha1: "{{ checksum.tcm_loop }}" }
Matt P
  • 2,452
  • 1
  • 12
  • 15
  • okay: but where is your task failing incase, a case fails? i mean where is failed_when – AhmFM Dec 05 '19 at 00:13
  • Sorry I figured you would adapt my example to suit your code. I have made the changes for you, but please try to understand how it works and what it is doing. I am unable to reproduce or test your code (other than with debug messages), so it will help to have a better understanding of the loop controls for any potential errors that arise from this. – Matt P Dec 05 '19 at 00:21