# using copy module, with\_items and a vault

**URL:** <https://forum.ansible.com/t/using-copy-module-with-items-and-a-vault/16340>\
**Category:** Ansible Project\
**Created:** [September 25, 2014, 6:03pm UTC](https://forum.ansible.com/t/using-copy-module-with-items-and-a-vault/16340 "2014-09-25T18:03:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Ekberg](https://avatars.discourse-cdn.com/v4/letter/t/919ad9/32.png) [@Tom\_Ekberg](https://forum.ansible.com/u/Tom_Ekberg)\
**Post date:** [September 25, 2014, 6:03pm UTC](https://forum.ansible.com/t/using-copy-module-with-items-and-a-vault/16340/1 "2014-09-25T18:03:17Z")

</div>

I’ve been having trouble with the copy module, using with\_items and a vault. I pulled the devel version of ansible so the version should be 1.7.2 + changes added since then.

I put the whole thing in a public bitbucket repository. You can retrieve it with this:

git clone [git@bitbucket.org](mailto:git@bitbucket.org):tekberg/ansible-foo.git

I distilled the problem down to this small set of files. The larger case has more things (keys, certs, CSR) in the vault. I use with\_items to iterate through them and copy the data to where the keys, certs, CSR belong in Unbuntu.

The instructions on how to run it are included in foo.yml - your host may vary.

The main part is using the copy module:

```auto
  tasks:
  - name: install private key, if one exists
    copy:
      dest: "{{ item[1] }}"
      content: "{{ item[0] }}"
    when: item[1]
    with_items:
      - ("{{PRIVATE_KEY}}", "{{PRIVATE_KEY_FILE}}")

```

```auto

```

```auto
My larger case has more in with_items. The things in caps are in the vault. I had to do the "{{PRIVATE_KEY}}" because without the punctuation item[0] was 'PRIVATE_KEY'. Here is a snippet of the ansible output:

```

```auto

```

```auto
failed: [apps2] => (item=(PRIVATE_KEY, PRIVATE_KEY_FILE)) => {"failed": true, "item": "(PRIVATE_KEY, PRIVATE_KEY_FILE)", "md5sum": "84c40473414caf2ed4a7b1283e48bbf4"}

```

```auto

```

```auto
With the extra "{{...}}" syntax it still fails, but item looks better:

```

```auto

```

```auto
failed: [apps2] => (item=("dfihahf
adkfhalkdfjhalkdghalghjalkjd
49147174*&^(^&((&
", "/etc/ssl/private/foo.key")) => {"failed": true, "item": "(\"dfihahf\nadkfhalkdfjhalkdghalghjalkjd\n49147174*&^(^&((&\n\", \"/etc/ssl/private/foo.key\")", "md5sum": "84c40473414caf2ed4a7b1283e48bbf4"}

```

```auto

```

```auto
You can see item[0] is a 3 line value and item[1] is the a file name.

```

```auto

```

```auto
The error I get is:

```

```auto

```

```auto
  msg: Destination directory does not exist

```

```auto
I hacked the ansible source to display more for this error and this is the result:

```

```auto

```

```auto
  msg: TWE Destination directory does not exist
  dest=", len(dest)=1

```

```auto

```

```auto
So it is trying to deal with a file name consisting of a single double quote.

```

```auto

```

```auto
I have tried everything I could think of but nothing works. Do you have an idea on what I am doing wrong?

```

```auto

```

---

<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:** [September 25, 2014, 6:10pm UTC](https://forum.ansible.com/t/using-copy-module-with-items-and-a-vault/16340/2 "2014-09-25T18:10:42Z")

</div>

I believe the problem is that you are trying to use a tuple in your YAML file, and YAML doesn’t support it.

You with\_items should probably be:

```auto
with_items:
      - [["{{PRIVATE_KEY}}", "{{PRIVATE_KEY_FILE}}"]]

```

```auto
Due to how ansible collapses lists of lists in with_items, you have to nest your list deeper.

```

```auto
Otherwise you could use a hash instead like (this is my preference):

```

---

<div class="post-metadata">

**Author:** ![Tom\_Ekberg](https://avatars.discourse-cdn.com/v4/letter/t/919ad9/32.png) [@Tom\_Ekberg](https://forum.ansible.com/u/Tom_Ekberg)\
**Post date:** [September 25, 2014, 6:58pm UTC](https://forum.ansible.com/t/using-copy-module-with-items-and-a-vault/16340/3 "2014-09-25T18:58:51Z")

</div>

Matt,

Thanks for your help! Coming from a Python world I made the incorrect assumption that YAML supported tuples. Both of your suggestions worked fine. I like the hash better too because it is easier to read.

Tom
