I want to iterate over items, check if fluxcd value exist in any of the items, and if fluxcd doesn't exists I want to execute task using when condition. So far I wrote this:
- name: Check flux status
k8s_facts:
api_version: v1
kind: Namespace
kubeconfig: "{{ kubeconfig if kubeconfig_exists else omit }}"
register: namespace_result
- name: Delete flux
k8s:
state: absent
definition: "{{ lookup('template', 'flux-deletion.yaml') }}"
kubeconfig: "{{ kubeconfig if kubeconfig_exists else omit }}"
wait: True
when: item.metadata.name != "fluxcd"
with_items: "{{ namespace_result.resources }}"
ignore_errors: yes
Output (debug code provided by: Zeitounator):
- debug:
msg: "{{ item.metadata.name }} meets the condition"
with_items: "{{ namespace_result.resources }}"
when: item.metadata.name != "fluxcd"
ok: [localhost] => (item={'metadata': {'name': 'default', 'selfLink': '/api/v1/namespaces/default', 'uid': '9cf3a983-df77-4905-8650-4bd43f1d2e8e', 'resourceVersion': '149', 'creationTimestamp': '2020-01-09T15:48:21Z'}, 'spec': {'finalizers': ['kubernetes']}, 'status': {'phase': 'Active'}, 'apiVersion': 'v1', 'kind': 'Namespace'}) => {
"msg": "default meets the condition"
}
skipping: [localhost] => (item={'metadata': {'name': 'fluxcd', 'selfLink': '/api/v1/namespaces/fluxcd', 'uid': '83b159be-8aeb-45ef-b5b5-5f0b6c8a098e', 'resourceVersion': '1433997', 'creationTimestamp': '2020-01-15T08:18:43Z'}, 'spec': {'finalizers': ['kubernetes']}, 'status': {'phase': 'Active'}, 'apiVersion': 'v1', 'kind': 'Namespace'})
ok: [localhost] => (item={'metadata': {'name': 'kube-node-lease', 'selfLink': '/api/v1/namespaces/kube-node-lease', 'uid': '04199d99-5527-4455-a22f-d42abedf87eb', 'resourceVersion': '35', 'creationTimestamp': '2020-01-09T15:48:18Z'}, 'spec': {'finalizers': ['kubernetes']}, 'status': {'phase': 'Active'}, 'apiVersion': 'v1', 'kind': 'Namespace'}) => {
"msg": "kube-node-lease meets the condition"
}
ok: [localhost] => (item={'metadata': {'name': 'kube-public', 'selfLink': '/api/v1/namespaces/kube-public', 'uid': 'e84d01de-d053-4c28-af8b-fd5b55de4c41', 'resourceVersion': '29', 'creationTimestamp': '2020-01-09T15:48:18Z'}, 'spec': {'finalizers': ['kubernetes']}, 'status': {'phase': 'Active'}, 'apiVersion': 'v1', 'kind': 'Namespace'}) => {
"msg": "kube-public meets the condition"
}
ok: [localhost] => (item={'metadata': {'name': 'kube-system', 'selfLink': '/api/v1/namespaces/kube-system', 'uid': '6b6a50ee-a2f3-4bc7-9342-553a2acd79d2', 'resourceVersion': '16', 'creationTimestamp': '2020-01-09T15:48:18Z'}, 'spec': {'finalizers': ['kubernetes']}, 'status': {'phase': 'Active'}, 'apiVersion': 'v1', 'kind': 'Namespace'}) => {
"msg": "kube-system meets the condition"
}
ok: [localhost] => (item={'metadata': {'name': 'tiller', 'selfLink': '/api/v1/namespaces/tiller', 'uid': 'eb889937-3012-41e8-86d5-f2d4679d23ac', 'resourceVersion': '1467903', 'creationTimestamp': '2020-01-15T11:17:18Z'}, 'spec': {'finalizers': ['kubernetes']}, 'status': {'phase': 'Active'}, 'apiVersion': 'v1', 'kind': 'Namespace'}) => {
"msg": "tiller meets the condition"
}
But when I run this, it seems to me like it will check first field in the list where value is default and it will automatically execute task. I don't have experience with ansible, any help would be appreciated. Thanks.
If you port your condition to the debug
task, you will see that it is skipped when condition is not met:
- debug:
msg: "{{ item.metadata.name }} meets the condition"
with_items: "{{ namespace_result.resources }}"
when: item.metadata.name != "fluxcd"
This will validate how your task loops and checks your condition. You can then replace the debug
module with k8s
and it will skip the exact same way.