# Ansible deployng VM and transfering and execute shell script on remote host

**URL:** <https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386>\
**Category:** Ansible Project\
**Created:** [March 4, 2020, 6:46pm UTC](https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386 "2020-03-04T18:46:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julio\_Cesar\_Caraca](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@Julio\_Cesar\_Caraca](https://forum.ansible.com/u/Julio_Cesar_Caraca)\
**Post date:** [March 4, 2020, 6:46pm UTC](https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386/1 "2020-03-04T18:46:43Z")

</div>

Hi Guys,

I got a playbook to deploy virtual machines and it´s working very well.

And now I´m trying to transfer and execute a shell script during the deploy of virtual machine but I´m getting many issues when changing the playbook.

Has anyone been faced this need as well?

Cheers,

Julio

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 4, 2020, 7:04pm UTC](https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386/2 "2020-03-04T19:04:53Z")

</div>

> Hi Guys,
> 
> I got a playbook to deploy virtual machines and it´s working very well.
> 
> And now I´m trying to transfer and execute a shell script during the deploy of virtual machine but I´m getting many  
> issues when changing the playbook.
> 
> Has anyone been faced this need as well?

It might be helpful to detail the changes and the issues your are seeing.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Julio\_Cesar\_Caraca](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@Julio\_Cesar\_Caraca](https://forum.ansible.com/u/Julio_Cesar_Caraca)\
**Post date:** [March 5, 2020, 1:14pm UTC](https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386/3 "2020-03-05T13:14:36Z")

</div>

Hi Racke,

Thanks for the answer!

All configuration that I´ve been trying is based on this documentation:  
[https://docs.ansible.com/ansible/latest/modules/shell\_module.html](https://docs.ansible.com/ansible/latest/modules/shell_module.html)

Although I´m getting error the virtual machine is been created but without the copy and execution of shell script:

 ![Ansible_error_1.PNG](https://us1.discourse-cdn.com/ansible/original/2X/9/9266e2453ca09d4691dba49ea5811caea7ff6aa7.png)

I just added the following lines in the playbook:

- name: testing copy  
copy:  
src: /root/shell.sh  
dest: /tmp  
delegate\_to: localhost
- name: testing execute  
shell: /tmp/shell.sh

This is my playbook:

 ![Ansible_error_2.PNG](https://us1.discourse-cdn.com/ansible/original/2X/2/254148e5e49892b03f5ba9572e8715a777c2f2c9.png)

A simple shell script only for testing:

 ![Ansible_error_3.PNG](https://us1.discourse-cdn.com/ansible/original/2X/6/6109aee57d95decad930567a0d6368af783264a5.png)

Really appreciate any help.

Thanks in advance!

Julio

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 5, 2020, 1:22pm UTC](https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386/4 "2020-03-05T13:22:37Z")

</div>

> Hi Racke,
> 
> Thanks for the answer!
> 
> All configuration that I´ve been trying is based on this documentation:  
> [https://docs.ansible.com/ansible/latest/modules/shell\_module.html](https://docs.ansible.com/ansible/latest/modules/shell_module.html)
> 
> Although I´m getting error the virtual machine is been created but without the copy and execution of shell script:
> 
> Ansible\_error\_1.PNG
> 
> I just added the following lines in the playbook:
> 
> - name: testing copy  
> copy:  
> src: /root/shell.sh  
> dest: /tmp  
> delegate\_to: localhost  
> - name: testing execute  
> shell: /tmp/shell.sh

The error message states clearly "permission denied". Also there is no point the shell module for simple commands  
as explained in the documentation.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Julio\_Cesar\_Caraca](https://avatars.discourse-cdn.com/v4/letter/j/8c91f0/32.png) [@Julio\_Cesar\_Caraca](https://forum.ansible.com/u/Julio_Cesar_Caraca)\
**Post date:** [March 5, 2020, 6:59pm UTC](https://forum.ansible.com/t/ansible-deployng-vm-and-transfering-and-execute-shell-script-on-remote-host/32386/5 "2020-03-05T18:59:02Z")

</div>

Hi Racke ,

I moved the script to /tmp directory and also changed permission to 777.

In the playbook I just put:

- name: testing  
script: /tmp/shell.sh

The virtual machine is created with success and I figured out that the shell script is executed with success but in the localhost (ansible machine) instead of remote vm.

 ![Ansible_error_1.PNG](https://us1.discourse-cdn.com/ansible/original/2X/5/5003a9fa0a447b2d43f50de3e1ca97c336d957ca.png)

 ![Ansible_error_2.PNG](https://us1.discourse-cdn.com/ansible/original/2X/1/18382f619c1976b4368e6cf86e9b64426465857f.png)

Do you know some way to hit the new virtual machine instead of localhost?

Kindly Regards,

Julio
