Skip to content

Added create_only param to ignore existing objects #884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dcraigmv
Copy link

SUMMARY

Instead of checking for existing resources before creating a new resource to avoid modifying it each time e.g.

k8s_info:
  kind: Secret
  name: my-auto-generated-psk
  namespace: project-1
register: existing_secret

k8s:
  state: present
  definition:
    kind: Secret
    metadata:
      name: my-auto-generated-psk
      namespace: project-1
    data:
      admin: "{{ lookup('community.general.random_string', length=15, base64=true) }}"
when: existing_secret.resources == []
register: new_secret

set_fact:
  # check for existing and new resource, consolidate into one fact for later use
  admin_psk: "{{ (existing_secret.resources | default([new_secret.result])) | first }}"

This new create_only param reduces this to a single task

k8s:
  state: present
  create_only: true
  definition:
    kind: Secret
    metadata:
      name: my-auto-generated-psk
      namespace: project-1
    data:
      admin: "{{ lookup('community.general.random_string', length=15, base64=true) }}"
register: admin_psk

Otherwise this task would recreate the secret's data each time. This is especially useful with register as the original resource can be used in subsequent tasks without checking for existing or new. It is also beneficial in ansible operators.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

k8s create_only param

ADDITIONAL INFORMATION

A couple of unit test cases added

Copy link

@dcraigmv
Copy link
Author

immutable causes a failure, but this can be used with immutable to avoid that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant