How to make ansible connect to windows host behind linux jump server

kwach

I want to provision Windows host that is in subnet accessible only with Linux jump host.

Windows machine uses winrm connection method. Linux jump server is available via SSH.

I have no problem accessing windows host if available directly with:

ansible_connection: winrm

If I try to delegate the task to the Linux jump server (that has direct access to Windows) by:

- name: Ping windows
  hosts: windows_machines
  tasks:
    - name: ping
      win_ping:
      delegate_to: "{{ item }}"
      with_items: "{{ groups['jump_servers'][0] }}"

it tries to connect to establish WINRM connection to the jump host. Not exactly what I had in mind.

Note that for windows_machines group I have group_vars defined:

ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

How should I provision Windows hosts via a bastion host?

kwach

My priority was to have all the configuration in one place and not distribute part of Ansible to the bastion/jump host. I went for establishing ssh tunnel for the 5986 port. Here is the complete task:

- name: Tunneled configuration of Windows host in a subnet
  hosts: windows
  connection: local #This is the trick to connect to localhost not actual host
  gather_facts: no
  tasks:
    - name: First setup a tunnel
      local_action: command ssh -Nf -4 -o ControlPersist=1m -o ControlMaster=auto -o ControlPath="~/.ssh/mux2win-%r@%h:%p" -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o UserKnownHostsFile="/dev/null" -i {{ hostvars[item].ansible_ssh_private_key_file }} {{ hostvars[item].ansible_ssh_user }}@{{ hostvars[item].ansible_host }} -L {{ ansible_port }}:{{ actual_host }}:{{ ansible_port }}
      with_items:
        - "{{ groups['jump_servers'][0] }}" #I know my topology so I know which host to use
    - name: (optional) Second ensure it is up
      local_action: command ssh -O check -S "~/.ssh/mux2win-%r@%h:%p" {{ hostvars[item].ansible_ssh_user }}@{{ hostvars[item].ansible_host }}
      with_items:
        - "{{ groups['jump_servers'][0] }}"

    # ------- actual windows tasks (from ansible examples) ------------
    - name: Ping
      connection: local
      win_ping:
    - name: test raw module- run ipconfig
      raw: ipconfig
      register: ipconfig
    - debug: var=ipconfig

    - name: Test stat module- test stat module on file
      win_stat: path="C:/Windows/win.ini"
      register: stat_file

    - debug: var=stat_file

    - name: Check stat_file result
      assert:
          that:
             - "stat_file.stat.exists"
             - "not stat_file.stat.isdir"
             - "stat_file.stat.size > 0"
             - "stat_file.stat.md5"
    # ------- end of actual windows tasks ------------

    - name: Stop the tunnel. It would stop anyway after 1m.
      local_action: command ssh -O stop -S "~/.ssh/mux2win-%r@%h:%p" {{ hostvars[item].ansible_ssh_user }}@{{ hostvars[item].ansible_host }}
      with_items:
        - "{{ groups['jump_servers'][0] }}"

For this to work I had to modify slightly the inventory file:

[windows]
windows1 ansible_host=127.0.0.1 ansible_ssh_user=Administrator  actual_host=192.168.0.2 (...)

Ansible can connect by accessing 5986 port on local host, so ansible_host has to be set to 127.0.0.1 and to have the information on the actual ip of the Windows machine a custom variable actual_host is set.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In Ansible, How to connect to windows host?

From Dev

How to connect to SSH server behind another server?

From Dev

Can't connect to web-server on local host behind NAT

From Dev

How to connect to the server behind NAT using mosh

From Dev

How to connect my Rails App to SQL Server from Ubuntu Guest to Windows 8.1 Host?

From Dev

Cannot connect to windows server with Domain Account using Ansible

From Dev

How to Make Virtual host in xampp on Windows 7

From Dev

How to connect a client behind a NAT router to an Asterisk server behind a NAT router?

From Dev

How to make a jump sprite

From Dev

How to transfer files from remote server to my local desktop using jump host (? gateway server?)

From Dev

Will Linux as a guest be able to make use of hardware support of a Windows host?

From Dev

Gateway Server Jump Bastion Host Local Setup

From Dev

Connect PHP on Linux server to MS SQL on remote server running Windows

From Dev

How connect to Microsoft Azure (MSA) Windows Server (WIN) with Remote Desktop (RDP) in Linux

From Dev

How to connect to a remote server with my SSH keypair, through the new Windows 10 Linux bash?

From Dev

How do connect to a Windows NT 4.0 Terminal Server from a Linux machine?

From Dev

Windows 10 native SSH jump host error

From Dev

Server running on guest Linux VM will not accept connections from Windows host

From Java

How to connect to a docker container from outside the host (same network) [Windows]

From Dev

Ansible-Failed to connect to the host via ssh

From Dev

Is it possible to host a web server from behind a NAT

From Dev

How to connect JACK Audio from windows to linux?

From Dev

How to connect to AWS windows server without rdp?

From Dev

How to use IIS WCF Host Service over internet when server is behind NAT

From Dev

Windows XP cannot connect to samba share (linux server 20.04)

From Java

How to take backup of Windows server in Linux Server

From Dev

How to make a character jump in Pygame?

From Dev

Windows 10 update cannot connect (behind a firewall)

From Dev

Trying to make a dynamic host file for ansible in python

Related Related

  1. 1

    In Ansible, How to connect to windows host?

  2. 2

    How to connect to SSH server behind another server?

  3. 3

    Can't connect to web-server on local host behind NAT

  4. 4

    How to connect to the server behind NAT using mosh

  5. 5

    How to connect my Rails App to SQL Server from Ubuntu Guest to Windows 8.1 Host?

  6. 6

    Cannot connect to windows server with Domain Account using Ansible

  7. 7

    How to Make Virtual host in xampp on Windows 7

  8. 8

    How to connect a client behind a NAT router to an Asterisk server behind a NAT router?

  9. 9

    How to make a jump sprite

  10. 10

    How to transfer files from remote server to my local desktop using jump host (? gateway server?)

  11. 11

    Will Linux as a guest be able to make use of hardware support of a Windows host?

  12. 12

    Gateway Server Jump Bastion Host Local Setup

  13. 13

    Connect PHP on Linux server to MS SQL on remote server running Windows

  14. 14

    How connect to Microsoft Azure (MSA) Windows Server (WIN) with Remote Desktop (RDP) in Linux

  15. 15

    How to connect to a remote server with my SSH keypair, through the new Windows 10 Linux bash?

  16. 16

    How do connect to a Windows NT 4.0 Terminal Server from a Linux machine?

  17. 17

    Windows 10 native SSH jump host error

  18. 18

    Server running on guest Linux VM will not accept connections from Windows host

  19. 19

    How to connect to a docker container from outside the host (same network) [Windows]

  20. 20

    Ansible-Failed to connect to the host via ssh

  21. 21

    Is it possible to host a web server from behind a NAT

  22. 22

    How to connect JACK Audio from windows to linux?

  23. 23

    How to connect to AWS windows server without rdp?

  24. 24

    How to use IIS WCF Host Service over internet when server is behind NAT

  25. 25

    Windows XP cannot connect to samba share (linux server 20.04)

  26. 26

    How to take backup of Windows server in Linux Server

  27. 27

    How to make a character jump in Pygame?

  28. 28

    Windows 10 update cannot connect (behind a firewall)

  29. 29

    Trying to make a dynamic host file for ansible in python

HotTag

Archive