win_copy azure ansible module not working

[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,**_

Hi ,

Error says in dest. Can you try to put in any other destination with absolute path ?

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.

Please suggest any other alternative…

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.

[1] https://docs.ansible.com/ansible/latest/modules/add_host_module.html

Thanks

Jordan

Hi Jordan,

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

its a module failure…is the localhost linux or windows.???..win_copy works for windows …for rest use copy module

localhost is windows. And we are using win_copy for copying the file. Any alternative for this?

If u want to copy from local to remote u should mention -host: remote_host…i think u r using localhost

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…!!!

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

  1. The directory where your playbook and inventory file reside also have a directory called “files”.

  2. 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

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)

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:
https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html#inventory-options

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

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

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.

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.

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.

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