win_copy azure ansible module not working

164 views
Skip to first unread message

gottumukkala srija

unread,
Apr 9, 2019, 6:19:18 AM4/9/19
to Ansible Project

Hi Team,

We are trying to automate the installation of tools in azure using ansible. VM spin up is successful and we are trying to invoke the powershell script(code for tools installation) so the files has to be installed on the server.

Tried number of ways using win_copy command for copying the files to remote server but no luck.


ANSIBLE VERSION:

ansible 2.7.9

  config file = None

  ansible python module location = /opt/ansible/local/lib/python2.7/site-packages/ansible

  executable location = /opt/ansible/bin/ansible

  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]


Below is the code snippet used for copying files from local to remote server:


- name: Copy script from local to remote server

    win_copy:

       src: files

       dest: C:\

       register: myfile

  - debug: var=myfile

  - win_command: powershell.exe -ExecutionPolicy ByPass -File C:/userdata-winsetup.ps1


Below are the inventory file details:


[windows]

127.0.0.1

[windows:vars]

        ansible_user: admin

        ansible_password: admin

        ansible_connection: winrm

        ansible_winrm_transport: basic

        ansible_port: 5986

        ansible_winrm_server_cert_validation: ignore

        validate_certs: false


Results while running ansible playbook:


fatal: [localhost]: FAILED! => {

    "changed": false,

    "dest": "C:\\",

    "module_stderr": "",

    "module_stdout": "",

    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",

    "operation": "folder_copy",

    "rc": 0,**_

 

 

vinoth kumar

unread,
Apr 10, 2019, 1:26:21 AM4/10/19
to ansible...@googlegroups.com
Hi ,
Error says in dest. Can you try to put in any other destination with absolute path ?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4aa2d698-e3c0-41ae-8739-916e988abac0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gottumukkala srija

unread,
Apr 10, 2019, 5:58:03 AM4/10/19
to Ansible Project
Hi,
We have tried changing the destination to C://Toolsinstall/.
But the problem here is win_copy , which is not allowing to copy the file to remote server.


On Wednesday, April 10, 2019 at 10:56:21 AM UTC+5:30, vinoth kumar wrote:
Hi ,
Error says in dest. Can you try to put in any other destination with absolute path ?
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

gottumukkala srija

unread,
Apr 10, 2019, 5:58:31 AM4/10/19
to Ansible Project
Please suggest any other alternative...

Jordan Borean

unread,
Apr 11, 2019, 3:00:28 AM4/11/19
to Ansible Project
If your inventory is targeting localhost then that means it's trying to run a Windows module on the Ansible controller which won't work. You cna use the azure_rm_virtualmachine module to spin up a host but then you will have to use something like 'add_host' [1] to add the host for the current execution then change the play to that new host.


Thanks

Jordan

gottumukkala srija

unread,
Apr 12, 2019, 4:31:46 AM4/12/19
to Ansible Project
Hi Jordan,

Can you please describe it briefly? You want us to create a new VM and then point that IP to add_host?

CORLEONE##

unread,
Apr 12, 2019, 4:57:27 AM4/12/19
to ansible...@googlegroups.com
its a module failure...is the localhost linux or windows.???...win_copy works for windows ...for rest use copy module

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

gottumukkala srija

unread,
Apr 12, 2019, 5:06:45 AM4/12/19
to Ansible Project
localhost is windows. And we are using win_copy for copying the file. Any alternative for this?


On Friday, April 12, 2019 at 2:27:27 PM UTC+5:30, CORLEONE## wrote:
its a module failure...is the localhost linux or windows.???...win_copy works for windows ...for rest use copy module

On Fri, 12 Apr 2019, 14:02 gottumukkala srija, <srij...@gmail.com> wrote:
Hi Jordan,

Can you please describe it briefly? You want us to create a new VM and then point that IP to add_host?

On Thursday, April 11, 2019 at 12:30:28 PM UTC+5:30, Jordan Borean wrote:
If your inventory is targeting localhost then that means it's trying to run a Windows module on the Ansible controller which won't work. You cna use the azure_rm_virtualmachine module to spin up a host but then you will have to use something like 'add_host' [1] to add the host for the current execution then change the play to that new host.


Thanks

Jordan

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

CORLEONE##

unread,
Apr 12, 2019, 6:32:12 AM4/12/19
to ansible...@googlegroups.com
If u want to copy from local to remote u should mention -host: remote_host....i think u r using localhost

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

gottumukkala srija

unread,
Apr 15, 2019, 12:53:45 AM4/15/19
to Ansible Project
I changed localhost to remote_host as suggested, but while running the playbook getting error as "no hosts matched". 

Is there any possible way that we can get on call to discuss further....Please suggest..!!!


On Friday, April 12, 2019 at 4:02:12 PM UTC+5:30, CORLEONE## wrote:
If u want to copy from local to remote u should mention -host: remote_host....i think u r using localhost

On Fri, 12 Apr 2019, 14:36 gottumukkala srija, <srij...@gmail.com> wrote:
localhost is windows. And we are using win_copy for copying the file. Any alternative for this?

On Friday, April 12, 2019 at 2:27:27 PM UTC+5:30, CORLEONE## wrote:
its a module failure...is the localhost linux or windows.???...win_copy works for windows ...for rest use copy module

On Fri, 12 Apr 2019, 14:02 gottumukkala srija, <srij...@gmail.com> wrote:
Hi Jordan,

Can you please describe it briefly? You want us to create a new VM and then point that IP to add_host?

On Thursday, April 11, 2019 at 12:30:28 PM UTC+5:30, Jordan Borean wrote:
If your inventory is targeting localhost then that means it's trying to run a Windows module on the Ansible controller which won't work. You cna use the azure_rm_virtualmachine module to spin up a host but then you will have to use something like 'add_host' [1] to add the host for the current execution then change the play to that new host.


Thanks

Jordan

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8254df4c-e104-405f-b9b8-08080ce5e0e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

d...@linder.org

unread,
Apr 15, 2019, 2:27:28 PM4/15/19
to Ansible Project
Please clarify a couple questions:

1. You're running the ansible command from a Linux (RedHat, Ubuntu, CentOS, Debian) system, correct?

2. The playbook you're using has these tasks:

- name: Copy script from local to remote server

    win_copy:

       src: files

       dest: C:\

       register: myfile

  - debug: var=myfile

  - win_command: powershell.exe -ExecutionPolicy ByPass -File C:/userdata-winsetup.ps1


3. The directory where your playbook and inventory file reside also have a directory called "files".


4. Your inventory file has this section:

[windows]

127.0.0.1


Based on your inventory file, you're attempting to run the commands in the playbook on the system you're running Ansible on.  Your inventory file probably needs the _windows_ server listed in the "[windows]" section like this:


[windows]

MyWinSys01.example.com


Then when you run your playbook with Ansible, it will connect using the "winrm" protocol and login with "admin" (password: "admin"), then copy the files from ".\files\" to "c:\" on the remote system.


Once you've validated all that, please re-run the playbook with "-v" (or more for additional verbose output) and post it here.


Thanks,

DanL


gottumukkala srija

unread,
Apr 16, 2019, 11:12:08 AM4/16/19
to Ansible Project

Hi,

We are running the playbook via azure cloud shell "bash" editor.

The source file and the inventory are placed in the same folder and while running playbook we are invoking the playbook and below is the command:

ansible-playbook -i inventory wincopy.yml

We are getting the below error:
"fatal: [remote host]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host remote_host port 22: Connection timed out", "unreachable": true}"

We are trying to copy the file from Azure cloud shell to the remote machine, but the error says ssh connection time out(Ideally it should be windows exception as we are using windows but it's trying ssh like linux machine)

d...@linder.org

unread,
Apr 17, 2019, 8:57:37 AM4/17/19
to Ansible Project
I don't do much Ansible work on Windows systems, but the message "Failed to connect to host via ssh" seems to imply that the connection type for that remote host isn't setup for Windows.  At minimum you'll need to setup these connection variables (connection type, user, and password) as shown here:

Once you get that setup, try re-running your playbook but add a "-vvv" (verbose output) to show more details on the error:
    ansible-playbook -i inventory -vvv wincopy.yml

gottumukkala srija

unread,
Apr 18, 2019, 12:19:15 AM4/18/19
to Ansible Project
Hi,

We are using the mentioned connection variables and below are the inventory file details:

[windows]
remote_host
[windows:vars]
        ansible_user: admin
        ansible_password: admin
        ansible_connection: winrm
        ansible_winrm_transport: basic
        ansible_port: 5986
        ansible_winrm_server_cert_validation: ignore
        validate_certs: false

And executed playbook as mentioned by placing -vvv:

[WARNING]: Unable to parse /home/f2/.ansible/inventory as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAYBOOK: wincopy.yml ***************************************************************************************************************************
1 plays in wincopy.yml

PLAY [Configurations for windows instance] ******************************************************************************************************
META: ran handlers

TASK [A single debug statement] *****************************************************************************************************************
task path: /home/f2/.ansible/wincopy.yml:7
<remote_host> ESTABLISH SSH CONNECTION FOR USER: None
<remote_host> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/f2/.ansible/cp/56de033974 [WARNING]: Unable to parse /home/f2/.ansible/inventory as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAYBOOK: wincopy.yml ***************************************************************************************************************************
1 plays in wincopy.yml

PLAY [Configurations for windows instance] ******************************************************************************************************
META: ran handlers

TASK [A single debug statement] *****************************************************************************************************************
task path: /home/f2/.ansible/wincopy.yml:7
<remote_host> ESTABLISH SSH CONNECTION FOR USER: None
<remote_host> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/f2c34c90-9efb-4ea2-afcc-f24ace01/.ansible/cp/56de033974 40.71.47.192 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<remote_host> (255, '', 'ssh: connect to host remote_host port 22: Connection timed out\r\n')
fatal: [remote_host]: UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host remote_host port 22: Connection timed out",
    "unreachable": true
}
        to retry, use: --limit @/home/f2/.ansible/wincopy.retry

PLAY RECAP **************************************************************************************************************************************
remote_host               : ok=0    changed=0    unreachable=1    failed=0 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<remote_host> (255, '', 'ssh: connect to host remote_host port 22: Connection timed out\r\n')
fatal: [remote_host]: UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host remote_host port 22: Connection timed out",
    "unreachable": true
}
        to retry, use: --limit @/home/f2/.ansible/wincopy.retry

PLAY RECAP **************************************************************************************************************************************
remote_host               : ok=0    changed=0    unreachable=1    failed=0

d...@linder.org

unread,
Apr 18, 2019, 9:11:08 AM4/18/19
to Ansible Project

On Thursday, April 18, 2019 at 4:19:15 AM UTC, gottumukkala srija wrote:
 [WARNING]: Unable to parse /home/f2/.ansible/inventory as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

Somethign is wrong with your inventory file (format, access, etc) - this is why it's not connecting to the Windows system using your connection information.  Fix this and reply with the results.

gottumukkala srija

unread,
Apr 22, 2019, 6:28:42 AM4/22/19
to Ansible Project
Hi,

I tried changing the format of the inventory but still the same issue. Not sure what else to be done. I mentioned all the necessary modules for windows connection.

Do you anything to suggest here?

Thanks in Advance.

d...@linder.org

unread,
Apr 22, 2019, 8:57:28 AM4/22/19
to Ansible Project
You could upload all these files (playbook, inventory, sample file to copy, etc) to a temporary GitHub.com or GitLab.com repository for us to look at it.  The "unable to parse inventory" probably means formatting or other weirdness, not a bug or problem with Ansible.

Jordan Borean

unread,
Apr 22, 2019, 5:24:40 PM4/22/19
to Ansible Project
The first thing you want to do is create an inventory with a Windows group that contains the generic WinRM connection vars like;

[windows]

[windows:vars]
ansible_connection
=winrm
ansible_port
=5986
ansible_winrm_transport
=ntlm
ansible_winrm_server_cert_validation
=ignore

This group is empty as we use add_host to add the new Azure VM to the host mid play. This host will be an in memory host and is not persisted once the play is complete. If you want to persist this then you should look at using a dynamic inventory but that is outside of the scope here. The reason we do this is to define generic connection vars like 'ansible_connection=winrm', 'ansible_winrm_server_cert_validation=ignore' so we don't have to do it in the play. You could define more vars here or in a group_vars file but that's dependent on what your play does.

From there you want a play similar to

- name: provision Windows host in Azure
  hosts
: localhost
  gather_facts
: no
  vars
:
    azure_resource_group
: RG01
    azure_vm_name
: WindowsVM01
    windows_username
: azure
    windows_password
: 's=3@W%qTDYtm%nWW'

  tasks
:
 
# This is not a full task, you still need to do the work around creating the resource group and other components.
 
# This is just a brief example to show you how to provision the Windows host, enable WinRM, and then connect to it
 
# with Ansible.
 
- name: provision Windows VM
    azure_rm_virtualmachine
:
      admin_username
: '{{ windows_username }}'
      admin_password
: '{{ windows_password }}'
      os_type
: Windows
      image
:
        offer
: WindowsServer
        publisher
: MicrosoftWindowsServer
        sku
: 2016-Datacenter
        version
: latest
      name
: '{{ azure_vm_name }}'
      resource_group
: '{{ azure_resource_group }}'
      state
: present
   
register: azure_vm

 
- name: set fact for VM extension PS script to run
    set_fact
:
     
# You should be sourcing your own script and not this one on GitHub. This is just an example of how to download
     
# a PS script from a URL and then invoke it using the VM extension.
      azure_vm_extension_ps
: Invoke-Expression -Command ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1'))

 
- name: set fact for VM extension setings
      azure_vm_extension
:
        commandToExecute
: powershell.exe -ExecutionPolicy ByPass -EncodedCommand {{ azure_vm_extension_ps | string | b64encode(encoding='utf-16-le') }}

 
- name: set fact for newly create VM public IP address
    set_fact
:
      azure_vm_ip
: '{{ azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.publicIPAddress.properties.ipAddress }}'

 
- name: create Azure VM extension to enable HTTPS WinRM listener
    azure_rm_virtualmachine_extension
:
      name
: winrm-extension
      resource_group
: '{{ azure_resource_group }}'
      virtual_machine_name
: '{{ azure_vm_name }}'
      publisher
: Microsoft.Compute
      virtual_machine_extension_type
: CustomScriptExtension
      type_handler_version
: 1.9
      settings
: '{{ azure_vm_extension | to_json }}'
      auto_upgrade_minor_version
: yes

 
- name: wait for the WinRM port to come online
    wait_for
:
      port
: 5986
      host
: '{{ azure_vm_ip }}'
      timeout
: 600

 
- name: add new Azure VM as a Windows host
    add_host
:
      name
: '{{ azure_vm_ip }}'
      groups
: windows
      ansible_user
: '{{ windows_username }}'
      ansible_password
: '{{ windows_password }}'

# Now that we have provisioned our new Windows host and added it to the in memory host inventory, we change our play
# to run on the windows group and then run our script
- name: deploy configuration to Windows host in Azure
  hosts
: windows
  gather_facts
: no
  tasks
:
 
- name: wait for the WinRM connection to be fully reachable
    wait_for_connection
:

 
- name: gather facts about host
    setup
:

 
# Your example had you copying the script with win_copy then invoking it. Unless there were other files there, it is
 
# simpler to just use script instead.
 
- name: invoke setup script
    script
: userdata-winsetup.ps1

This playbook contains 2 plays;

  1. The first play runs on localhost (Ansible controller) to provision the Azure VM so that Ansible can talk to it
  2. The second play is targeted towards the 'windows' group which should contain our new Azure VM as the 1st play uses add_host
I've left some comments in line to explain some of the steps a bit more but hopefully it is mostly self explanatory. I haven't actually tested this so there could be some mistakes but the general workflow should work and give you enough info to continue onwards.
Thanks

Jordan
Reply all
Reply to author
Forward
0 new messages