How to add a number to an {{ item }}

Hello,

In certain cases it’s useful to be able to add a number to the value of an {{ item }} variable

For example when creating md (raid) devices, md0 for example is made up of partitions sda1 and sdb1.

with_items:
[ 0, 1, 2, 3, 4 etc ]

It would be nice to be able to reference {{ item }} for the md device names as well
as {{ item + 1 }} for the partition names.

{{ item + 1 }} didn’t seem to work for me and I haven’t been able to find anything online or
in the documentation as to whether something like this is possible.

I wound up using with_together with two sequences for ex. [0, 1, 2 ] and [1, 2, 3] but it’s
not completely elegant and can lead to typing errors.

Any hints?

Thanks in advance,
–Ed

Try {{ item|int +1 }}

K

That won’t quite work as you cannot access the whole list this way.

However:

http://docs.ansible.com/playbooks_loops.html#id20

each item and it’s successor:

  • debug: msg=“at index {{ item[0] }} there lies a value {{ item[1] }} and the next item is {{ foo[item[0]] }}”
    with_indexed: foo

Though you’ll walk off the end of the list and will need to account for that.

It seems it would be much better to instead loop over a hash.

my_setup:

  • partition: foo
    mount: bar
  • partition: baz
    mount: quux

with_items: my_setup

{{ item.mount }} and {{ item.partition }}

etc.