# with\_items question

**URL:** <https://forum.ansible.com/t/with-items-question/29856>\
**Category:** Ansible Project\
**Created:** [March 9, 2019, 1:18am UTC](https://forum.ansible.com/t/with-items-question/29856 "2019-03-09T01:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liverpudlian88](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@liverpudlian88](https://forum.ansible.com/u/liverpudlian88)\
**Post date:** [March 9, 2019, 1:18am UTC](https://forum.ansible.com/t/with-items-question/29856/1 "2019-03-09T01:18:21Z")

</div>

Hello,

I am kinda new to Ansible, I would appreciate if you could assist me with a question, I am trying to do the following in a role in a playbook

1- Copy 3 zip files to a specific destination  
2- Unzip only the first  
3- Rename a directory in the unzipped location  
4- Unzip the remaining 2 files

I mainly have 2 problems, the first is that only the first file is copied and second the when condition is apparently not written correctly

This is my list in the role’s default directory

installer\_archives:

- { name: V978971-01.zip, dest: “{{oracle\_home\_gi}}” }
- { name: p28828717\_180000\_Linux-x86-64.zip, dest: “{{oracle\_stage}}” }
- { name: p6880880\_121010\_Linux-x86-64.zip, dest: “{{oracle\_home\_gi}}” }

This is my role:

# ==\> Copy installer files

- name: Copy installer files  
copy:  
src: ‘{{ item }}’  
dest: ‘{{ oracle\_stage }}/’  
owner: ‘{{ grid\_install\_user }}’  
mode: 0644  
group: ‘{{ oracle\_group }}’  
with\_items: ‘{{ installer\_archives }}’  
when: ansible\_hostname==groups[group\_names | first] | first

- name: install-home-gi | Extract files to ORACLE\_HOME (gi)  
unarchive:  
src: ‘{{ oracle\_stage }}/{{ item }}’  
dest: ‘{{ item.dest }}’  
copy: no  
owner: ‘{{ grid\_install\_user }}’  
group: ‘{{ oracle\_group }}’  
with\_items: ‘{{installer\_archives}}’  
args:  
creates: ‘{{ oracle\_home\_gi }}/root.sh’  
when: ansible\_hostname==groups[group\_names | first] | first and “{{installer\_archives}}” | first

- name: install-home-gi | Move old Opatch directory  
shell: “mv {{ oracle\_home\_gi }}/OPatch {{ oracle\_home\_gi }}/OPatch.old”

- name: install-home-gi | Extract Opatch and BP  
unarchive:  
src: ‘{{ oracle\_stage }}/{{ item.name }}’  
dest: ‘{{ item.dest }}’  
copy: no  
owner: ‘{{ grid\_install\_user }}’

group: ‘{{ oracle\_group }}’  
with\_items: ‘{{installer\_archives}}’  
when: ansible\_hostname==groups[group\_names | first] | first and not {{installer\_archives}} | first

Thank you

---

<div class="post-metadata">

**Author:** ![liverpudlian88](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@liverpudlian88](https://forum.ansible.com/u/liverpudlian88)\
**Post date:** [March 10, 2019, 2:34pm UTC](https://forum.ansible.com/t/with-items-question/29856/2 "2019-03-10T14:34:08Z")

</div>

Hello, Any help please?

---

<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 10, 2019, 3:17pm UTC](https://forum.ansible.com/t/with-items-question/29856/3 "2019-03-10T15:17:22Z")

</div>

> Hello, Any help please?

A lot of fundamental problems. I'll try to address them all.

> > Hello,
> > 
> > I am kinda new to Ansible, I would appreciate if you could assist me with  
> > a question, I am trying to do the following in a role in a playbook
> > 
> > 1- Copy 3 zip files to a specific destination  
> > 2- Unzip only the first  
> > 3- Rename a directory in the unzipped location  
> > 4- Unzip the remaining 2 files
> > 
> > I mainly have 2 problems, the first is that only the first file is copied  
> > and second the when condition is apparently not written correctly

This is not very helpful and probably the reason you haven't gotten an answer (plus is a weekend).  
You should always provide the full output of the run, very hard to help without since it takes a lot more time.

> > This is my list in the role's default directory  
> > &nbsp;&nbsp;installer\_archives:  
> > &nbsp;&nbsp;- { name: V978971-01.zip, dest: "{{oracle\_home\_gi}}" }  
> > &nbsp;&nbsp;- { name: p28828717\_180000\_Linux-x86-64.zip, dest: "{{oracle\_stage}}" }  
> > &nbsp;&nbsp;- { name: p6880880\_121010\_Linux-x86-64.zip, dest: "{{oracle\_home\_gi}}" }
> > 
> > This is my role:  
> > # ==\> Copy installer files  
> > - name: Copy installer files  
> > &nbsp;&nbsp;copy:  
> > &nbsp;&nbsp;&nbsp;&nbsp;src: '{{ item }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;dest: '{{ oracle\_stage }}/'  
> > &nbsp;&nbsp;&nbsp;&nbsp;owner: '{{ grid\_install\_user }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
> > &nbsp;&nbsp;&nbsp;&nbsp;group: '{{ oracle\_group }}'  
> > &nbsp;&nbsp;with\_items: '{{ installer\_archives }}'  
> > &nbsp;&nbsp;when: ansible\_hostname==groups[group\_names | first] | first

Every item will contain a dict with two keys, item.name and item.dest.  
You are here using item that will contain both key and value.  
I guess you are after item.name in your src: and not both of them since that make no sense.

> > - name: install-home-gi | Extract files to ORACLE\_HOME (gi)  
> > &nbsp;&nbsp;unarchive:  
> > &nbsp;&nbsp;&nbsp;&nbsp;src: '{{ oracle\_stage }}/{{ item }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;dest: '{{ item.dest }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;copy: no  
> > &nbsp;&nbsp;&nbsp;&nbsp;owner: '{{ grid\_install\_user }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;group: '{{ oracle\_group }}'  
> > &nbsp;&nbsp;with\_items: '{{installer\_archives}}'  
> > &nbsp;&nbsp;args:  
> > &nbsp;&nbsp;&nbsp;&nbsp;creates: '{{ oracle\_home\_gi }}/root.sh'  
> > &nbsp;&nbsp;when: ansible\_hostname==groups[group\_names | first] | first and  
> > "{{installer\_archives}}" | first

Same here, your item is a dict and your are probably looking for item.name and not just item.

> > - name: install-home-gi | Move old Opatch directory  
> > &nbsp;&nbsp;shell: "mv {{ oracle\_home\_gi }}/OPatch {{ oracle\_home\_gi }}/OPatch.old"
> > 
> > - name: install-home-gi | Extract Opatch and BP  
> > &nbsp;&nbsp;unarchive:  
> > &nbsp;&nbsp;&nbsp;&nbsp;src: '{{ oracle\_stage }}/{{ item.name }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;dest: '{{ item.dest }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;copy: no  
> > &nbsp;&nbsp;&nbsp;&nbsp;owner: '{{ grid\_install\_user }}'  
> > &nbsp;&nbsp;&nbsp;&nbsp;group: '{{ oracle\_group }}'  
> > &nbsp;&nbsp;with\_items: '{{installer\_archives}}'  
> > &nbsp;&nbsp;when: ansible\_hostname==groups[group\_names | first] | first and not  
> > {{installer\_archives}} | first

Here you actually have item.name and this is probably the only task that works.

---

<div class="post-metadata">

**Author:** ![Gavin\_Henry](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Gavin\_Henry](https://forum.ansible.com/u/Gavin_Henry)\
**Post date:** [March 10, 2019, 3:19pm UTC](https://forum.ansible.com/t/with-items-question/29856/4 "2019-03-10T15:19:28Z")

</div>

Have you run your playbook with -vvvvv to see what is actually happening?

---

<div class="post-metadata">

**Author:** ![liverpudlian88](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@liverpudlian88](https://forum.ansible.com/u/liverpudlian88)\
**Post date:** [March 11, 2019, 10:12pm UTC](https://forum.ansible.com/t/with-items-question/29856/5 "2019-03-11T22:12:25Z")

</div>

Thank you for all the help, this is the output of the first task with -vvvvv when I try item.name

TASK [oraswgi-install : Copy installer files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /root/MY\_RAC/roles/oraswgi-install/tasks/main.yml:25  
Read vars\_file ‘infra-vars.yml’  
Read vars\_file ‘db-vars.yml’  
Read vars\_file ‘secrets.yml’  
fatal: [oradb1]: FAILED! =\> {  
“msg”: “The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘name’\n\nThe error appears to have been in ‘/root/MY\_RAC/roles/oraswgi-install/tasks/main.yml’: line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# ==\> Copy installer files\n- name: Copy installer files\n ^ here\n”  
}  
skipping: [oradb2] =\> (item=V978971-01.zip) =\> {  
“changed”: false,  
“item”: “V978971-01.zip”,  
“skip\_reason”: “Conditional result was False”  
}  
Read vars\_file ‘infra-vars.yml’  
Read vars\_file ‘db-vars.yml’  
Read vars\_file ‘secrets.yml’

The modified task is:

# ==\> Copy installer files

- name: Copy installer files  
copy:  
src: ‘{{ item.name }}’  
dest: ‘{{ oracle\_stage }}/’  
owner: ‘{{ grid\_install\_user }}’  
mode: 0644  
group: ‘{{ oracle\_group }}’  
with\_items: ‘{{ installer\_archives }}’  
when: ansible\_hostname==groups[group\_names | first] | first
