# Ansible looping over register and multiple lists

**URL:** <https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 21, 2018, 1:55pm UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578 "2018-05-21T13:55:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremie\_Levy](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jeremie\_Levy](https://forum.ansible.com/u/Jeremie_Levy)\
**Post date:** [May 21, 2018, 1:55pm UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/1 "2018-05-21T13:55:11Z")

</div>

Hi  
I’m trying to do the following (in windows but it’s not important):

1. Need to deploy N number of build agents to a server (according to the inventory nb\_of\_agents) - **iteration1**

2. Each agent should have a unique port starting from 8811 - **iteration2**

3. Check if the service is already present - store in register. - **REGISTER LIST**

4. Install according to the register.  
First question: Using a register list, how do i use : when: service.results.exists for specific index?

Second question: how do i go in parrallel via multiple list:

1. with\_sequence: start=1 end=‘{{ nb\_of\_agents}}’’ **and**
2. with\_sequence: start=8811 count=‘{{ nb\_of\_agents}}’’ **and**
3. when: service.result[index].exists

So the code looks like this:

- name: check service ‘{{ item }}’ is not already running  
win\_service:  
name: Build Agent Prod ‘{{ item }}’  
register: svc  
with\_sequence: start=1 end=‘{{nb\_of\_agents}}’

- name: output  
debug:  
var: svc

- name: output  
debug:  
var: svc.results.[item].exists  
with\_sequence: count=‘{{nb\_of\_agents}}’

- name: install Agent ‘{{ item }}’  
win\_copy:  
src: \some\share\buildagent\_prod\_template  
dest: ‘D:\buildagent\_prod\_{{ item }}’  
force: no  
remote\_src: yes  
when: svc.results[item].exists == false  
register: agent  
with\_sequence: count=‘{{nb\_of\_agents}}’

- name: modify Port  
win\_lineinfile:  
path: D:\buildagent\_prod\_{{ item }}\conf\node.properties  
backrefs: yes  
regexp: ‘(^port)=.\*’  
line: ‘$1={{ item2 }}’  
when: ‘agent.results[item]’  
with\_together: count=nb\_of\_agents  
with\_items: start=qb\_prod\_agent\_port count=nb\_of\_agents

**Thanks!!**

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 21, 2018, 2:05pm UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/2 "2018-05-21T14:05:31Z")

</div>

Please paste the code WITHOUT any formatting. Font should be any single-space so that spacing is correct.

---

<div class="post-metadata">

**Author:** ![Jeremie\_Levy](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jeremie\_Levy](https://forum.ansible.com/u/Jeremie_Levy)\
**Post date:** [May 22, 2018, 4:39am UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/3 "2018-05-22T04:39:49Z")

</div>

Hi David  
Here it is:

`

- name: check service ‘{{ item }}’ is not already running  
win\_service:  
name: Build Agent Prod ‘{{ item }}’  
register: svc  
with\_sequence: start=1 end=‘{{nb\_of\_agents}}’

- name: output  
debug:  
var: svc

- name: output  
debug:  
var: svc.results.[item].exists  
with\_sequence: count=‘{{nb\_of\_agents}}’

- name: install Agent ‘{{ item }}’  
win\_copy:  
src: \some\share\buildagent\_prod\_template  
dest: ‘D:\buildagent\_prod\_{{ item }}’  
force: no  
remote\_src: yes  
when: svc.results[item].exists == false  
register: agent  
with\_sequence: count=‘{{nb\_of\_agents}}’

- name: modify Port  
win\_lineinfile:  
path: D:\buildagent\_prod\_{{ item }}\conf\node.properties  
backrefs: yes  
regexp: ‘(^port)=.\*’  
line: ‘$1={{ item2 }}’  
when: ‘agent.results[item]’  
with\_together: count=nb\_of\_agents  
with\_items: start=qb\_prod\_agent\_port count=nb\_of\_agents

`

Thanks

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 22, 2018, 5:04am UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/4 "2018-05-22T05:04:06Z")

</div>

You can’t normally run in parallel with a loop, they are run sequentially. You can hack around it by running with async and poll 0 and then poll that status but it isn’t a perfect “run all items in list in parallel” that you are looking for.

Your debug output

`

- name: output  
debug:  
var: svc.results.[item].exists  
with\_sequence: count=‘{{nb\_of\_agents}}’

`

Won’t work as you are trying to get key 0, 1, 2, 3, etc of the svc.results var when it is actually a list. You want something like '`svc.results[item].exists' instead. What this does is get's the list entry of item, bare in mind that lists are a 0 based index, so the first entry is 0, 2nd is 1 and so on. You might have to do something like 'svc.results[item - 1].exists' if you want to match up the entry to your agent index starting at 1.`

`I haven't used with_sequence a lot so can't tell if that syntax is ok, apart from that what you have seems ok at a brief glance but having the output from a run that failed would help us narrow down your issue.`

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Jeremie\_Levy](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jeremie\_Levy](https://forum.ansible.com/u/Jeremie_Levy)\
**Post date:** [May 22, 2018, 5:11am UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/5 "2018-05-22T05:11:17Z")

</div>

Thank you Jordan  
Actually, I also tried without the “.” as you suggested and it didn’t work  
Code:

`

- name: output  
debug:  
var: qb\_svc

- name: output  
debug:  
var: qb\_svc.results[item].exists  
with\_sequence: count=‘{{nb\_of\_agents}}’

`

Here is my output with your suggestion:

`  
task path: /ansible/scripts/playbooks/qb-agents-fw-multiple.yml:16  
ok: [hasgappqba1102.DOMAIN] =\> {  
**“qb\_svc”: {**  
“changed”: false,  
“msg”: “All items completed”,  
**“results”: [**  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
**“exists”: false,**  
“failed”: false,  
“item”: “1”  
},  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
**“exists”: false,**  
“failed”: false,  
“item”: “2”  
}  
]  
}  
}

TASK [output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /ansible/scripts/playbooks/qb-agents-fw-multiple.yml:20  
ok: [hasgappqba1102.DOMAIN] =\> (item=None) =\> {  
“qb\_svc.results[item].exists”: “VARIABLE IS NOT DEFINED!: ‘list object’ has no attribute u’1’”  
}  
ok: [hasgappqba1102.DOMAIN] =\> (item=None) =\> {  
“qb\_svc.results[item].exists”: “VARIABLE IS NOT DEFINED!: ‘list object’ has no attribute u’2’”  
}

TASK [install Agent ‘{{ item }}’] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /ansible/scripts/playbooks/qb-agents-fw-multiple.yml:25  
fatal: [hasgappqba1102.DOMAIN]: FAILED! =\> {  
“msg”: “The conditional check ‘qb\_svc.results[item].exists’ failed. The error was: error while evaluating conditional (qb\_svc.results[item].exists): ‘list object’ has no attribute u’1’\n\nThe error appears to have been in ‘/ansible/scripts/playbooks/qb-agents-fw-multiple.yml’: line 25, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: install Agent ‘{{ item }}’\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - "{{ foo }}"\n”  
}  
to retry, use: --limit @/ansible/scripts/playbooks/qb-agents-fw-multiple.retry  
`

Thank you!

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 22, 2018, 6:39am UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/6 "2018-05-22T06:39:50Z")

</div>

Looks like item is being used as a unicode string (u’1’) and not an int, can you try `qb_svc.results[item|int].exists and see if that works?`

---

<div class="post-metadata">

**Author:** ![Jeremie\_Levy](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jeremie\_Levy](https://forum.ansible.com/u/Jeremie_Levy)\
**Post date:** [May 22, 2018, 7:42am UTC](https://forum.ansible.com/t/ansible-looping-over-register-and-multiple-lists/27578/7 "2018-05-22T07:42:27Z")

</div>

It worked!  
Since i the first is 0, the last should be {{nb\_of\_agents -1 |int}}

`

- name: output  
debug:  
var: qb\_svc.results[item|int].exists  
with\_sequence: start=0 end=‘{{nb\_of\_agents - 1 |int}}’

`

Thanks!!!
