anislble with_items with with_nested in loop inside role task
anislble with_items with with_nested in loop inside role task
团队,我需要 运行 通过使用 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
您不能在同一任务上放置 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
.
等变量访问 keys/values
一定要经常查看关于循环的最新文档——它非常好: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 }}" }
团队,我需要 运行 通过使用 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
您不能在同一任务上放置 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 }}" }