community.kubevirt.kubevirt_pvc – Manage PVCs on Kubernetes

Note

This plugin is part of the community.kubevirt collection (version 1.0.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.kubevirt.

To use it in a playbook, specify: community.kubevirt.kubevirt_pvc.

Synopsis

  • Use Openshift Python SDK to manage PVCs on Kubernetes

  • Support Containerized Data Importer out of the box

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.7

  • openshift >= 0.8.2

Parameters

Parameter

Comments

access_modes

list / elements=string

Contains the desired access modes the volume should have.

More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#access-modes

annotations

dictionary

api_key

string

Token used to authenticate with the API. Can also be specified via K8S_AUTH_API_KEY environment variable.

ca_cert

aliases: ssl_ca_cert

path

Path to a CA certificate used to authenticate with the API. The full certificate chain must be provided to avoid certificate validation errors. Can also be specified via K8S_AUTH_SSL_CA_CERT environment variable.

cdi_source

dictionary

If data is to be copied onto the PVC using the Containerized Data Importer you can specify the source of the data (along with any additional configuration) as well as it’s format.

Valid source types are: blank, http, s3, registry, pvc and upload. The last one requires using the community.kubevirt.kubevirt_cdi_upload module to actually perform an upload.

Source data format is specified using the optional content_type. Valid options are kubevirt (default; raw image) and archive (tar.gz).

This uses the DataVolume source syntax: https://github.com/kubevirt/containerized-data-importer/blob/master/doc/datavolumes.md#https3registry-source

client_cert

aliases: cert_file

path

Path to a certificate used to authenticate with the API. Can also be specified via K8S_AUTH_CERT_FILE environment variable.

client_key

aliases: key_file

path

Path to a key file used to authenticate with the API. Can also be specified via K8S_AUTH_KEY_FILE environment variable.

context

string

The name of a context found in the config file. Can also be specified via K8S_AUTH_CONTEXT environment variable.

force

boolean

If set to True, and state is present, an existing object will be replaced.

Choices:

  • no ← (default)

  • yes

host

string

Provide a URL for accessing the API. Can also be specified via K8S_AUTH_HOST environment variable.

kubeconfig

path

Path to an existing Kubernetes config file. If not provided, and no other connection options are provided, the openshift client will attempt to load the default configuration file from ~/.kube/config.json. Can also be specified via K8S_AUTH_KUBECONFIG environment variable.

labels

dictionary

merge_type

list / elements=string

Whether to override the default patch merge approach with a specific type.

This defaults to ['strategic-merge', 'merge'], which is ideal for using the same parameters on resource kinds that combine Custom Resources and built-in resources.

See https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment

If more than one merge_type is given, the merge_types will be tried in order

Choices:

  • json

  • merge

  • strategic-merge

name

string / required

Use to specify a PVC object name.

namespace

string / required

Use to specify a PVC object namespace.

password

string

Provide a password for authenticating with the API. Can also be specified via K8S_AUTH_PASSWORD environment variable.

Please read the description of the username option for a discussion of when this option is applicable.

persist_config

boolean

Whether or not to save the kube config refresh tokens. Can also be specified via K8S_AUTH_PERSIST_CONFIG environment variable.

When the k8s context is using a user credentials with refresh tokens (like oidc or gke/gcloud auth), the token is refreshed by the k8s python client library but not saved by default. So the old refresh token can expire and the next auth might fail. Setting this flag to true will tell the k8s python client to save the new refresh token to the kube config file.

Default to false.

Please note that the current version of the k8s python client library does not support setting this flag to True yet.

The fix for this k8s python library is here: https://github.com/kubernetes-client/python-base/pull/169

Choices:

  • no

  • yes

proxy

string

The URL of an HTTP proxy to use for the connection. Can also be specified via K8S_AUTH_PROXY environment variable.

Please note that this module does not pick up typical proxy settings from the environment (e.g. HTTP_PROXY).

resource_definition

aliases: definition, inline

dictionary

A partial YAML definition of the PVC object being created/updated. Here you can define Kubernetes PVC Resource parameters not covered by this module’s parameters.

NOTE: resource_definition has lower priority than module parameters. If you try to define e.g. metadata.namespace here, that value will be ignored and namespace used instead.

selector

dictionary

A label query over volumes to consider for binding.

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

size

aliases: storage

string

How much storage to allocate to the PVC.

state

string

Determines if an object should be created, patched, or deleted. When set to present, an object will be created, if it does not already exist. If set to absent, an existing object will be deleted. If set to present, an existing object will be patched, if its attributes differ from those specified using module options and resource_definition.

Choices:

  • present ← (default)

  • absent

storage_class_name

string

Name of the StorageClass required by the claim.

More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#class-1

username

string

Provide a username for authenticating with the API. Can also be specified via K8S_AUTH_USERNAME environment variable.

Please note that this only works with clusters configured to use HTTP Basic Auth. If your cluster has a different form of authentication (e.g. OAuth2 in OpenShift), this option will not work as expected and you should look into the k8s_auth module, as that might do what you need.

validate_certs

aliases: verify_ssl

boolean

Whether or not to verify the API server’s SSL certificates. Can also be specified via K8S_AUTH_VERIFY_SSL environment variable.

Choices:

  • no

  • yes

volume_mode

string

This defines what type of volume is required by the claim. Value of Filesystem is implied when not included in claim spec. This is an alpha feature of kubernetes and may change in the future.

volume_name

string

This is the binding reference to the PersistentVolume backing this claim.

wait

boolean

If set, this module will wait for the PVC to become bound and CDI (if enabled) to finish its operation before returning.

Used only if state set to present.

Unless used in conjunction with cdi_source, this might result in a timeout, as clusters may be configured to not bind PVCs until first usage.

Choices:

  • no ← (default)

  • yes

wait_timeout

integer

Specifies how much time in seconds to wait for PVC creation to complete if wait option is enabled.

Default value is reasonably high due to an expectation that CDI might take a while to finish its operation.

Default: 300

Notes

Note

  • The OpenShift Python client wraps the K8s Python client, providing full access to all of the APIS and models available on both platforms. For API version details and additional information visit https://github.com/openshift/openshift-restclient-python

  • To avoid SSL certificate validation errors when validate_certs is True, the full certificate chain for the API server must be provided via ca_cert or in the kubeconfig file.

Examples

- name: Create a PVC and import data from an external source
  community.kubevirt.kubevirt_pvc:
    name: pvc1
    namespace: default
    size: 100Mi
    access_modes:
      - ReadWriteOnce
    cdi_source:
      http:
        url: https://www.source.example/path/of/data/vm.img
      # If the URL points to a tar.gz containing the disk image, uncomment the line below:
      #content_type: archive

- name: Create a PVC as a clone from a different PVC
  community.kubevirt.kubevirt_pvc:
    name: pvc2
    namespace: default
    size: 100Mi
    access_modes:
      - ReadWriteOnce
    cdi_source:
      pvc:
        namespace: source-ns
        name: source-pvc

- name: Create a PVC ready for data upload
  community.kubevirt.kubevirt_pvc:
    name: pvc3
    namespace: default
    size: 100Mi
    access_modes:
      - ReadWriteOnce
    cdi_source:
      upload: yes
    # You need the kubevirt_cdi_upload module to actually upload something

- name: Create a PVC with a blank raw image
  community.kubevirt.kubevirt_pvc:
    name: pvc4
    namespace: default
    size: 100Mi
    access_modes:
      - ReadWriteOnce
    cdi_source:
      blank: yes

- name: Create a PVC and fill it with data from a container
  community.kubevirt.kubevirt_pvc:
    name: pvc5
    namespace: default
    size: 100Mi
    access_modes:
      - ReadWriteOnce
    cdi_source:
      registry:
        url: "docker://kubevirt/fedora-cloud-registry-disk-demo"

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

result

complex

The created, patched, or otherwise present object. Will be empty in the case of a deletion.

Returned: success

api_version

string

The versioned schema of this representation of an object.

Returned: success

duration

integer

elapsed time of task in seconds

Returned: when wait is true

Sample: 48

items

list / elements=string

Returned only when multiple yaml documents are passed to src or resource_definition

Returned: when resource_definition or src contains list of objects

kind

string

Represents the REST resource this object represents.

Returned: success

metadata

complex

Standard object metadata. Includes name, namespace, annotations, labels, etc.

Returned: success

spec

complex

Specific attributes of the object. Will vary based on the api_version and kind.

Returned: success

status

complex

Current status details for the object.

Returned: success

Authors

  • KubeVirt Team (@kubevirt)