Jinja2 for loop in templates

7,106 views
Skip to first unread message

Mark Maas

unread,
Nov 30, 2012, 5:42:23 AM11/30/12
to ansible...@googlegroups.com
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

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

And one of the host_vars has this added to it:
---
extra-apt-lines:
  - deb http://oss.oracle.com/debian unstable main non-free

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

Daniel Hokka Zakrisson

unread,
Nov 30, 2012, 5:48:01 AM11/30/12
to ansible...@googlegroups.com
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.

> {{ item }}
> {% endfor %}
>
>
> And one of the host_vars has this added to it:
>
> ---
> extra-apt-lines:
> - deb http://oss.oracle.com/debian unstable main non-free
>
>
> 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
>
> --
>
>
>

Mark Maas

unread,
Nov 30, 2012, 6:00:53 AM11/30/12
to ansible...@googlegroups.com
On Fri, Nov 30, 2012 at 11:48 AM, Daniel Hokka Zakrisson <dan...@hozac.com> wrote:
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! 

Arie Skliarouk

unread,
Dec 4, 2012, 2:52:56 PM12/4/12
to ansible...@googlegroups.com, dan...@hozac.com
> 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.

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?

Michael DeHaan

unread,
Dec 4, 2012, 3:35:42 PM12/4/12
to ansible...@googlegroups.com
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.
> --
>
>

Arie Skliarouk

unread,
Dec 10, 2012, 7:22:34 AM12/10/12
to ansible...@googlegroups.com
On Tue, Dec 4, 2012 at 10:35 PM, Michael DeHaan <michael...@gmail.com> wrote:
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"] }}

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

worked perfectly, thank you!

--
Arie
 

Should work.

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



On Tue, Dec 4, 2012 at 2:52 PM, Arie Skliarouk <skli...@gmail.com> wrote:
>> > 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.
>
>
> 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?
>
>> > fatal: [xmgtansible] => {'msg': "unsupported operand type(s) for -:
>> > 'Undefined' and 'Undefined'", 'failed': True}
>>
> --
>
>

--



Reply all
Reply to author
Forward
0 new messages