# Ansible script work with array

**URL:** <https://forum.ansible.com/t/ansible-script-work-with-array/23516>\
**Category:** Ansible Project\
**Created:** [January 20, 2017, 12:45pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516 "2017-01-20T12:45:15Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 12:45pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/1 "2017-01-20T12:45:15Z")

</div>

Hi Everyone,

Hear is some part of my ansible code, I want to take contactId from Json array when contactAccount is [someemail2@somedomain.com](mailto:someemail2@somedomain.com), but my script’s this part “ **{{array.{{item}}.contactId}}** ” doesn’t work. Could anyone help me to solve this problem?

**- set\_fact: msg: “{{array.{{item}}.contactId}}” with\_sequence: start=0 end={{ array | length-1 }} when: array.{{item}}.contactAccount == ‘[someemail2@somedomain.com](mailto:someemail2@somedomain.com)’**

Hear is array example:

[{“contactId”: 1234,“contactAccount”: “[someemail1@somedomain.com](mailto:someemail1@somedomain.com)”  
},{ “contactId”: 2345, “contactAccount”: “[someemail2@somedomain.com](mailto:someemail2@somedomain.com)”  
},

…  
{ “contactId”: 6567, “contactAccount”: “[someemail100@somedomain.com](mailto:someemail100@somedomain.com)”  
}

**]**

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 1:11pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/2 "2017-01-20T13:11:20Z")

</div>

Maybe the find filter does what you are looking for?

array.{{item}}.contactaccount.find('someemail') == 1

Untested!

This is not in the filter documentation, I found it in conditionals:  
[https://docs.ansible.com/ansible/playbooks\_filters.html](https://docs.ansible.com/ansible/playbooks_filters.html)  
[https://docs.ansible.com/ansible/playbooks\_conditionals.html](https://docs.ansible.com/ansible/playbooks_conditionals.html)

Johannes

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 1:34pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/3 "2017-01-20T13:34:47Z")

</div>

Thank you for your reply, but I can check contactAccount value with my when condition, I want to take contactId according on contactAccount

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 1:42pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/4 "2017-01-20T13:42:20Z")

</div>

Sorry, misunderstood your problem.

This is wrong syntax, double curly braces are not allowed inside  
double curly braces:  
msg="{{array.{{item}}.contactId}}...

Try this  
msg= "{{array[item]['contactId'] }}...

(contactId is taken as the string contactID, while item is replaced by  
its value. I have no clue how to put that in dot notation...)

Johannes

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 2:01pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/5 "2017-01-20T14:01:47Z")

</div>

Thank you Johannes,

i put msg: “{{array[item][‘contactId’]}}”

but i got following error:

fatal: [10.137.25.64]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘list object’ has no attribute u’3’\n\nThe error appears to have been in ‘/etc/ansible/monitoring/roles/set\_global\_vars\_and\_add\_tab/tasks/main.yml’: line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set\_fact:\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 2:10pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/6 "2017-01-20T14:10:15Z")

</div>

Obviously arrays do not like to be treated like a list...

[https://docs.ansible.com/ansible/playbooks\_variables.html#accessing-complex-variable-data](https://docs.ansible.com/ansible/playbooks_variables.html#accessing-complex-variable-data)

Does this at least spit out something?  
msg: "{{array[[ item] ]}}"

msg: "{{array[[ item] ]['contactId'] }}"

I guess the array needs the square brackets, and AFAIK you get the  
value of a variable by putting it in square brackets, when you are  
inside the double curly ones...

Otherwise separating with commas might be an alternative.

Johannes

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 2:32pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/7 "2017-01-20T14:32:25Z")

</div>

I put msg: “{{array[[ item] ][‘contactId’] }}”

But again got error:

fatal: [10.137.25.64]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: list object has no element [u’3’]\n\nThe error appears to have been in ‘/etc/ansible/monitoring/roles/set\_global\_vars\_and\_add\_tab/tasks/main.yml’: line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set\_fact:\n ^ here\n”}

I think script doesn’t recognizing “item” as a variable beacuse when I am changing msg: “{{array[4][‘contactId’]}}” it works and I got 4th element’s contactId

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [January 20, 2017, 2:42pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/8 "2017-01-20T14:42:35Z")

</div>

u'3' means that the value is a string (unicode).  
Try with:

{{ array[int(item)]['contactId'] }}

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 2:50pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/9 "2017-01-20T14:50:09Z")

</div>

Again error((((

msg: “{{ array[int(item)][‘contactId’] }}”

fatal: [10.137.25.64]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘int’ is undefined\n\nThe error appears to have been in ‘/etc/ansible/monitoring/roles/set\_global\_vars\_and\_add\_tab/tasks/main.yml’: line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set\_fact:\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 2:52pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/10 "2017-01-20T14:52:09Z")

</div>

I would say that again the brackets are missing:  
msg: "{{ array[[ int(item)] ]['contactId'] }}"

Johannes

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 2:57pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/11 "2017-01-20T14:57:35Z")

</div>

msg: "{{ array[[ int(item)] ]['contactId'] }}"

Error:

fatal: [10.137.25.64]: FAILED! =\> {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'int' is undefined\n\nThe error appears to have been in '/etc/ansible/monitoring/roles/set\_global\_vars\_and\_add\_tab/tasks/main.yml': line 53, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- set\_fact:\n ^ here\n"}

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [January 20, 2017, 2:58pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/12 "2017-01-20T14:58:02Z")

</div>

Sorry, I posted the solution without trying it.  
It's not int(item), but item | int(). So, this works for me:  
msg: "{{ array[item | int()]['contactId'] }}"

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 3:02pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/13 "2017-01-20T15:02:07Z")

</div>

Thank you very very much. It works thank you for help. Great Job!!!

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [January 20, 2017, 3:08pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/14 "2017-01-20T15:08:40Z")

</div>

You're welcome.  
I just figured out we were scratching our right ear with left hand.  
The same result can be accomplished by using  
with\_items: "{{ array }}"  
Then you can access the attributes by item.contactId and item.contactAccount:

- set\_fact:  
&nbsp;&nbsp;&nbsp;msg: "{{ item.contactId }}"  
&nbsp;&nbsp;with\_items: "{{ array }}"  
&nbsp;&nbsp;when: item.contactAccount == 'somee...@somedomain.com'

Cheers,  
Marko

---

<div class="post-metadata">

**Author:** ![Stepan\_Harutyunyan](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stepan\_Harutyunyan](https://forum.ansible.com/u/Stepan_Harutyunyan)\
**Post date:** [January 20, 2017, 3:20pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/15 "2017-01-20T15:20:52Z")

</div>

You are right, I tried and this works too, I think this is better solution!!! Thank you one more time!!!

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 4:19pm UTC](https://forum.ansible.com/t/ansible-script-work-with-array/23516/16 "2017-01-20T16:19:12Z")

</div>

Nice, good to know.

Johannes
