-1

I am trying to load an ansible vault file into an k8 configmap YAML file using Ansible Jinja template but facing an issue with a trailing whitespace getting added at the end of the contents of the YAML file. This is causing errors as below:

Vault format unhexlify error: Odd-length string

Sample of ansible template am using is :

Playbook main.yml -

- name: display multiple files   
  shell: cat /tmp/test.yml   
  register: test

Ansible Jinja Template

apiVersion: v1
data:
 test.yml: |-
     {{ test.stdout.splitlines()|indent(4, false)|trim|replace(' ','') }}
kind: ConfigMap
metadata: 
  name: test
  namespace: test-namespace

test.yml example:

  $ANSIBLE_VAULT;1.1;AES256
  62313365396662343061393464336163383764373764613633653634306231386433626436623361
  6134333665353966363534333632666535333761666131620a663537646436643839616531643561
  63396265333966386166373632626539326166353965363262633030333630313338646335303630
  3438626666666137650a353638643435666633633964366338633066623234616432373231333331
  6564

Output YAML created from Jinja Template is below

apiVersion: v1
data:
 test.yml:
     $ANSIBLE_VAULT;1.1;AES256
  62313365396662343061393464336163383764373764613633653634306231386433626436623361
  6134333665353966363534333632666535333761666131620a663537646436643839616531643561
  63396265333966386166373632626539326166353965363262633030333630313338646335303630
  3438626666666137650a353638643435666633633964366338633066623234616432373231333331
  6564   
kind: ConfigMap
metadata: 
  name: test
  namespace: test-namespace

Can you please let me know what i may be missing in my ansible template file to fix the above trailing whitespace issues.

Amala
  • 161
  • 1
  • 3
  • 13
  • 2
    Please don't post *pictures* of text. Just post the text itself in your question, properly formatted. – larsks Dec 07 '18 at 22:31
  • Please explain what the **problem** is; you have what appears to be a jinja2 template that is generating yaml text, but I don't see what ansible-vault has to do with any of that – mdaniel Dec 08 '18 at 06:43
  • I have edited my question without any pictures and also provided more details on the questions. I am trying to load a Ansible Vault encrypted file into a configmap using jinja2 templating. When doing the same the output ConfigMap has a space added in the end of the encrypted file which is causing corruption of the same and giving me the above ansible vault error. – Amala Dec 10 '18 at 15:38

1 Answers1

2

I am trying to load a Ansible Vault encrypted file into a configmap using jinja2 templating

Then you are solving the wrong problem; let the to_yaml filter do all that escaping for you, rather than trying to jinja your way through it.

- command: cat /tmp/test.yml
  register: tmp_test
- set_fact:
    cm_skeleton:
      apiVersion: v1
      data:
      kind: ConfigMap
      metadata: 
        name: test
        namespace: test-namespace
- copy:
    content: >-
      {{ cm_skeleton | combine({"data":{"test.yml": tmp_test.stdout}}) | to_yaml }}
    dest: /tmp/test.configmap.yml

If you have other things you are trying to template into that ConfigMap, fine, you can still do so, but deserialize in into a dict so you can insert the literal contents of test.yml into the dict and then re-serialize using the to_yaml filter:

- set_fact:
   cm_skeleton: '{{ lookup("template", "cm.j2") | from_yaml }}'
- copy:
   contents: '{{ cm_sketeton | combine({"data"...}) | to_yaml }}'
mdaniel
  • 31,240
  • 5
  • 55
  • 58