Jinja2 for loop in templates

Hi List,

I’m currently trying to roll out a default /etc/apt/sources.list and add “deb” lines to it for specific hosts.

The template sources.j2 looks like this:

#############################################################

################### OFFICIAL UBUNTU REPOS ###################

#############################################################

Ubuntu Main Repos

deb http://nl.archive.ubuntu.com/ubuntu/ {{ ansible_distribution_release }} main restricted universe multiverse

Ubuntu Update Repos

deb http://nl.archive.ubuntu.com/ubuntu/ {{ ansible_distribution_release }}-security main restricted universe multiverse

deb http://nl.archive.ubuntu.com/ubuntu/ {{ ansible_distribution_release }}-updates main restricted universe multiverse

Ubuntu Partner Repo

deb http://archive.canonical.com/ubuntu {{ ansible_distribution_release }} partner

deb-src http://archive.canonical.com/ubuntu {{ ansible_distribution_release }} partner

Ubuntu Opsview Repo

deb https://XXXXXX:XXXXXXXX@downloads.opsview.com/opsview-commercial/latest/apt {{ ansible_distribution_release }} main

{% for item in extra-apt-lines %}

{{ item }}

{% endfor %}

And one of the host_vars has this added to it:


extra-apt-lines:

The task itself, ofcourse:


  • name: APT | Kopieer apt-sources config

action: template src=~/playbooks/templates/sources.j2 dest=/etc/apt/sources.list owner=root group=root mode=0644

notify:

  • reload apt

tags:

  • apt

When I run this, I get this error message:

fatal: [xmgtansible] => {‘msg’: “unsupported operand type(s) for -: ‘Undefined’ and ‘Undefined’”, ‘failed’: True}

I’m trying to base this of: http://jinja.pocoo.org/docs/templates/
Any suggestions?

Thanks!
Mark

Hi,

Mark Maas wrote:

Hi List,

I'm currently trying to roll out a default /etc/apt/sources.list and add
"deb" lines to it for specific hosts.

The template sources.j2 looks like this:

<snip>
{% for item in extra-apt-lines %}

You can't have - in variable names. It's an operator. Name it
extra_apt_lines or something instead, and things should be fine.

Oh my. That simple a fix huh? Thanks! Good catch!

The template sources.j2 looks like this:

{% for item in extra-apt-lines %}

You can’t have - in variable names. It’s an operator. Name it
extra_apt_lines or something instead, and things should be fine.

Sorry for hijacking the thread, but looks like it already have been resolved…

On one of my machines I have bonded network interface that happens to be named “main-net0”. Yes, with a dash in the middle. Now, one of my templates requires IP number of the interface. This means to get the IP I need to address ansible_main-net2.ipv4.address variable. And this causes the same “unsupported operand type” error.

How can I workaround the problem?

You can't have those in variable names, and it's a little weird to do
with Ansible's shorthand templating, but nothing wrong with:

{{ hostvars[$inventory_hostname]["whatever variable name you
want"]["some subkey"] }}

Should work.

Though IMHO the setup module should replace any "-" or "." with "_" to
simplify this problem.

{% set mainnet0_ip = hostvars[inventory_hostname][“ansible_main-net0”][“ipv4”][“address”] %}

worked perfectly, thank you!