Skip to content

Instantly share code, notes, and snippets.

@redbaron
Last active March 24, 2020 13:06
Show Gist options
  • Save redbaron/45b43c871a08d15e43e2a3a607e0e854 to your computer and use it in GitHub Desktop.
Save redbaron/45b43c871a08d15e43e2a3a607e0e854 to your computer and use it in GitHub Desktop.
Ansible jsonnet filter
- name: Load kube-apiserver.yaml
slurp:
src: "{{tmpconfdir.path}}/etc/kubernetes/manifests/kube-apiserver.yaml"
register: "apiserver_yaml"
# Implement https://github.com/kubernetes/kubeadm/issues/1105#issuecomment-544887733
- name: Patch apiserver healthcheck
vars:
mixin: |
{spec+: {containers: [super.containers[0] + {
livenessProbe+: { httpGet+: { httpHeaders: [
{ name: "Authorization",
value: "Bearer {{healthcheck_token}}" }
]}}
}] + super.containers[1:]}}
copy:
dest: "{{tmpconfdir.path}}/etc/kubernetes/manifests/kube-apiserver.yaml"
content: |
{{ apiserver_yaml.content | b64decode | from_yaml | to_json | jsonnet_mixin(mixin) | from_json | to_yaml }}
# place me int filter_plugins dir next to your playbook
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.errors import AnsibleError, AnsibleFilterError
from ansible.utils.display import Display
display = Display()
try:
import _jsonnet
HAS_LIB = True
except ImportError:
HAS_LIB = False
def jsonnet_mixin(data, mixin):
'''Evaluates Jsonnet '+ mixin' for a given input.
Example:
- debug: msg="{{ '{a: 10}' | jsonnet_mixin('{b: self.a}') }}"
'''
if not HAS_LIB:
raise AnsibleError('You need to install "jsonnet" from pypi')
try:
return _jsonnet.evaluate_snippet("filter_input", data + '+' + mixin)
except Exception as e:
# For older jmespath, we can get ValueError and TypeError without much info.
raise AnsibleFilterError('Error in jsonnet_mixing filter plugin:\n%s' % e)
class FilterModule(object):
''' Query filter '''
def filters(self):
return {
'jsonnet_mixin': jsonnet_mixin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment