skip to Main Content

I have this playbook:

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

Here is the files that make up the role:

(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 %}

}

Here are my host_vars …

(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
       }

When I run my play it produces this file:

(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
}


}

As you can see the above is probably a valid nginx config but how can I fix up the indentation on my location lines to make it more readable?

UPDATE: I tried Carlos’s suggestion so I have tried this:

...
# HTTP Insecure
server {
  blha blah blah
  something here = that
  this = that

  {% for loc in my_vars.http_locations %}
    {{ loc.entry }}

  {% endfor %}

}

But that yields the same output:

# HTTP Insecure
server {
  blha blah blah
  something here = that
  this = that

      location / {
 set something
}


      location /foo {
  set something
  hover: craft
}

2

Answers


  1. Chosen as BEST ANSWER

    Ok. I am started playing with the indent filter in my template. Here is the template I am using to generate the output I wanted:

    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 %}
    
    }
    

    Now I get this output:

    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. have you tried modifying the indentation in the second loop?

    # HTTP Insecure
    server {
      blha blah blah
      something here = that
      this = that
    
      {% for ups in my_vars.http_locations %}
        {{ ups.entry }}
    
      {% endfor %}
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search