代码之家  ›  专栏  ›  技术社区  ›  Red Cricket

用Ansible控制Jinja2模板中的压痕

  •  0
  • Red Cricket  · 技术社区  · 3 年前

    我有这样的剧本:

    (venv) bash-3.2$ cat playbooks/nginx_config_generate.yml
    ---
    - name: Generate Nginx Config
      hosts: all
      gather_facts: False
      roles:
        - ../roles/nginx_config_generate
    

    以下是组成该角色的文件:

    (venv) bash-3.2$ cat roles/nginx_config_generate/tasks/main.yml
    ---
    - name: Generate Nginx Config
      import_tasks: gen_config.yml
    
    (venv) bash-3.2$ cat roles/nginx_config_generate/tasks/gen_config.yml
    - name: Generate Nginx Config File From Template
      ansible.builtin.template:
       src: sp.conf.j2
       dest: "/tmp/nginx.cfg"
    
    (venv) bash-3.2$ cat roles/nginx_config_generate/templates/sp.conf.j2
    something here
    
    # Upstreams
    {% for ups in my_vars.upstreams %}
    {{ ups.entry }}
    {% endfor %}
    
    # HTTP Insecure
    server {
      blha blah blah
      something here = that
      this = that
    
    {% for ups in my_vars.http_locations %}
      {{ ups.entry }}
    {% endfor %}
    
    }
    

    这是我的主机(_V)。。。

    (venv) bash-3.2$ cat inventory/prod/sfo/host_vars/127.0.0.1
    my_vars_str: "host,127.0.0.1,srv1,srv2"
    my_vars:
      upstreams:
        - entry: |
            upstream something {
              foo: blah blah
            }
        - entry: |
            upstream completely {
              foo: blah blah
              different: True
            }
      http_locations:
        - entry: |
           location / {
            set something
           }
        - entry: |
           location /foo {
             set something
             hover: craft
           }
    

    当我运行播放时,它会生成以下文件:

    (venv) bash-3.2$ cat /tmp/nginx.cfg
    something here
    
    # Upstreams
    upstream something {
      foo: blah blah
    }
    
    upstream completely {
      foo: blah blah
      different: True
    }
    
    
    # HTTP Insecure
    server {
      blha blah blah
      something here = that
      this = that
    
      location / {
     set something
    }
    
      location /foo {
      set something
      hover: craft
    }
    
    
    }
    

    正如你所看到的,上面可能是一个有效的nginx配置,但我该如何修复我的 location 行以使其更可读?

    更新:我尝试了卡洛斯的建议,所以我尝试了这个:

    ...
    # HTTP Insecure
    server {
      blha blah blah
      something here = that
      this = that
    
      {% for loc in my_vars.http_locations %}
        {{ loc.entry }}
    
      {% endfor %}
    
    }
    

    但这产生了相同的产出:

    # HTTP Insecure
    server {
      blha blah blah
      something here = that
      this = that
    
          location / {
     set something
    }
    
    
          location /foo {
      set something
      hover: craft
    }
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Red Cricket    3 年前

    好的。我开始玩 indent 在我的模板中筛选。以下是我用来生成所需输出的模板:

    something here
    
    # Upstreams
    {% for ups in my_vars.upstreams %}
    {{ ups.entry }}
    {% endfor %}
    
    # HTTP Insecure
    server {
        blha blah blah
        something here = that
        this = that
    
      {% for loc in my_vars.http_locations %}
      {{ loc.entry | indent(4)}}
      {% endfor %}
    
    }
    

    现在我得到这个输出:

    something here
    
    # Upstreams
    upstream something {
      foo: blah blah
    }
    
    upstream completely {
      foo: blah blah
      different: True
    }
    
    
    # HTTP Insecure
    server {
        blha blah blah
        something here = that
        this = that
    
        location / {
          set something
        }
    
        location /foo {
          set something
          hover: craft
        }
    
    
    }
    
        2
  •  0
  •   Carlos Monroy Nieblas    3 年前

    你试过修改第二个循环中的缩进吗?

    # HTTP Insecure
    server {
      blha blah blah
      something here = that
      this = that
    
      {% for ups in my_vars.http_locations %}
        {{ ups.entry }}
    
      {% endfor %}
    
    }