【问题标题】:How to make ansible connect to windows host behind linux jump server如何使ansible连接到linux跳转服务器后面的windows主机
【发布时间】:2015-12-17 12:06:37
【问题描述】:

我想配置只能通过 Linux 跳转主机访问的子网中的 Windows 主机。

Windows 机器使用 winrm 连接方式。 Linux 跳转服务器可通过 SSH 访问。

如果可以直接通过以下方式访问 Windows 主机,我没有问题:

ansible_connection: winrm

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

- 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:

ansible_port: 5986
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

我应该如何通过堡垒主机配置 Windows 主机?

【问题讨论】:

    标签: linux ansible ansible-playbook


    【解决方案1】:

    我的首要任务是将所有配置集中在一个地方,而不是将 Ansible 的一部分分发给堡垒/跳转主机。我去为 5986 端口建立 ssh 隧道。 这是完整的任务:

    - 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] }}"
    

    为此,我不得不稍微修改库存文件:

    [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

    【讨论】:

    • 但这不适用于运行多个主机的端口,因为该端口只能使用一次,对吗?
    【解决方案2】:

    这不是任务上的delegate_to 选项的作用。

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

    因此,例如,您可能有一个角色在一组机器上设置 MySQL,这些机器通常定义但随后想单独在主服务器上执行特定的配置/任务,然后让主服务器将这些复制到从服务器。

    您可以通过SSH proxying 通过堡垒/跳转主机转发 SSH 连接,但这显然需要您的连接始终是 SSH,这对您没有帮助。

    我能想到的唯一能帮助你的就是直接从堡垒/跳转主机使用 Ansible,这可能是由 Ansible(或其他任何东西)从你的保护区外的机器上触发的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多