k8s - Query the K8s API

New in version 2.5.

Synopsis

  • Uses the OpenShift Python client to fetch a specific object by name, all matching objects within a namespace, or all matching objects for all namespaces.
  • Provides access the full range of K8s APIs.
  • Enables authentication via config file, certificates, password or token.

Requirements

The below requirements are needed on the local master node that executes this lookup.

  • python >= 2.7
  • openshift == 0.4.1
  • PyYAML >= 3.11

Parameters

Parameter Choices/Defaults Configuration Comments
api_key
Token used to authenticate with the API. Can also be specified via K8S_AUTH_API_KEY environment variable.
api_version Default:
v1
Use to specify the API version. If resource definition is provided, the apiVersion from the resource_definition will override this option.
cert_file
Path to a certificate used to authenticate with the API. Can also be specified via K8S_AUTH_CERT_FILE environment variable.
context
The name of a context found in the config file. Can also be specified via K8S_AUTH_CONTEXT environment variable.
field_selector
Specific fields on which to query. Ignored when resource_name is provided.
host
Provide a URL for accessing the API. Can also be specified via K8S_AUTH_HOST environment variable.
key_file
Path to a key file used to authenticate with the API. Can also be specified via K8S_AUTH_HOST environment variable.
kind
required
Use to specify an object model. If resource definition is provided, the kind from a resource_definition will override this option.
kubeconfig
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.
label_selector
Additional labels to include in the query. Ignored when resource_name is provided.
namespace
Limit the objects returned to a specific namespace. If resource definition is provided, the metadata.namespace value from the resource_definition will override this option.
password
Provide a password for authenticating with the API. Can also be specified via K8S_AUTH_PASSWORD environment variable.
resource_definition
Provide a YAML configuration for an object. NOTE: kind, api_version, resource_name, and namespace will be overwritten by corresponding values found in the provided resource_definition.
resource_name
Fetch a specific object by name. If resource definition is provided, the metadata.name value from the resource_definition will override this option.
src
Provide a path to a file containing a valid YAML definition of an object dated. Mutually exclusive with resource_definition. NOTE: kind, api_version, resource_name, and namespace will be overwritten by corresponding values found in the configuration read in from the src file.
Reads from the local file system. To read from the Ansible controller's file system, use the file lookup plugin or template lookup plugin, combined with the from_yaml filter, and pass the result to resource_definition. See Examples below.
ssl_ca_cert
Path to a CA certificate used to authenticate with the API. Can also be specified via K8S_AUTH_SSL_CA_CERT environment variable.
username
Provide a username for authenticating with the API. Can also be specified via K8S_AUTH_USERNAME environment variable.
verify_ssl
    Choices:
  • no
  • yes
Whether or not to verify the API server's SSL certificates. Can also be specified via K8S_AUTH_VERIFY_SSL environment variable.

Notes

Note

Examples

- name: Fetch a list of namespaces
  set_fact:
    projects: "{{ lookup('k8s', api_version='v1', kind='Namespace') }}"

- name: Fetch all deployments
  set_fact:
    deployments: "{{ lookup('k8s', kind='Deployment', namespace='testing') }}"

- name: Fetch all deployments in a namespace
  set_fact:
    deployments: "{{ lookup('k8s', kind='Deployment', namespace='testing') }}"

- name: Fetch a specific deployment by name
  set_fact:
    deployments: "{{ lookup('k8s', kind='Deployment', namespace='testing', resource_name='elastic') }}"

- name: Fetch with label selector
  set_fact:
    service: "{{ lookup('k8s', kind='Service', label_selector='app=galaxy') }}"

# Use parameters from a YAML config

- name: Load config from the Ansible controller filesystem
  set_fact:
    config: "{{ lookup('file', 'service.yml') | from_yaml }}"

- name: Using the config (loaded from a file in prior task), fetch the latest version of the object
  set_fact:
    service: "{{ lookup('k8s', resource_definition=config) }}"

- name: Use a config from the local filesystem
  set_fact:
    service: "{{ lookup('k8s', src='service.yml') }}"

Return Values

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

Key Returned Description
_list
complex
One ore more object definitions returned from the API.

status
complex
success
Current status details for the object.

kind
str
success
Represents the REST resource this object represents.

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

api_version
str
success
The versioned schema of this representation of an object.

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



Status

Author

  • UNKNOWN

Hint

If you notice any issues in this documentation you can edit this document to improve it.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.5/plugins/lookup/k8s.html