Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 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 Answer

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...