Converted "with_items" play to new-style loop, now it is failing

33 views
Skip to first unread message

Willard Dennis

unread,
Dec 18, 2018, 4:19:50 PM12/18/18
to Ansible Project
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...)

Matt Martz

unread,
Dec 18, 2018, 4:23:15 PM12/18/18
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f33909b6-bffa-42e1-82d2-eea482c9805e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Willard Dennis

unread,
Dec 18, 2018, 4:29:25 PM12/18/18
to Ansible Project
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?


On Tuesday, December 18, 2018 at 4:23:15 PM UTC-5, Matt Martz wrote:
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.

Matt Martz

unread,
Dec 18, 2018, 4:35:38 PM12/18/18
to ansible...@googlegroups.com
`with_items` is not deprecated for package manager modules either.  What is deprecated is "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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Karl Auer

unread,
Dec 18, 2018, 4:36:29 PM12/18/18
to ansible-project
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Willard Dennis

unread,
Dec 20, 2018, 11:33:13 AM12/20/18
to Ansible Project
Thank you for clearing this up for me! Never knew about "squash actions" before...


On Tuesday, December 18, 2018 at 4:35:38 PM UTC-5, Matt Martz wrote:
`with_items` is not deprecated for package manager modules either.  What is deprecated is "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.

Reply all
Reply to author
Forward
0 new messages