# Converted "with\_items" play to new-style loop, now it is failing

**URL:** <https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294>\
**Category:** Ansible Project\
**Created:** [December 18, 2018, 9:19pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294 "2018-12-18T21:19:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [December 18, 2018, 9:19pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294/1 "2018-12-18T21:19:50Z")

</div>

Hi all,

I had a play in one of my roles that used to look like this:

`

- name: Ensure PRTG monitoring scripts are installed  
copy:  
src: “{{ item }}”  
dest: /var/prtg/scriptsxml/{{ item }}  
owner: root  
group: root  
mode: 0744  
with\_items:
- get\_gpu\_watts.py
- get\_gpu\_watts\_wrapper.sh  
tags: prtgmon  
`

Since I’ve moved up to Ansible 2.7, and was getting the “with\_items” deprecation warning, I re-wrote the play to be in this form:

`

- name: Ensure PRTG monitoring scripts are installed  
copy:  
src: “{{ scripts }}”  
dest: /var/prtg/scriptsxml/{{ scripts }}  
owner: root  
group: root  
mode: 0744  
vars:  
scripts:
- get\_gpu\_watts.py
- get\_gpu\_watts\_wrapper.sh  
tags: prtgmon

`

But now, when I run my role, this play errors out, with the traceback:

`  
TASK [gpu-computing-stack : Ensure PRTG monitoring scripts are installed] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/its/wdennis/workstation-gpu/roles/gpu-computing-stack/tasks/main.yml:316  
The full traceback is:  
Traceback (most recent call last):  
File “/usr/lib/python2.7/dist-packages/ansible/executor/task\_executor.py”, line 140, in run  
res = self.\_execute()  
File “/usr/lib/python2.7/dist-packages/ansible/executor/task\_executor.py”, line 612, in \_execute  
result = self.\_handler.run(task\_vars=variables)  
File “/usr/lib/python2.7/dist-packages/ansible/plugins/action/copy.py”, line 454, in run  
trailing\_slash = source.endswith(os.path.sep)  
AttributeError: ‘list’ object has no attribute ‘endswith’

fatal: [skyserver15k]: FAILED! =\> {  
“msg”: “Unexpected failure during module execution.”,  
“stdout”: “”  
}  
`

Can anyone tell me what the problem is with my play? (AttributeError: ‘list’ object has no attribute ‘endswith’ doesn’t mean much to me…)

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 18, 2018, 9:22pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294/2 "2018-12-18T21:22:28Z")

</div>

I don’t know what the deprecation warning was that you were experiencing, but I doubt it was for that task. It was likely for a package manager task, and not for the copy module.

There should be no deprecation for the task you provide in this email.

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [December 18, 2018, 9:29pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294/3 "2018-12-18T21:29:25Z")

</div>

Ah, I see you are correct - I guess I was assuming that all “with\_items” loops had to be re-written… I was getting “DEPRECIATION WARNING” outputs for all the “with\_items” lists I was using with the ‘apt’ module. So, “with\_items” is not deprecated across the board? Why was it deprecated for the package manager modules?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 18, 2018, 9:34pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294/4 "2018-12-18T21:34:53Z")

</div>

`with_items` is not deprecated for package manager modules either. What is deprecated is “squash actions”.

More info can be found at [https://docs.ansible.com/ansible/latest/porting\_guides/porting\_guide\_2.7.html#using-a-loop-on-a-package-module-via-squash-actions](https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.7.html#using-a-loop-on-a-package-module-via-squash-actions)

Before, when using `with_items` on a package manager task, ansible would effectively remove the loop, and squash the items to call the module only 1 time, and not actually loop. That specific squashing functionality will be removed in a future version as indicated by the warning and in the docs link I provide.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 18, 2018, 9:36pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294/5 "2018-12-18T21:36:05Z")

</div>

I think it is because in your re-written version, there is no loop construct. Thus the variable “scripts” is being used in full, wherever you reference it. The problem is that it is a list, not a string. The copy module wants a string as the source, and expects to be able to check whether it has a trailing slash, but the check function (endwith) is an attribute of strings, not lists, hence the error you are seeing.

Whenever you see “X has no Y attribute” or similar, it means you have got X or Y wrong - or both. Or that you have misunderstood what X is. In this particular case, the error message is telling you that “scripts” is not what you think it is. You know it is “scripts” because that is the only list in your play.

If you want to avoid "with\_items’, you will need to use some other loop construct, or unwind your loop (have two plays, one for each script).

Regards, K.

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [December 20, 2018, 4:33pm UTC](https://forum.ansible.com/t/converted-with-items-play-to-new-style-loop-now-it-is-failing/29294/6 "2018-12-20T16:33:13Z")

</div>

Thank you for clearing this up for me! Never knew about “squash actions” before…
