7

based on the following ansible playbook values ..

target:  "actual.domain.com"
aliases:
  - "alias1.domain.com"
  - "alias2.domain.com"

I am trying to setup an ansible template to produce the nginx server_name which in this case should be:

server_name: "actual.domain.com alias1.domain.com alias2.domain.com"

so , I tried the following jinja2 script ...

{% if item.aliases is defined %}
    {% set servername = [ item.target ] %}
    {% for alias in item.aliases.iteritems() %}
       {% if alias|length > 0 %}
           {% servername|join(' '), alias %}  # <= line 30
       {% endif %}
    {% endfor %}
    server_name {{ servername }};
 {% else %}
     server_name {{ item.target }};
 {% endif %}
  ....

but it's failing , line number: 30, error: Encountered unknown tag 'servername'

where could I be wrong ?

thanks for help and HNY !

techraf
  • 64,883
  • 27
  • 193
  • 198

2 Answers2

7

It seems you've made this substantially more complicated than necessary. Why not something like this?

$ ansible-playbook -i hosts play.yml

PLAY [localhost] **************************************************************

TASK: [template src='servername.j2' dest=tmp/servername-{{item.target}}] ******
changed: [localhost] => (item={'target': 'actual.domain.com', 'aliases': ['alias1.domain.com', 'alias2.domain.com']})

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0

Contents of files

$ tail -n 1000 `find ./ -type f`

==> .//hosts <==
[localhost]
localhost ansible_connection=local


==> .//play.yml <==
- hosts: localhost
  gather_facts: false
  vars:
    servers:
      - target: "actual.domain.com"
        aliases:
          - "alias1.domain.com"
          - "alias2.domain.com"
  tasks:
    - template: src='servername.j2' dest=tmp/servername-{{item.target}}
      with_items: servers


==> .//servername.j2 <==
server_name {{ item.target }} {{ item.aliases|join(" ") }}


==> .//tmp/servername-actual.domain.com <==
server_name actual.domain.com alias1.domain.com alias2.domain.com
slm
  • 15,396
  • 12
  • 109
  • 124
erik258
  • 14,701
  • 2
  • 25
  • 31
  • thanks ! I'm not yet very fluent with it .. but it's true that it can be made simpler ... got it ! –  Jan 01 '15 at 22:45
  • 1
    note the implicit answer is in here- pulling `server_name` out, since it's constant text, not a variable containing a dict. – tedder42 Jan 01 '15 at 23:32
5

Why not simply concatenate target and aliases and then run join(' ') on the result?

{% set servername = [ item.target ] %}
{% set aliases = item.aliases if item.aliases is defined else [] %}
{% set ignored = servername.extend(aliases) %}
servername: {{ servername | join(' ') }}
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • thanks ... even if Dan's answer is the one I implemented ... your answer give me some hints on jinja2 scripting... –  Jan 01 '15 at 22:45
  • 3
    `set ignored = `. Ugh. The `{% do %}` block would make that less ugly, but Ansible doesn't seem to enable it. – Craig Ringer Aug 11 '15 at 05:36