<p>I need to enable istio injection in Ansible. My command is, </p>

<p><code>kubectl label namespace default istio-injection=enabled</code></p>

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

<pre><code>  k8s:
    name: default
    kind: Namespace
    label: istio-injection=enabled
</code></pre>

<p>How to do this?</p>

<p>You can't use Ansible to modify a Kubernetes object in-place (with the exception that you can <a href="https://docs.ansible.com/ansible/latest/modules/k8s_scale_module.html#k8s-scale-module" rel="nofollow noreferrer"><code>k8s_scale</code></a> a deployment).  The flip side of this is that a <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#namespace-v1-core" rel="nofollow noreferrer">Namespace</a> is an ordinary Kubernetes object, so you can use <a href="https://docs.ansible.com/ansible/latest/modules/k8s_module.html#k8s-module" rel="nofollow noreferrer"><code>k8s</code></a> 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 <em>all</em> of the namespace labels, if you have more than just the Istio label.)</p>

<pre class="lang-yaml prettyprint-override"><code>k8s:
  state: present
  definition:
    apiVersion: v1
    kind: Namespace
    metadata:
      name: default
      labels:
        'istio-injection': enabled
</code></pre>
