# File copy/Create folder on Windows server based upon json output from Powershell

**URL:** <https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 15, 2018, 12:58am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527 "2018-05-15T00:58:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 15, 2018, 12:58am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/1 "2018-05-15T00:58:42Z")

</div>

Firstly - sorry for the noob question.

I’ve written a Powershell script that is run from a play that returns in json the name of a zip file.

**Output from Powershell**

{  
“Name”: “latest-ansible v0.1.zip”  
}

_ **playbook** _

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 15, 2018, 1:58am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/2 "2018-05-15T01:58:01Z")

</div>

The issue you are seeing is that you are using the stdout\_lines return value which is the stdout of the script that was run but split into a list on each newline. You want to use the stdout return value from the script which would be the full stdout of your json. The task’s would look something like this

`

- name: Run Powershell script and return json including zip file name  
script: files/script.ps1  
register: script\_result

# this uses the filter from\_json to convert the raw json string to a

# dictionary in Ansible so you can access the variables a lot easier

- name: convert the script result to a dict in Ansible  
set\_fact:  
script\_filename: ‘{{ script\_result.stdout|from\_json }}’

- name: copy files  
win\_copy:  
src: C:\Temp{{ script\_filename.Name }} # if Name in the JSON is ‘latest-ansible v0.1.zip’, then src sent to win\_copy is ‘C:\Temp\latest-ansible v0.1.zip’  
dest: C:\Inetpub  
remote\_src: yes # you need to set this so the copy happens remote to remote, by default src is based on the local Ansible controller

`

I haven’t tested this, but see how you go, it should bring you onto the right track. A few more notes;

- You are getting warnings saying ansible\_winrm\_cert\_validation is unsupported, the variable should actually be ansible\_winrm\_server\_cert\_validation, if you remove the definition of ansible\_winrm\_cert\_validation then those warnings will go away
- In your win\_copy task, you quoted the jinja2 block, you only need to quote it when the value starts with a block like so  
`

# doesn’t need quoting as the value itself doesn’t start with {

key: value/{{ variable }}

# needs to be quoted as the value does start with {

key: “{{ variable }}”  
`

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 15, 2018, 2:17am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/3 "2018-05-15T02:17:32Z")

</div>

Thanks Jordan! That code worked a treat - really appreciate your assistance and detailed explanations below.

---

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 15, 2018, 3:17am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/4 "2018-05-15T03:17:15Z")

</div>

Jordan do you know how to strip the “.zip” off the file name? I’ve been looking at [http://jinja.pocoo.org/docs/2.10/templates/#truncate](http://jinja.pocoo.org/docs/2.10/templates/#truncate) but haven’t cracked it yet.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 15, 2018, 3:56am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/5 "2018-05-15T03:56:20Z")

</div>

Hey

There are a few ways to do this, they usually revolve around using filters to “filter” the value into something else. Ansible has a few filters available outside of the standard Jinja2 functions which can be found here [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html). In your case you want to use the splitext to split the path between the path/filename and the extension like so;

`  
src: C:\Temp{{ (script\_filename.Name|splitext)[0] }}

`

What we do here is pass the value of script\_filename.Name to the filter splitext and that ultimately returns a value, in this case returns a tuple of (path without extension, extension). We don’t want the extension so we only get the first element from the return value and because it is a 0 based index, 0 is the first element.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 15, 2018, 4:20am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/6 "2018-05-15T04:20:44Z")

</div>

Nice - thanks Jordan. That’s easier than what I’ve just done…

win\_file:  
path: C:\inetpub{{ script\_filename.Name | regex\_replace(‘.zip$’,‘’) }}

Thanks again!

---

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 17, 2018, 4:49am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/7 "2018-05-17T04:49:07Z")

</div>

Sorry - I’ve got another question(s) related to this. I’ve modified the PS script to return 2 x json outputs, each assigned to a different PS variable. Each output contains a different file name.

Is it possible to return two outputs to the playbook ran off the task that runs the PS script? Can 2 x variables in the play be registered for this task? Not just script\_result?

Another option would be to create a separate script - each script returns a different string that is parsed to playbook… or create a hashtable… @ { file1 = string1; file2 = string 2} - then convert this to json?

Assuming the hashtable is the recommended approach - the returns json could then be searched for the relevant key name?

---

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 17, 2018, 4:57am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/8 "2018-05-17T04:57:30Z")

</div>

Updated - changed script to now add the two variables into a hashtable that is converted to json - assume this is the correct approach. This can be returned to the script\_output registered in the play.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 17, 2018, 7:13am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/9 "2018-05-17T07:13:24Z")

</div>

Yep, I would create a hashtable, convert that to JSON and output that. That way you can parse that into Ansible easily and select whatever key you want. This is how the PowerShell modules work as well.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Lee\_Drew](https://avatars.discourse-cdn.com/v4/letter/l/e0b2c6/32.png) [@Lee\_Drew](https://forum.ansible.com/u/Lee_Drew)\
**Post date:** [May 17, 2018, 10:46am UTC](https://forum.ansible.com/t/file-copy-create-folder-on-windows-server-based-upon-json-output-from-powershell/27527/10 "2018-05-17T10:46:06Z")

</div>

Cool. Thanks Jordan. Works a treat.
