Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving contents of j2 template file on stdout

Tags:

ansible

jinja2

I'm attempting to use Ansible to better manage my Kubernetes configmaps in a multienvironment project (dev, stage, and prod). I've generalized each of the config maps as j2 templates, and I'll override the variables depending on how they might change in different environments (so that they aren't duplicated three times for basically the same file).

My playbook currently looks something like this:

---
- hosts: localhost
  vars_files:
    - "vars/{{ env }}.yml"
  tasks:
    - name: Generate YAML from j2 template
      template:
        src: templates/foo.j2
        dest: output/foo.yml

And this has been working great for testing so far. However, I'm at the point where I want to incorporate this into my already existing Jenkins CI/CD, but I'm having trouble understanding how it might work with what I am doing currently.

After generating what is basically a Kuberenets ConfigMap from the j2, I'll somehow do this within Jenkins:

kubectl apply -f <yaml>

However, the playbook is creating a YAML file every time I run it, and I am wondering if there is an alternative that would allow me to pipe the contents of the YAML file or somehow retrieve it from stdout.

Basically, I want to evaluate the template and retrieve it without necessarily creating a file.

If I do this, I could do something like the following:

echo result | kubectl apply -f -

where result of course is the contents of the YAML file that results after the templating, and the short dash after the f flag specifies Kubernetes to use the process' stdout.

Sorry for so much explaining, I can clarify anything if needed.

like image 883
Andys1814 Avatar asked Nov 19 '25 23:11

Andys1814


1 Answers

I would like to retrieve the result of the template, and pipe it into that command, such as "echo result | kubectl apply -f -"

In which case, you'd use the stdin: parameter of the command: module:

- name: generate kubernetes yaml
  command: echo "run your command that generates yaml here"
  register: k8s_yaml

- name: feed the yaml to kubectl apply
  command: kubectl apply -f -
  stdin: '{{ k8s_yaml.stdout }}'

It isn't super clear what the relationship is in your question between the top part, dealing with the template:, and the bottom part about apply -f -, but if you mean "how can I render a template to a variable, instead of a file?" the the answer is the template lookup plugin:

- name: render the yaml
  set_fact:
    k8s_yaml: '{{ lookup("template", "templates/foo.j2") }}'

- name: now feed it to apply
  command: kubectl apply -f -
  stdin: '{{ k8s_yaml }}'
like image 127
mdaniel Avatar answered Nov 24 '25 22:11

mdaniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!