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

**URL:** <https://forum.ansible.com/t/how-to-add-a-number-to-an-item/14475>\
**Category:** Ansible Project\
**Created:** [February 3, 2014, 6:06pm UTC](https://forum.ansible.com/t/how-to-add-a-number-to-an-item/14475 "2014-02-03T18:06:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![edrozenberg](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@edrozenberg](https://forum.ansible.com/u/edrozenberg)\
**Post date:** [February 3, 2014, 6:06pm UTC](https://forum.ansible.com/t/how-to-add-a-number-to-an-item/14475/1 "2014-02-03T18:06:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [February 3, 2014, 7:37pm UTC](https://forum.ansible.com/t/how-to-add-a-number-to-an-item/14475/2 "2014-02-03T19:37:14Z")

</div>

Try {{ item|int +1 }}

K

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [February 3, 2014, 7:42pm UTC](https://forum.ansible.com/t/how-to-add-a-number-to-an-item/14475/3 "2014-02-03T19:42:53Z")

</div>

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

However:

[http://docs.ansible.com/playbooks\_loops.html#id20](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.
