Skip to content

kubectl apply tasks report changes on every run #13033

@guoard

Description

@guoard

What happened?

Several Ansible tasks that invoke kubectl apply are currently marked as changed on every execution, even when the applied Kubernetes resources are already in the desired state. This breaks idempotency expectations and causes noisy or misleading change reporting.

What did you expect to happen?

The task should only be marked as changed when Kubernetes resources are actually:

  • created
  • configured (modified)

and not when:

  • unchanged

Proposed improvement:

Using kubectl diff

- name: Check manifest drift
  command: kubectl diff -f manifests.yaml
  register: kubectl_diff
  failed_when: kubectl_diff.rc > 1
  changed_when: false

- name: Apply manifests when drift detected
  command: kubectl apply -f manifests.yaml
  when: kubectl_diff.rc == 1

Using kubectl apply status

- name: Apply manifests like before
  command: kubectl apply -f manifests.yaml
  register: kubectl_apply
  changed_when: "' created' in kubectl_apply.stdout or ' configured' in kubectl_apply.stdout"

Metadata

Metadata

Assignees

Labels

Debian 12kind/bugCategorizes issue or PR as related to a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions