# Looping through list/array values

**URL:** <https://forum.ansible.com/t/looping-through-list-array-values/27151>\
**Category:** Ansible Project\
**Created:** [March 29, 2018, 6:43pm UTC](https://forum.ansible.com/t/looping-through-list-array-values/27151 "2018-03-29T18:43:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gtreynolds](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@gtreynolds](https://forum.ansible.com/u/gtreynolds)\
**Post date:** [March 29, 2018, 6:43pm UTC](https://forum.ansible.com/t/looping-through-list-array-values/27151/1 "2018-03-29T18:43:43Z")

</div>

Hello Everyone,

I’m new to Ansible/yaml and have been trying to develop some basic playbooks to assist with some simple deployment/verification tasks. I have a simple playbook/play that is using the uri module to make a connection to websites - I’d like to be able to output the name of the website that the play connected to and the status of the connection attempt.

I’ve found that the values I’m trying to reference (from the playbook below) are webpage.results[_].url and webpage.results[_].status. I can type in a debug message that prints the values when I know the number of entries, but my goal is to have a dynamic list, so I wouldn’t know how many sites to iterate through to print the results.

Here’s the playbook:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 30, 2018, 9:18am UTC](https://forum.ansible.com/t/looping-through-list-array-values/27151/2 "2018-03-30T09:18:24Z")

</div>

webpage.results is a list and with\_items takes a list.

&nbsp;&nbsp;- debug: msg="The result of {{ item.url }} is {{ item.status }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ webpage.results }}'

---

<div class="post-metadata">

**Author:** ![gtreynolds](https://avatars.discourse-cdn.com/v4/letter/g/97f17d/32.png) [@gtreynolds](https://forum.ansible.com/u/gtreynolds)\
**Post date:** [March 30, 2018, 3:28pm UTC](https://forum.ansible.com/t/looping-through-list-array-values/27151/3 "2018-03-30T15:28:03Z")

</div>

I’ve found that using a template has solved my general issue - the only thing I’m trying to figure out is how to insert a carriage return or new line for each result:

- debug:  
msg: “{% set output = %}  
{% for result in webpage.results %}  
{{output.append('Website: ’ + result.url + ’ got status ’ + result.status|string + ‘.’ )}}  
{% endfor %}  
{{output}}”

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 31, 2018, 5:41am UTC](https://forum.ansible.com/t/looping-through-list-array-values/27151/4 "2018-03-31T05:41:07Z")

</div>

You are here creating a variable output that is a list each element contains "Website: X got status Y."  
Since this is a list adding newline doesn't make sense.

If you are just trying to print out a nice output on screen you can use multiline yaml.

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for result in webpage.results %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Website: {{ result.url }} got status {{ result.status }}.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}
