template not expanded after upgrading from 1.7 to 2.0.1.0

45 views
Skip to first unread message

seb....@gmail.com

unread,
Apr 11, 2016, 5:22:11 AM4/11/16
to Ansible Project
I am sure I missed something here, but couldn't find a hint in the changelog. So far I used ansible 1.7 on Ubuntu 12.04.5 and recently upgraded to ansible 2.0.1.0 on ubuntu 16.04 (beta) and it worked well. Only one of my playbooks is causing problems.

The template in question sits in a sub directory called check_mk, and I call it like this:

ansible-playbook check_mk/gen_check_mk_config.yml

It gathers facts from all my servers and compiles a single configuration file to be put on some host. This works exactly like intended with my older ansible 1.7 installation, but not with my 2.0.1.0 installation on ubuntu 16.04 (beta). There is no error message, it just won't expand the template but rather copy the generic jinja2 template, unexpanded, to the destination.

This is the playbook:

$ cat check_mk/gen_check_mk_config.yml
---
- name: collect facts
  hosts
: all:!workstations

- hosts: myworkstation
  gather_facts
: no
  user
: root
  tasks
:
   
- name: assemble check_mk config
     
template: src=ei-auto.mk.j2 dest=/etc/nagios3/check_mk/conf.d/ei-auto.mk mode=0644
     
register: check_mk_config
   
- name: update nagios
      action
: shell cmk -O
     
when: check_mk_config.changed

And this is the template:

$ cat check_mk/ei-auto.mk.j2
# {{ ansible_managed }}
# vim: filetype=python:

ipaddresses
.update({
{% for h in hostvars.keys() %}
   
"{{ h }}": "{{ hostvars[h]['ansible_default_ipv4']['address'] }}",
{% endfor %}
})

all_hosts
+= [
{% for h in hostvars.keys() %}
   
{% set tags = hostvars[h]['check_mk_tags'] %}
   
"{{ h }}|agbs|{{ hostvars[h]['ansible_system'].lower().replace(' ', '') }}{{ '|'+'|'.join(tags) if tags }}",
{% endfor %}
]

extra_host_conf
["alias"] += [
{% for h in hostvars.keys() %}
   
( '{{ hostvars[h]['descr'] }}', ['{{ h }}'], ),
{% endfor %}
]


Again, thanks for any hint helping me to understand what I'm doing wrong or what has changed between ansible 1.7 and 2.0.1.0 concerning templates.


Sebastian

Johannes Kastl

unread,
Apr 11, 2016, 3:35:34 PM4/11/16
to ansible...@googlegroups.com
On 11.04.16 11:22 seb....@gmail.com wrote:

> The template in question sits in a sub directory called check_mk, and I
> call it like this:
>
> ansible-playbook check_mk/gen_check_mk_config.yml

Does calling the playbook with

ansible-playbook -vvv check_mk/...

return any more info what goes wrong?

Johannes

signature.asc

Brian Coca

unread,
Apr 12, 2016, 12:07:52 AM4/12/16
to ansible...@googlegroups.com
hostvars is not a dict anymore, so .keys() might not work


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/570BFC76.2050909%40ojkastl.de.
For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

seb....@gmail.com

unread,
Apr 12, 2016, 2:07:03 AM4/12/16
to Ansible Project
The output is a bit long so I'm hesitating to post it here. But in my eyes it looks no different from the output of a working playbook execution. In particular there is no error message. Thank you anyway.

seb....@gmail.com

unread,
Apr 12, 2016, 2:11:47 AM4/12/16
to Ansible Project


Am Dienstag, 12. April 2016 06:07:52 UTC+2 schrieb Brian Coca:
hostvars is not a dict anymore, so .keys() might not work

That's it! Thank you so much. If I replace hostvars.keys() with groups['servers'] it works!

Using unsupported ways to access internal data structures (.keys()) should be avoided, I know, but still I wonder why ansible does not indicate a failed template expansion and simply pushes the jinja2 code, which in almost any case should be the wrong thing to do and lead to service failure.
Reply all
Reply to author
Forward
0 new messages