# Iterate with variable and items lists in With\_Items

**URL:** <https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338>\
**Category:** Ansible Project\
**Created:** [November 11, 2022, 8:05pm UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338 "2022-11-11T20:05:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [November 11, 2022, 8:05pm UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338/1 "2022-11-11T20:05:53Z")

</div>

Hello, I hope you can understand what I need, my english is not good, i still learning.

I have a variable

easyrsa\_clients:

- name: ‘{{ client }}’

and this tasks:

- name: "Copy files  
copy:  
src: ‘{{ item.src }}’  
dest: ‘{{ item.dest }}’  
with\_items:
- { src: ‘{{ easyrsa\_dest }}/easyrsa/pki/private/{{ [item.name](http://item.name) }}.key’, dest: ‘{{ easyrsa\_dest }}/keys’ }
- { src: ‘{{ easyrsa\_dest }}/easyrsa/issued/{{ [item.name](http://item.name) }}.crt’, dest: ‘{{ easyrsa\_dest }}/keys’ }

I need to be able to interact with the value of the variable easyrsa\_clients within the parameter with\_item.

I think I should use [easyrsa\_clients.item.name](http://easyrsa_clients.item.name) but I don’t know.

Any ideas??

Regards

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 12, 2022, 2:24am UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338/2 "2022-11-12T02:24:23Z")

</div>

You can’t use “item” inside “with\_items:” because “item” is being defined by “with\_items:”.

Why don’t you just use “client”:

```auto
- name: "Copy files
     copy:
       src: '{{ item.src }}'
       dest: '{{ item.dest }}'
     with_items:
     - { src: '{{ easyrsa_dest }}/easyrsa/pki/private/{{ client }}.key', dest: '{{ easyrsa_dest }}/keys' }
     - { src: '{{ easyrsa_dest }}/easyrsa/issued/{{ client }}.crt', dest: '{{ easyrsa_dest }}/keys' }

```

or “easyrsa\_clients.name”?

Or is there more to the problem that you’ve shown us?

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [November 12, 2022, 2:48am UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338/3 "2022-11-12T02:48:32Z")

</div>

Hello Todd, i try with [easyrsa\_clients.name](http://easyrsa_clients.name) but i see this error:

TASK [Probando salida] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘name’\n\nThe error appears to be in ‘/etc/ansible/openvpn/create\_access\_prod/playbook/list-test.yaml’: line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: "Probando salida"\n ^ here\n”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

How can I use the name key in a with\_items?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 12, 2022, 11:17am UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338/4 "2022-11-12T11:17:28Z")

</div>

Oh, right, because “easyrsa\_clients” is a list, not a dict.  
So try it with either

```auto
     - { src: '{{ easyrsa_dest }}/easyrsa/pki/private/{{ **client** }}.key', dest: '{{ easyrsa_dest }}/keys' }
     - { src: '{{ easyrsa_dest }}/easyrsa/issued/{{ **client** }}.crt', dest: '{{ easyrsa_dest }}/keys' }

```

or

```auto
     - { src: '{{ easyrsa_dest }}/easyrsa/pki/private/{{ **easyrsa_clients[0]["name"]** }}.key', dest: '{{ easyrsa_dest }}/keys' }
     - { src: '{{ easyrsa_dest }}/easyrsa/issued/{{ **easyrsa_clients[0]["name"]** }}.crt', dest: '{{ easyrsa_dest }}/keys' }

or
     - { src: '{{ easyrsa_dest }}/easyrsa/pki/private/{{ **easyrsa_clients[0].name** }}.key', dest: '{{ easyrsa_dest }}/keys' }
     - { src: '{{ easyrsa_dest }}/easyrsa/issued/{{ **easyrsa_clients[0].name** }}.crt', dest: '{{ easyrsa_dest }}/keys' }

```

But I suspect you have bigger plans. Why is easyrsa\_clients a list? Do you intend to add other clients later? In which case, the above isn’t going to work. you need something that will handle a fully articulated set of the data. That’s what I alluded to when I (mis)typed “Or is there more to the problem than you’ve shown us?”

Do let us know how you get on.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [November 14, 2022, 2:36pm UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338/5 "2022-11-14T14:36:45Z")

</div>

Make easyrsa\_clients a dict if you really need a dict (remove the dash).

> easyrsa\_clients:  
> name: ‘{{ client }}’

> ```auto
> - { src: '{{ easyrsa_dest }}/easyrsa/pki/private/{{ **easyrsa_clients["name"]** }}.key', dest: '{{ easyrsa_dest }}/keys' }
> - { src: '{{ easyrsa_dest }}/easyrsa/issued/{{ **easyrsa_clients["name"]** }}.crt', dest: '{{ easyrsa_dest }}/keys' }
> 
> ```

I agree with Todd … just use “client” if you have no other need to use easyrsa\_client.

Walter

---

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [November 14, 2022, 3:44pm UTC](https://forum.ansible.com/t/iterate-with-variable-and-items-lists-in-with-items/37338/6 "2022-11-14T15:44:16Z")

</div>

Hello!! thanks for the help.

You’re right, it’s a list, not a dictionary.

The idea was to generate a variable in the form of a list for cases that I need to create accesses to the VPN to more than one user, with your explanation I could understand my error.  
Thank you very much.
