2015-12-17 11 views
9

Desidero eseguire il provisioning di host Windows che si trova nella subnet accessibile solo con Linux jump host.Come rendere ansible connettersi all'host di windows dietro il server di salto Linux

La macchina Windows utilizza il metodo di connessione winrm. Il server di salto Linux è disponibile tramite SSH.

non ho alcun problema accesso host di Windows, se disponibile direttamente con:

ansible_connection: winrm 

Se provo a delegare il compito al server salto Linux (che ha accesso diretto a Windows) da:

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

tenta di connettersi per stabilire la connessione WINRM all'host jump. Non esattamente quello che avevo in mente.

Si noti che per il gruppo windows_machines ho group_vars definito:

ansible_port: 5986 
ansible_connection: winrm 
ansible_winrm_server_cert_validation: ignore 

come dovrei fornitura host Windows attraverso un bastion host?

risposta

4

La mia priorità era di avere tutta la configurazione in un posto e non distribuire parte di Ansible all'host bastion/jump. Sono andato a stabilire il tunnel ssh per la porta 5986. Ecco il compito completo:

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

Per far funzionare tutto questo ho dovuto modificare un po 'il file di inventario:

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

Ansible può collegarsi accedendo 5986 porta su host locale, così ha ansible_host essere impostato su 127.0.0.1 e per avere le informazioni sull'ip reale della macchina Windows è impostata una variabile personalizzata actual_host.

3

Non è ciò che fa l'opzione delegate_to in un'attività.

Invece, delegate_to si assicura che l'attività venga eseguita solo su un nodo specifico anziché sul gruppo elencato nel ruolo/nella cartella.

Quindi, ad esempio, si può avere un ruolo che imposta MySQL su un cluster di riquadri definiti genericamente, ma che poi vogliono eseguire specifiche configurazioni/attività sul master, lasciando il master per poi replicarli agli slave. .

È possibile eseguire SSH proxying in cui si inoltrano le connessioni SSH tramite un host bastion/jump, ma ovviamente la connessione deve essere SSH e non è di aiuto.

L'unica cosa che posso pensare di aiutarti qui è di usare Ansible direttamente dall'host bastion/jump eventualmente attivato da Ansible (o qualsiasi altra cosa realmente) dalla tua macchina al di fuori della zona protetta.