Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

salt stack - Saltstack/Jinja: How to remove the u' prefix from a list of strings

This is the salt config.sls

{% set testval = ["172.16.49.169:5044", "172.16.51.156:5044"] %}

filebeat_config:
  file.managed:
    - name: /etc/filebeat/filebeat.yml
    - source: salt://filebeat/files/filebeat.yml.tmpl
    - template: jinja
    - user: root
    - mode: 600
    - context:
      logstash_hosts: {{ testval }}

When rendered, the host entry looks like:

hosts: [u'172.16.49.169:5044', u'172.16.51.156:5044']

How do I remove the u' before each string?

I have tried {{ testval|tojson }}, {{ testval|json }} and - encoding: ascii/utf-8


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This issue seems to be related to encoding, as I am getting the configuration rendered as expected with the below example.

My config.sls state file:

{% set testval = ["192.168.1.11:5044", "192.168.1.12:5044"] %}

filebeat_config:
  file.managed:
  - name: /tmp/filebeat.yml
  - source: salt://files/filebeat.yml.j2
  - template: jinja
  - context:
      logstash_hosts: {{ testval }}

The filebeat.yml.j2 template:

hosts: {{ logstash_hosts }}

Renders:

hosts: ['192.168.1.11:5044', '192.168.1.12:5044']

However

Since the Filebeat configuration follows YAML syntax, we can use the YAML list - syntax. We can have a filebeat.yml.j2 template file like:

output.logstash:
  hosts:
  {%- for host in logstash_hosts %}
  - "{{ host }}"
  {%- endfor %} 

Using the same config.sls state, the configuration is rendered as:

output.logstash:
  hosts:
  - "192.168.1.11:5044"
  - "192.168.1.12:5044"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...