角色任务内循环中的with_nested与anislble with_items

时间:2019-12-04 23:20:49

标签: ansible ansible-2.x ansible-facts

团队,我需要在所有节点上运行一个任务,方法是使用with_items拉节点,然后使用with_nested,我需要对拉入with_item的每个节点进行性能检查。有什么提示吗?

我无法从剧本中运行它。我必须从角色内部的任务文件中运行它。

      - 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 

1 个答案:

答案 0 :(得分:1)

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

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

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

确保始终检查有关循环的最新文档-很好:https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

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

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