首页 > 解决方案 > 在 ansible 循环中删除标准输出中返回的 pod

问题描述

团队,

我正在尝试遍历返回的所有主机的 pod 并删除它们。

但我收到错误。不确定我的循环是否正确?我在节点上使用 kubectl 拉动 pod。

输出:

ok: [node0117d] => {
00:11:05      "kos_pod": {
00:11:05          "changed": true, 
00:11:05          "cmd": "kubectl get pods -n kos-vdiskplugin  --no-headers --field-selector spec.nodeName=node0117d | grep -E kos-vdiskplugin-'[[:alnum:]]{5}' -o -w", 
00:11:05          "stdout": "kos-vdiskplugin-fps5b", 
00:11:05          "stdout_lines": [
00:11:05              "kos-vdiskplugin-fps5b"
00:11:05          ]
00:11:05      }
00:11:05  }
00:11:05  ok: [node0118d] => {
00:11:05      "kos_pod": {
00:11:05          "changed": true, 
00:11:05          "cmd": "kubectl get pods -n kos-vdiskplugin  --no-headers --field-selector spec.nodeName=node0118d | grep -E kos-vdiskplugin-'[[:alnum:]]{5}' -o -w", 
00:11:05          "stderr_lines": [], 
00:11:05          "stdout": "kos-vdiskplugin-dz794", 
00:11:05          "stdout_lines": [
00:11:05              "kos-vdiskplugin-dz794"
00:11:05          ]
00:11:05      }
00:11:05  }

任务:

- name: "fetch kos pod on node"
  shell: "kubectl get pods -n kos-vdiskplugin  --no-headers --field-selector spec.nodeName={{ inventory_hostname }} | grep -E kos-vdiskplugin-'[[:alnum:]]{5}' -o -w"
  register: kos_pod
  delegate_to: localhost
  become: false

- debug:
    var: kos_pod

- name: "Delete kos pod on node"
  command: kubectl delete pod -n kos-vdiskplugin "{{ item }}"
  delegate_to: localhost
  become: false
  with_items: "{{ kos_pod.stdout }}"`

错误输出:

fatal: [node0117d -> localhost]: FAILED! => {"changed": true, "cmd": ["kubectl", "delete", "pod", "-n", "kos-vdiskplugin", "{stderr_lines:", "[],", "uchanged:", "True,", "uend:", "u2020-04-30 07:11:05.177457,", "failed:", "False,", "ustdout:", "ukos-vdiskplugin-fps5b,", "ucmd:", "ukubectl get pods -n kos-vdiskplugin  --no-headers --field-selector spec.nodeName=node0117d | grep -E kos-vdiskplugin-'[[:alnum:]]{5}' -o -w,", "urc:", "0,", "ustart:", "u2020-04-30 07:11:04.319947,", "ustderr:", "u,", "udelta:", "u0:00:00.857510,", "stdout_lines:", "[ukos-vdiskplugin-fps5b]}"], "delta": "0:00:02.972829", "end": "2020-04-30 07:11:08.369804", "msg": "non-zero return code", "rc": 1, "start": "2020-04-30 07:11:05.396975", "stderr": "Error from server (NotFound): pods \"{stderr_lines:\" not found\nError from server (NotFound): pods \"[],\" not found\nError from server (NotFound): pods \"uchanged:\" not found\nError from server (NotFound): pods \"True,\" not found\nError from server (NotFound): pods \"uend:\" not found\nError from server (NotFound): pods \"u2020-04-30 07:11:05.177457,\" not found\nError from server (NotFound): pods \"failed:\" not found\nError from server (NotFound): pods \"False,\" not found\nError from server (NotFound): pods \"ustdout:\" not found\nError from server (NotFound): pods \"ukos-vdiskplugin-fps5b,\" not found\nError from server (NotFound): pods \"ucmd:\" not found\nError from server (NotFound): pods \"ukubectl get pods -n kos-vdiskplugin  --no-headers --field-selector spec.nodeName=node0117d | grep -E kos-vdiskplugin-'[[:alnum:]]{5}' -o -w,\" not found\nError from server (NotFound): pods \"urc:\" not found\nError from server (NotFound): pods \"0,\" not found\nError from server (NotFound): pods \"ustart:\" not found\nError from server (NotFound): pods \"u2020-04-30 07:11:04.319947,\" not found\nError from server (NotFound): pods \"ustderr:\" not found\nError from server (NotFound): pods \"u,\" not found\nError from server (NotFound): pods \"udelta:\" not found\nError from server (NotFound): pods \"u0:00:00.857510,\" not found\nError from server (NotFound): pods \"stdout_lines:\" not found\nError from server (NotFound): pods \"[ukos-vdiskplugin-fps5b]}\" not found", "stderr_lines": ["Error from server (NotFound): pods \"{stderr_lines:\" not found", "Error from server (NotFound): pods \"[],\" not found", "Error from server (NotFound): pods \"uchanged:\" not found", "Error from server (NotFound): pods \"True,\" not found", "Error from server (NotFound): pods \"uend:\" not found", "Error from server (NotFound): pods \"u2020-04-30 07:11:05.177457,\" not found", "Error from server (NotFound): pods \"failed:\" not found", "Error from server (NotFound): pods \"False,\" not found", "Error from server (NotFound): pods \"ustdout:\" not found", "Error from server (NotFound): pods \"ukos-vdiskplugin-fps5b,\" not found", "Error from server (NotFound): pods \"ucmd:\" not found", "Error from server (NotFound): pods \"ukubectl get pods -n kos-vdiskplugin  --no-headers --field-selector spec.nodeName=node0117d | grep -E kos-vdiskplugin-'[[:alnum:]]{5}' -o -w,\" not found", "Error from server (NotFound): pods \"urc:\" not found", "Error from server (NotFound): pods \"0,\" not found", "Error from server (NotFound): pods \"ustart:\" not found", "Error from server (NotFound): pods \"u2020-04-30 07:11:04.319947,\" not found", "Error from server (NotFound): pods \"ustderr:\" not found", "Error from server (NotFound): pods \"u,\" not found", "Error from server (NotFound): pods \"udelta:\" not found", "Error from server (NotFound): pods \"u0:00:00.857510,\" not found", "Error from server (NotFound): pods \"stdout_lines:\" not found", "Error from server (NotFound): pods \"[ukos-vdiskplugin-fps5b]}\" not found"], "stdout": "", "stdout_lines": []}

标签: ansibleansible-2.xansible-inventoryansible-facts

解决方案


我的表达有误,导致失败。我的坏我没有看到它,直到我发布问题。我的解决方案工作正常。

with_items: "{{ kos_pod.stdout }}"`

应该

with_items: "{{ kos_pod.stdout }}"

推荐阅读