在ansible循环中删除stdout中返回的pods

问题描述 投票:-1回答:2

团队,

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

但出现错误。不知道我的循环是否正确?在节点上用kubectl拉吊舱。

输出:

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": []}
ansible ansible-2.x ansible-inventory ansible-facts
2个回答
0
投票

我的表情中有错别字,导致失败。我不好,直到我发布问题,我才看到它。我的解决方案效果很好。

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

应该是

with_items: "{{ kos_pod.stdout }}"

-1
投票

[with_items需要一个list,但您给了它一个str

您想要:

with_items: "{{ kos_pod.stdout_lines }}"`
© www.soinside.com 2019 - 2024. All rights reserved.