Ansible anislble with_项,with_嵌套在角色任务内的循环中

Ansible anislble with_项,with_嵌套在角色任务内的循环中,ansible,ansible-2.x,ansible-facts,Ansible,Ansible 2.x,Ansible Facts,团队,我需要在所有节点上运行任务,方法是使用with_项拉入节点,然后使用with_嵌套。我需要对使用with_项拉入的每个节点执行ERM检查。有什么提示吗 我不能从剧本上运行这个。我必须从角色内的任务文件运行它 - name: verify if kernel modules exists nested loop stat: path: /lib/modules/{{ kernel_version }}/kernel/{{ item.dir }}

团队,我需要在所有节点上运行任务,方法是使用with_项拉入节点,然后使用with_嵌套。我需要对使用with_项拉入的每个节点执行ERM检查。有什么提示吗

我不能从剧本上运行这个。我必须从角色内的任务文件运行它

      - 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'] }}"
输出:

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 

不能在同一个任务上放置两个不同的循环,但是仍然可以使用单个循环语句实现所需的功能。看看这个例子(为了简单起见,使用调试语句):

这将在
组['kube-gpu-node']
中的所有主机上循环,并将值分配给
项。0
。对于每个主机(即每个“item.0”),它在第二个列表的所有元素上循环。在本例中,为了可读性,我创建了一个名为
dicts
的任务变量。这些字典可以通过
item.1
访问,这意味着您可以使用变量
item.1.sha1
访问键/值

确保始终查看有关循环的最新文档-这非常好:

编辑:添加修改后的原始任务

- 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 }}" }

好的:但是你的任务失败在哪里?如果一个案例失败了?我的意思是失败在哪里?我想你会修改我的例子来适应你的代码。我已经为您做了更改,但是请尝试了解它是如何工作的以及它在做什么。我无法复制或测试您的代码(调试消息除外),因此这将有助于更好地了解循环控件中由此产生的任何潜在错误。
- 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 }}" }