-2

I'm trying to replace {{ var }} in file and have no idea how to escape curly brackets.

" {{ item.key }} "

of course replaces item.key but ignores {{ }}. Variables to replace in my file look like this:

define ( 'DB_HOST', '{{ DB_HOST }}' );

Part of script:

replace:
dest=env.php.j2
regexp= " {{ item.key }} " << ??????
replace= " {{ item.value }} "

and I need to replace inside of ''. Expected result:

define ( 'DB_HOST', 'hostname' );

Thanks in advance.

Saus
  • 33
  • 5
  • Now the question is: why? You've got Jinja2 templates in Ansible which are exactly for the purpose. Why are you trying to replicate them manually? – techraf Dec 01 '16 at 11:31
  • @techraf At my work, files with variables to replace are written like this and I cannot change this. Have to replace entire {{ var }} – Saus Dec 01 '16 at 11:33
  • 2
    And I am asking: why? Ansible does this out-of-the-box, you don't need to replace anything. – techraf Dec 01 '16 at 11:39

1 Answers1

0

You don't have to replace the variables yourself. Simply use the template module:

template:
  src:  env.php.j2
  dest: "/path/to/final/destination"

and Ansible/Jinja2 will do all the heavy work for you.

dgw
  • 13,418
  • 11
  • 56
  • 54
  • This is done with many folders and locations, that's why first I'm "copy"ing files to proper folders, then using external file script I'm trying to replace values. It was ok till today when I had to use our templates which have {{ }} around vars. As far as I tried your way, template cannot use my dict which is also external file. – Saus Dec 01 '16 at 12:10