title: Execute cmd commands in Windows target host from linux host using ansible
body: <p>I am trying to reach out to the Windows target VM that I have created in Azure portal. But even after enabling winrm on port 5986, I am unable to reach that target vm using ansible host.</p>
  
  <p>VM was created using below playbook file create_win.yml:</p>
  
  <pre><code>- hosts: localhost
    tasks:
      - name: Prepare random postfix
        set_fact:
          rpfx: "{{ 100000 | random }}"
        run_once: yes
  
  - name: provision new azure host
    hosts: localhost
    connection: local
    vars:
      resource_group: myTestRG
      vm_name: wintestvm{{ rpfx }}
      vm_user: azureuser
      vm_password: MyPassword123!!!
      location: eastus
  
      # Below is UTF-16 Base64 encoding for:
      #   Invoke-Expression -Command ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1')); Enable-WSManCredSSP -Role Server -Force
      winrm_enable_script: SQBuAHYAbwBrAGUALQBFAHgAcAByAGUAcwBzAGkAbwBuACAALQBDAG8AbQBtAGEAbgBkACAAKAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABTAHkAcwB0AGUAbQAuAE4AZQB0AC4AVwBlAGIAQwBsAGkAZQBuAHQAKQAuAEQAbwB3AG4AbABvAGEAZABTAHQAcgBpAG4AZwAoACcAaAB0AHQAcABzADoALwAvAHIAYQB3AC4AZwBpAHQAaAB1AGIAdQBzAGUAcgBjAG8AbgB0AGUAbgB0AC4AYwBvAG0ALwBhAG4AcwBpAGIAbABlAC8AYQBuAHMAaQBiAGwAZQAvAGQAZQB2AGUAbAAvAGUAeABhAG0AcABsAGUAcwAvAHMAYwByAGkAcAB0AHMALwBDAG8AbgBmAGkAZwB1AHIAZQBSAGUAbQBvAHQAaQBuAGcARgBvAHIAQQBuAHMAaQBiAGwAZQAuAHAAcwAxACcAKQApADsAIABFAG4AYQBiAGwAZQAtAFcAUwBNAGEAbgBDAHIAZQBkAFMAUwBQACAALQBSAG8AbABlACAAUwBlAHIAdgBlAHIAIAAtAEYAbwByAGMAZQA=
  
    tasks:
  
    - name: create Azure virtual network in resource group
      azure_rm_virtualnetwork:
        name: "{{ vm_name }}"
        resource_group: "{{ resource_group }}"
        address_prefixes_cidr:
        - 10.1.0.0/16
        state: present
  
    - name: create Azure subnet in virtualnetwork
      azure_rm_subnet:
        name: '{{ vm_name }}'
        state: present
        virtual_network_name: "{{ vm_name }}"
        resource_group: "{{ resource_group }}"
        address_prefix_cidr: 10.1.0.0/24
  
    - name: create Azure storage account
      azure_rm_storageaccount:
        name: '{{ vm_name }}'
        resource_group: "{{ resource_group }}"
        account_type: Standard_LRS
  
    - name: provision new Azure virtual host
      azure_rm_virtualmachine:
        admin_username: '{{ vm_user }}'
        admin_password: "{{ vm_password }}"
        os_type: Windows
        image:
          offer: WindowsServer
          publisher: MicrosoftWindowsServer
          sku: 2016-Datacenter
          version: latest
        name: "{{ vm_name }}"
        resource_group: "{{ resource_group }}"
        state: present
        vm_size: Standard_D1
        storage_account_name: "{{ vm_name }}"
        virtual_network_name: "{{ vm_name }}"
        subnet_name: "{{ vm_name }}"
  
    - name: create Azure vm extension to enable HTTPS WinRM listener
      azure_rm_virtualmachine_extension:
        name: winrm-extension
        resource_group: "{{ resource_group }}"
        virtual_machine_name: "{{ vm_name }}"
        publisher: Microsoft.Compute
        virtual_machine_extension_type: CustomScriptExtension
        type_handler_version: 1.9
        settings: '{"commandToExecute": "powershell.exe -ExecutionPolicy ByPass -EncodedCommand {{winrm_enable_script}}"}'
        auto_upgrade_minor_version: true
  
    - name: wait for the WinRM port to come online
      wait_for:
        port: 5986
        host: '{{azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties.publicIPAddress.properties.ipAddress}}'
        timeout: 600
  </code></pre>
  
  <p>I created inventory.txt file with below content:</p>
  
  <pre><code>target1 ansible_host=&lt;my-target-ip-pasted-here&gt; ansible_user=azureuser ansible_password=MyPassword123!!! ansible_connection=winrm ansible_winrm_server_cert_validation=ignore
  </code></pre>
  
  <p>After running:</p>
  
  <pre><code>ansible target1 -m ping -i inventory.txt
  </code></pre>
  
  <p>I am getting below output:</p>
  
  <pre><code>[WARNING]: No python interpreters found for host target1 (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-python', '/usr/bin/python3', 'python'])
  
  target1 | FAILED! =&gt; {
      "ansible_facts": {
          "discovered_interpreter_python": "/usr/bin/python"
      },
      "changed": false,
      "module_stderr": "Exception calling \"Create\" with \"1\" argument(s): \"At line:4 char:21\r\n+ def _ansiballz_main():\r\n+                     ~\r\nAn expression was expected after '('.\r\nAt line:13 char:27\r\n+     except (AttributeError, OSError):\r\n+                           ~\r\nMissing argument in parameter list.\r\nAt line:15 char:7\r\n+     if scriptdir is not None:\r\n+       ~\r\nMissing '(' after 'if' in if statement.\r\nAt line:22 char:7\r\n+     if sys.version_info &lt; (3,):\r\n+       ~\r\nMissing '(' after 'if' in if statement.\r\nAt line:22 char:30\r\n+     if sys.version_info &lt; (3,):\r\n+                              ~\r\nMissing expression after ','.\r\nAt line:22 char:25\r\n+     if sys.version_info &lt; (3,):\r\n+                         ~\r\nThe '&lt;' operator is reserved for future use.\r\nAt line:24 char:32\r\n+         MOD_DESC = ('.py', 'U', imp.PY_SOURCE)\r\n+                                ~\r\nMissing expression after ','.\r\nAt line:24 char:33\r\n+         MOD_DESC = ('.py', 'U', imp.PY_SOURCE)\r\n+                                 ~~~~~~~~~~~~~\r\nUnexpected token 'imp.PY_SOURCE' in expression or statement.\r\nAt line:24 char:32\r\n+         MOD_DESC = ('.py', 'U', imp.PY_SOURCE)\r\n+                                ~\r\nMissing closing ')' in expression.\r\nAt line:24 char:46\r\n+         MOD_DESC = ('.py', 'U', imp.PY_SOURCE)\r\n+                                              ~\r\nUnexpected token ')' in expression or statement.\r\nNot all parse errors were reported.  Correct the reported errors and try again.\"\r\nAt line:6 char:1\r\n+ $exec_wrapper = [ScriptBlock]::Create($split_parts[0])\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException\r\n    + FullyQualifiedErrorId : ParseException\r\n \r\nThe expression after '&amp;' in a pipeline element produced an object that was not valid. It must result in a command \r\nname, a script block, or a CommandInfo object.\r\nAt line:7 char:2\r\n+ &amp;$exec_wrapper\r\n+  ~~~~~~~~~~~~~\r\n    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException\r\n    + FullyQualifiedErrorId : BadExpression\r\n ",
      "module_stdout": "",
      "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
      "rc": 1
  }
  ankur512512@testvmansible-ankur:~$
  </code></pre>
  
  <p>Just to let you know, target machine already has python installed and tried to copy it to /usr/bin path as well there but still my ansible is not able to communicate with that windows machine. Can someone please help?</p>