0

Team,

I am trying to loop over the pods from all hosts returned and delete them.

but am getting error. not sure if my loop is right? am pulling pods with kubectl on node.

output:

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  }

task:

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

error output:

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": []}
AhmFM
  • 1,552
  • 3
  • 23
  • 53

2 Answers2

0

There was a typo in my expression and that caused failure. my bad i did not see it untill i posted question. my solutions works fine.

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

should be

with_items: "{{ kos_pod.stdout }}"
AhmFM
  • 1,552
  • 3
  • 23
  • 53
-1

with_items expects a list, but you have given it a str

You'll want:

with_items: "{{ kos_pod.stdout_lines }}"`
mdaniel
  • 31,240
  • 5
  • 55
  • 58