0

I have an vendor Ansible playbook given to us and I will need add an new line to the j2 template and tweak the playbook for our env, I will need to edit the template with an line -> retention_days: {{ xyz }}

This is how the orginal template looks like:

#cat cluster.j2
apiVersion: v1
metadata:
  name: cluster
  cluster_name: {{ my_name }}
data:
  new_image: |+
      baseImage: {{ FROM_repo }}

And here is my Ansible playbook to add the line.

---
- name: mydata
  hosts: localhost
  tasks:
   - name: edit files
     lineinfile:
       dest: cluster.j2
       line: " retention_days: {{ xyz }}"
       insertafter: 'new_image'

My end result ie; my j2 template file should have the exact string like this

retention_days: {{ xyz }}

final - file should look like this ->

#cat cluster.j2
apiVersion: v1
metadata:
  name: cluster
  cluster_name: {{ my_name }}
data:
  new_image: |+
      retention_days: {{ xyz }}
      baseImage: {{ FROM_repo }}

I don't want the {{ xyz }} to be treated as an variable by Ansible instead consider it a string and add them there... How can I escape the {{ and }} Please let me know .

Now ., I get an error: xyz is undefined..

MSG:

***The task includes an option with an undefined variable. The error was: 'xyz' is undefined***
helper
  • 15
  • 1
  • 5

1 Answers1

0

As specified in the documentation, you can use {% raw %} to escape elements in a block, or add additional curly braces.

For example:

---
- name: mydata
  hosts: localhost
  tasks:
   - name: edit files
     lineinfile:
       dest: cluster.j2
       line: " retention_days: {% raw %}{{ xyz }}{% endraw %}"
       # or
       # line: " retention_days: {{ '{{ xyz }}' }}" 
       insertafter: 'new_image'
Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19