Edit kubectl namespace label with ansible

1/23/2020

I need to enable istio injection in Ansible. My command is,

kubectl label namespace default istio-injection=enabled

I want to run this command in Ansible. I used the ansible k8s module for this task. But it keeps errors.

  k8s:
    name: default
    kind: Namespace
    label: istio-injection=enabled

How to do this?

--
ansible
istio
kubernetes

1 Answer

1/23/2020

You can't use Ansible to modify a Kubernetes object in-place (with the exception that you can k8s_scale a deployment). The flip side of this is that a Namespace is an ordinary Kubernetes object, so you can use k8s to create or update it. There's no facility to just specify the object metadata so you need to give a fairly complete object description. (You would need to know all of the namespace labels, if you have more than just the Istio label.)

k8s:
  state: present
  definition:
    apiVersion: v1
    kind: Namespace
    metadata:
      name: default
      labels:
        'istio-injection': enabled
-- David Maze
Source: StackOverflow