What you'll learn
- How to create Kubernetes K8s or OpenShift OCP secret with Ansible?
- Ansible create Kubernetes or OpenShift secret
- Parameters
- Links
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
How to create Kubernetes K8s or OpenShift OCP secret with Ansible?
Ansible create Kubernetes or OpenShift secret
- `kubernetes.core.k8s`
- Manage Kubernetes (K8s) objects
Let's talk about the Ansible module `k8s`.
The full name is `kubernetes.core.k8s`, which means that is part of the collection of modules of Ansible to interact with Kubernetes and Red Hat OpenShift clusters.
It manages Kubernetes (K8s) objects.
Parameters
- name _string_ /namespace _string_ - object name / namespace
- api_version _string_ - "v1"
- kind _string_ - object model
- state _string_ - present/absent/patched
- definition _string_ - YAML definition
- src _path_ - path for YAML definition
- template _raw_ - YAML template definition
- validate _dictionary_ - validate resource definition
There is a long list of parameters of the `k8s` module. Let me summarize the most used.
Most of the parameters are very generic and allow you to combine them for many use-cases.
The `name` and `namespace` specify object name and/or the object namespace. They are useful to create, delete, or discover an object without providing a full resource definition.
The `api_version` parameter specifies the Kubernetes API version, the default is "v1" for version 1.
The `kind` parameter specifies an object model.
The `state` like for other modules determines if an object should be created - `present` option, patched - `patched` option, or deleted - `absent` option.
The `definition` parameter allows you to provide a valid YAML definition (string, list, or dict) for an object when creating or updating.
If you prefer to specify a file for the YAML definition, the `src` parameter provides a path to a file containing a valid YAML definition of an object or objects to be created or updated.
You could also specify a YAML definition template with the `template` parameter.
You might find useful also the `validate` parameter in order to define how to validate the resource definition against the Kubernetes schema. Please note that requires the `kubernetes-validate` python module.
Links
- [kubernetes.core.k8s](https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html)
- [Kubernetes Secrets](https://kubernetes.io/docs/concepts/configuration/secret/)
## Playbook
How to create Kubernetes secret with Ansible Playbook using the module k8s.
Specifically, the following example is going to create the "mysecret" secret in namespace "default" in Kubernetes K8s or OpenShift OCP with Ansible.
code
- ansible_playbook.yml
```yaml
---
- name: k8s Playbook
hosts: localhost
gather_facts: false
connection: local
tasks:
- name: create k8s secret mysecret
kubernetes.core.k8s:
src: mysecret.yaml
state: present
```
- mysecret.yml
The values for all keys in the data the field has to be base64-encoded strings.
username = admin, password = secretpassword
```yaml
---
apiVersion: v1
kind: Secret
metadata:
name: mysecret
namespace: default
type: Opaque
data: