2015-12-17 30 views
9

Windows主機我想提供Windows主機僅與Linux的跳躍主機訪問子網。如何使ansible連接到Linux背後跳服務器

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主機通過一個堡壘主機?

回答

4

我的首要任務是讓所有的配置在同一個地方,而不是分配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-%[email protected]%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-%[email protected]%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-%[email protected]%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機器自定義變量actual_host設置的實際IP信息。

3

那不是一個任務delegate_to選項一樣。

相反,delegate_to將確保該任務只能運行鍼對一個特定的節點,而不是在角色/劇本中列出的組。

因此,例如,您可能有一個角色,在通常定義的框羣集上設置MySQL,但然後希望單獨在主服務器上執行特定的配置/任務,然後讓主服務器將這些配置複製到從服務器。

你可以做SSH proxying你通過堡壘/跳轉主機轉發SSH連接,但是顯然你需要你的連接是SSH而不是對你有幫助。

我能想到的唯一能幫助你的地方就是直接從保護區外的Ansible(或其他任何真正由你的機器觸發)的堡壘/跳躍主機直接使用Ansible。