首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在linux跳转服务器后实现对windows主机的连接

问如何在linux跳转服务器后实现对windows主机的连接
EN

Stack Overflow用户
提问于 2015-12-17 12:06:37
回答 2查看 5.6K关注 0票数 11

我想提供Windows主机,这是在子网访问,只有Linux跳转主机。

Windows机采用winrm连接方式。Linux跳转服务器可以通过SSH获得。

如果可以直接访问windows主机,则在以下方面我没有问题:

代码语言:javascript
复制
ansible_connection: winrm

如果我试图通过以下方式将任务委托给Linux跳转服务器(该服务器可以直接访问Windows):

代码语言:javascript
复制
- name: Ping windows
  hosts: windows_machines
  tasks:
    - name: ping
      win_ping:
      delegate_to: "{{ item }}"
      with_items: "{{ groups['jump_servers'][0] }}"

它试图连接到跳转主机建立WINRM连接。不完全是我想的那样。

注意,对于windows_machines组,我定义了group_vars:

代码语言:javascript
复制
ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

如何通过堡垒主机提供Windows主机?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-11 12:02:08

我的首要任务是将所有配置放在一个地方,而不是将Ansible的一部分分发给堡垒/跳转主机。我去建立了5986港口的ssh隧道。以下是完成的任务:

代码语言:javascript
复制
- 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] }}"

为此,我必须稍微修改库存文件:

代码语言:javascript
复制
[windows]
windows1 ansible_host=127.0.0.1 ansible_ssh_user=Administrator  actual_host=192.168.0.2 (...)

Ansible可以通过访问本地主机上的5986端口进行连接,因此必须将ansible_host设置为127.0.0.1,并设置有关Windows计算机实际ip的信息--自定义变量actual_host。

票数 8
EN

Stack Overflow用户

发布于 2015-12-17 17:30:17

这不是任务上的delegate_to选项所做的。

相反,delegate_to将确保任务只针对特定的节点运行,而不是在角色/剧本中列出的组上运行。

因此,例如,您可能有一个角色,该角色在一个框集群上设置MySQL,这些框是通用定义的,但随后希望单独在主服务器上执行特定的配置/任务,然后让主服务器将这些配置/任务复制到从服务器上。

您可以通过一个堡垒/跳转主机转发SSH连接,但这显然需要您的连接成为SSH,这对您没有任何帮助。

在这里,我唯一能想到的帮助您的是直接从堡垒/跳转主机上使用Ansible,这可能是从受保护区域以外的计算机触发的,可能是Ansible (或其他任何东西)触发的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34334209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档