简体   繁体   English

使用ansible jinja2循环时如何修剪最后一个字符

[英]How to trim last character when using ansible jinja2 loop

my hosts config我的主机配置

 [elasticsearch] 192.168.2.65 es_node_roles="master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform" 192.168.2.66 es_node_roles="master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform" 192.168.2.67 es_node_roles="master, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform" 192.168.2.77 es_node_roles="master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform"

My template like as blow我的模板就像打击一样

 aaaaa: '{% for i in elasticsearch_hosts_array %}{% if hostvars[i].es_node_roles | regex_search("data_hot") %}"{{i}}",{% endif %}{% endfor %}'

But the results:"192.168.2.65","192.168.2.66","192.168.2.77", I want to get rid of the last punctuation I want is the results:"192.168.2.65","192.168.2.66","192.168.2.77"但是结果:"192.168.2.65","192.168.2.66","192.168.2.77",我想去掉最后一个标点符号我想要的是结果:"192.168.2.65","192.168.2.66"," 192.168.2.77"

If I write it another way如果我用另一种方式写

 {% for i in elasticsearch_hosts_array %}{% if loop.index0>0 %},{% endif %}{% if hostvars[i].es_node_roles | regex_search("data_hot") %}"{{i}}"{% endif %}{% endfor %}'

the results:"192.168.2.65","192.168.2.66",,"192.168.2.77"结果:“192.168.2.65”、“192.168.2.66”、“192.168.2.77”

I want is the results:"192.168.2.65","192.168.2.66","192.168.2.77"我要的是结果:"192.168.2.65","192.168.2.66","192.168.2.77"

If you want to use the Jinja template the playbook below如果你想使用 Jinja 模板下面的剧本

- hosts: all
  vars:
    a: |-
      {% for i in ansible_play_hosts_all %}
      {% if 'data_hot' in hostvars[i].es_node_roles %}
      "{{ i }}"{%if not loop.last %},{% endif %}{% endif %}
      {% endfor %}
  tasks:
    - debug:
        var: a
      run_once: true

gives

 a: '"192.168.2.65","192.168.2.66","192.168.2.77"'

The next option is to create the list in a variable first.下一个选项是首先在变量中创建列表。 Use special variable ansible_play_hosts_all .使用特殊变量ansible_play_hosts_all When you run a playbook with - hosts: all this variable will keep the list of all hosts当您使用 -hosts 运行剧本时- hosts: all此变量将保留所有主机的列表

  ansible_play_hosts_all: [192.168.2.65, 192.168.2.66, 192.168.2.67, 192.168.2.77]

Create a list of all instances of es_node_roles创建所有es_node_roles实例的列表

  nr_list: "{{ ansible_play_hosts_all|
               map('extract', hostvars, 'es_node_roles')|
               list }}"

gives

  nr_list:
  - master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform
  - master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform
  - master, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform
  - master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform

Use this list and create a dictionary使用此列表并创建字典

  nr_dict: "{{ dict(ansible_play_hosts_all|zip(nr_list)) }}"

gives

  nr_dict:
    192.168.2.65: master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform
    192.168.2.66: master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform
    192.168.2.67: master, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform
    192.168.2.77: master, data_hot, data_content, data_warm, data_cold, data_frozen, ingest, ml, remote_cluster_client, transform

Now, you can use this dictionary and find hosts where the list contains data_hot现在,您可以使用此字典并查找列表包含data_hot的主机

  nodes_data_hot: "{{ nr_dict|dict2items|
                      selectattr('value', 'contains', 'data_hot')|
                      map(attribute='key')|
                      list }}"

gives

  nodes_data_hot: [192.168.2.65, 192.168.2.66, 192.168.2.77]

Create a string with comma-separated items创建带有逗号分隔项的字符串

  a: "{{ nodes_data_hot|join(',') }}"

gives

  a: 192.168.2.65,192.168.2.66,192.168.2.77

You can wrap the items with double-quotes您可以用双引号包裹项目

  b: "{{ nodes_data_hot|map('regex_replace', b_regex, b_replace)|join(',') }}"
  b_regex: '^(.*)$'
  b_replace: '"\1"'

gives

  b: '"192.168.2.65","192.168.2.66","192.168.2.77"'

Example of a complete playbook完整剧本示例

- hosts: all gather_facts: false vars: nr_list: "{{ ansible_play_hosts_all| map('extract', hostvars, 'es_node_roles')| list }}" nr_dict: "{{ dict(ansible_play_hosts_all|zip(nr_list)) }}" nodes_data_hot: "{{ nr_dict|dict2items| selectattr('value', 'contains', 'data_hot')| map(attribute='key')| list }}" a: "{{ nodes_data_hot|join(',') }}" b: "{{ nodes_data_hot|map('regex_replace', b_regex, b_replace)|join(',') }}" b_regex: '^(.*)$' b_replace: '"\1"' tasks: - debug: var: a run_once: true - debug: var: b run_once: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM