Multiline YAML Inserting \n in Template

1,225 views
Skip to first unread message

jaipepe

unread,
Jan 5, 2017, 11:45:43 AM1/5/17
to Salt-users
I am trying to define a multiline yaml string in a defaults.yml file, but the newlines in that string are interpretting to \n instead of actual new lines in my template.

I have defined a multiline yaml string in a defaults.yml file. This is just the relevant code:


- Exec: |


             $Message = $raw_event;


             $raw_omsg=$raw_event;


             parse_syslog_ietf($Message);


             $meta_sip4 =$MessageSourceAddress;





In my state, I am passing this into my jinja like the below:



{
% import_yaml "nxlog/defaults.yml" as default_settings %}

{% set nxlog_settings = salt['pillar.get']('nxlog', default_settings.nxlog, merge=True) %}



nxlog_config
:


  file.managed:


    - name: /opt/nxlog/conf/nxlog.conf


    - source: salt://nxlog/files/nxlog.config.jinja


    - template: jinja


    - defaults:


        nxlog_settings: {{ nxlog_settings }}



I then put the data into the jinja template like this excerpt below. There are loops that I didn't include that get the proper data.

{{ exec_code }}

The issue that I am seeing on the minion side once the template is rendered is that the string is all one line with \n for newlines instead of actual new lines, like so:

 
  $Message
= $raw_event;\n$raw_omsg=$raw_event;\nparse_syslog_ietf($Message);\n$meta_sip4 =$MessageSourceAddress;\n


But I am trying to achive:

$Message = $raw_event;
$raw_omsg=$raw_event;
parse_syslog_ietf($Message);
$meta_sip4 =$MessageSourceAddress;

Is there something that I am doing wrong or should be doing differently?
 

Loren Gordon

unread,
Jan 5, 2017, 2:49:45 PM1/5/17
to Salt-users
Try using multiline syntax to reference the `nxlog_settings` variable. I.e.:


{% import_yaml "nxlog/defaults.yml" as default_settings %}
{% set nxlog_settings = salt['pillar.get']('nxlog', default_settings.nxlog, merge=True) %}

nxlog_config:
  file.managed:
    - name: /opt/nxlog/conf/nxlog.conf
    - source: salt://nxlog/files/nxlog.config.jinja
    - template: jinja
    - defaults:
        nxlog_settings: |
          {{ nxlog_settings | indent(10) }}

IIRC, the "indent" number should match the number of spaces needed to indent the line so it is recognized by yaml, 10 in this case.

-Loren

jaipepe

unread,
Jan 13, 2017, 9:51:34 AM1/13/17
to Salt-users
Hi Loren, thanks for the reply. I got caught up in other tasks, so I am just now able to revisit this. This approach fails with the following:

AttributeError: 'dict' object has no attribute 'splitlines'


I am going to try using the indent within my template to see if I can achieve the desired results.

Reply all
Reply to author
Forward
Message has been deleted
0 new messages