How to use 'with_items' based on variable?

26 views
Skip to first unread message

Bharath Kumar

unread,
Dec 28, 2018, 3:03:27 PM12/28/18
to Ansible Project
Hello Ansible Experts,

I am creating a Role, which has a task to download multiple files from an URL, and, for that, I am using get_url module. Since, get_url cant download files recursively, I have been trying to use 'with_items'.

Role Snippets Below.

/defaults/main.yml
---

files
: "{{ dir }}_files"


/vars/main.yml
---
onedir_files
:
 
- abc1
 
- abc2
 
- abc3


twodir_files
:
 
- xyz1
 
- xyz2


threedir_files
:
 
- one
 
- two
 
- three

/tasks/main.yml
---
- name: Download Properties Files!
  get_url
:
    url
: "{{ url }}/{{ dir }}/{{ item }}"
    dest
: "{{ dest_dir }}/"
  with_items
: "{{ files }}"

ansible-playbook test.yml --extra-vars "dir=one"

Running all tasks fine until the get_url, and, failing with below.

failed: [target_host] (item=onedir_files) => {"changed": false, "dest": "/some/target/path", "gid": 0, "group": "root", "item": "onedir_files", "mode": "0755", "msg": "Request failed", "owner": "root", "response": "HTTP Error 404: org.tmatesoft.svn.core.SVNException: svn: E175007: Path '/some/path/", "secontext": "unconfined_u:object_r:default_t:s0", "size": 4096, "state": "directory", "status_code": 404, "uid": 0, "url": "https://someurl/one/onedir_files"}

Please let me know where I have gone wrong? or, a best practice or a good approach for the above problem/requirement.

Thanks in Advance.



James Cassell

unread,
Dec 28, 2018, 3:47:53 PM12/28/18
to Ansible List
Try

files: "{{ lookup('vars', dir ~ '_files' }}"

V/r,
James Cassell
> *ansible-playbook test.yml --extra-vars "dir=one"*
>
> Running all tasks fine until the get_url, and, failing with below.
>
> failed: [target_host] (item=onedir_files) => {"changed": false, "dest":
> "/some/target/path", "gid": 0, "group": "root", "item": "onedir_files",
> "mode": "0755", "msg": "Request failed", "owner": "root", "response": "HTTP
> Error 404: org.tmatesoft.svn.core.SVNException: svn: E175007: Path
> '/some/path/", "secontext": "unconfined_u:object_r:default_t:s0", "size":
> 4096, "state": "directory", "status_code": 404, "uid": 0, "url":
> "https://someurl/one/onedir_files"}
>
> Please let me know where I have gone wrong? or, a best practice or a good
> approach for the above problem/requirement.
>
> Thanks in Advance.
>
>
>
> --
> 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/0802a1f2-169c-4695-a26a-9e6ec31a8216%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bharath Kumar

unread,
Dec 31, 2018, 1:37:55 PM12/31/18
to Ansible Project
Thanks very much James for your advise. Was able to achieve what was intended, but there seems to be another problem.

Updated the code as below, and, Ansible is passing the list of items to the get_url.

---
- name: Download Properties Files!
  get_url
:
    url
: "{{ url }}/{{ dir }}/{{ item }}"
    dest
: "{{ dest_dir }}/"
  with_items
:
    - files: "{{ lookup('vars', dir ~'_files') }}"

However the get_url step is failing with the below ERROR.

failed: [sometarget-machine.com] (item={u'files': [u'abc1', u'abc2', u'abc3']}) => {"changed": false, "dest": "/some/path/onedir", "gid": 0, "group": "root", "item": {"files": ["abc1", "abc2", "abc3"]}, "mode": "0755", "msg": "Request failed", "owner": "root", "response": "HTTP Error 400: Bad request", "secontext": "unconfined_u:object_r:default_t:s0", "size": 4096, "state": "directory", "status_code": 400, "uid": 0, "url": "https://targetURL/{u'files': [u'abc1', u'abc2', u'abc3']}"}

Also, the same get_url on the same target machine works fine with specified with_items with a list (as below)

---
- hosts: sametarget
  gather_facts
: no
  become
: yes

  tasks
:
 
- name: Download Files
    get_url
:
      url
: https://targetURL{{ item }}
      dest
: some/target/path
    with_items
:

   
- abc1
   
- abc2
   
- abc3

James Cassell

unread,
Dec 31, 2018, 4:09:24 PM12/31/18
to ansible...@googlegroups.com
with_items: "{{ lookup('vars', dir ~'_files') }}"

V/r,
James Cassell
> > > an email to ansible-proje...@googlegroups.com <javascript:>.
> > > To post to this group, send email to ansible...@googlegroups.com
> > <javascript:>.
> > > To view this discussion on the web visit
> > >
> > https://groups.google.com/d/msgid/ansible-project/0802a1f2-169c-4695-a26a-9e6ec31a8216%40googlegroups.com.
> >
> > > For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> 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/7930d991-471c-4b4d-a50c-cf1cbfa7b183%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages