# Using with\_sequence with register

**URL:** <https://forum.ansible.com/t/using-with-sequence-with-register/15991>\
**Category:** Ansible Project\
**Created:** [August 20, 2014, 11:48am UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991 "2014-08-20T11:48:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Imran\_Khan1](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Imran\_Khan1](https://forum.ansible.com/u/Imran_Khan1)\
**Post date:** [August 20, 2014, 11:48am UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/1 "2014-08-20T11:48:17Z")

</div>

I would like to know that If I run a task in a loop and save the value in a register, how can I save the values so that I have them accessible when I need them  
P.S: I am running my playbook using **serial: 1 .** So, when I iterate a task, say 5 times, it should save them all seperately, so that when the playbook runs serially the second or third time around, I can use the registered values as needed.  
Example:

- hosts: just\_created\_clients  
serial: 1  
gather\_facts: False

---

<div class="post-metadata">

**Author:** ![Imran\_Khan1](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Imran\_Khan1](https://forum.ansible.com/u/Imran_Khan1)\
**Post date:** [August 20, 2014, 11:53am UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/2 "2014-08-20T11:53:09Z")

</div>

What I need is a register array!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 20, 2014, 12:01pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/3 "2014-08-20T12:01:51Z")

</div>

This is easy to experiment with, by constructing a simple playbook:

- shell: echo {{ foo }}  
with\_items: [1,2,3,4,5]  
register: result

- debug: var=result

You will see that the result variable does get registered as an array.

---

<div class="post-metadata">

**Author:** ![Imran\_Khan1](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Imran\_Khan1](https://forum.ansible.com/u/Imran_Khan1)\
**Post date:** [August 20, 2014, 12:11pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/4 "2014-08-20T12:11:20Z")

</div>

Thanks, but to use this I will need to know how many times this task will run. That is, in-fact, decided by the person running the playbook, at run time

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 20, 2014, 12:16pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/5 "2014-08-20T12:16:32Z")

</div>

I’m perhaps not understanding the underlying question, but:

{{ an\_array | length }}

will tell you the length of an array in Jinja2

---

<div class="post-metadata">

**Author:** ![Imran\_Khan1](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Imran\_Khan1](https://forum.ansible.com/u/Imran_Khan1)\
**Post date:** [August 20, 2014, 12:55pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/6 "2014-08-20T12:55:53Z")

</div>

#$ cat bbb.yml

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 20, 2014, 1:03pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/7 "2014-08-20T13:03:38Z")

</div>

Sounds like this is more of a question about asking ansible to do “static” DHCP reservations for you, and reserve a MAC for a future host.

Ansible’s not really a programming language. Can you just use DHCP dynamic ranges? I’m not following the “send to vm” parts and why you would need  
to set that up there.

If you are attempting to manage a large number of static IPs and auto-set up the networking configurations for a bare metal environment, something like Cobbler can be a good fit.

---

<div class="post-metadata">

**Author:** ![Imran\_Khan1](https://avatars.discourse-cdn.com/v4/letter/i/a4c791/32.png) [@Imran\_Khan1](https://forum.ansible.com/u/Imran_Khan1)\
**Post date:** [August 20, 2014, 1:18pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/8 "2014-08-20T13:18:45Z")

</div>

I just need to write some values into a file in a specified format, so that I don’t have to individually access each VM and make changes to the file. The rest of what happens is not ansible’s concern. All I would like to know is how can I run a task in a power of 2 loop (when the loop value is entered by the user) and save some values in a register , values which can be permanently stored in some array and be accessed in any iteration (Tasks run multiple times on each individual VM due to **serial: 1** constraint)

---

<div class="post-metadata">

**Author:** ![Alex\_Elent](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Alex\_Elent](https://forum.ansible.com/u/Alex_Elent)\
**Post date:** [June 18, 2015, 8:34am UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/9 "2015-06-18T08:34:35Z")

</div>

Better late than never but I ran into this same thing and this is how I solved it with example code:

- name: Launch instances  
ec2:  
user\_data: “{{ lookup(‘file’, ‘user\_data.yml’) }}”

keypair: “{{ keypair }}”  
group\_id: “{{ security\_group }}”  
instance\_type: “{{ instance\_type }}”  
image: “{{ image }}”  
count: 1  
region: “{{ region }}”

wait: true

volumes:

- device\_name: /dev/sdb  
ephemeral: ephemeral0

- device\_name: /dev/sdc  
ephemeral: ephemeral1  
with\_sequence: count=3  
register: inst

- debug: msg=“{{item.instances[item.item|int - 1].id}}”  
with\_items: “{{inst.results}}”

---

<div class="post-metadata">

**Author:** ![Alex\_Elent](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Alex\_Elent](https://forum.ansible.com/u/Alex_Elent)\
**Post date:** [June 18, 2015, 5:18pm UTC](https://forum.ansible.com/t/using-with-sequence-with-register/15991/10 "2015-06-18T17:18:15Z")

</div>

Sorry my reply was slightly incorrect. You don’t need “item.item|int -1”. “0” would suffice and provide all items values.
