Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Last active January 7, 2022 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebuildy/0e38c38fae4c7d2c6094e0462ccf3412 to your computer and use it in GitHub Desktop.
Save ebuildy/0e38c38fae4c7d2c6094e0462ccf3412 to your computer and use it in GitHub Desktop.
Transform data with Ansible / templating
---
- include_vars:
file: "{{ item }}"
name: "__content__"
with_fileglob:
- "/resources/*.yaml"
register: script_files_read
- set_fact:
script_files: "{{ buffer | from_json }}"
vars:
buffer: "{ {% for r in script_files_read.results %}\"{{ r.item | basename | splitext | first }}\" : {{ r.ansible_facts.__content__ | to_json | to_json }}{% if not loop.last %},{% endif %}{% endfor %} }"
- name: Another example, with YAML
vars:
buffer: |+
---
{% for file in (query('fileglob', '/resources/*.yaml', wantlist=True) | sort) %}
{% set r = lookup('template', file) | from_yaml %}
- {{ r | combine({"spec" : r.spec | to_json }) | to_yaml | indent(2) }}
{% endfor %}
set_fact:
script_files_data: "{{ buffer | from_yaml }}"
tags:
- always
- vars:
rules:
- port: 9091 # push gateway
labels: { app: prometheus-pushgateway }
- port: 9093 # alert manager
labels: { app: alertmanager }
- port: 9108 # elasticsearch exporter
labels: { app: prometheus-elasticsearch-exporter }
- port: 9108 # logstash exporter
labels: { app: prometheus-logstash-exporter }
- port: 9304 # logstash exporter
labels: { app: prometheus-logstash-exporter }
- port: 3000 # grafana
labels: { app.kubernetes.io/name: grafana }
- port: 15692 # rabbitmq
labels: { app.kubernetes.io/component: rabbitmq }
buffer: |
---
{% for rule in rules %}
- ports:
- port: {{ rule.port }}
protocol: TCP
selectors:
- podSelector:
matchLabels:
{{ rule.labels | to_yaml | indent(6) }}
{% endfor %}
set_fact:
scrap_egress_rules: "{{ buffer | from_yaml }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment