linux - 如何让ansible连接到linux跳转服务器后面的windows主机

标签 linux ansible

我想提供子网中只能使用 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 主机?

最佳答案

我的首要任务是将所有配置放在一个地方,而不是将 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] }}"

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

[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 的信息machine 设置了自定义变量 actual_host

关于linux - 如何让ansible连接到linux跳转服务器后面的windows主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334209/

相关文章:

docker - 当我们想通过github操作执行ansible命令时如何管理ssh key 文件

python - 属性 BOLD 似乎在我的诅咒中不起作用

linux - GVIM : in/root/. vimrc 我有 ":colorscheme desert"可以工作,但如果没有人告诉我,我会在哪里找到这个值?

linux -/lib/ld-linux.so.2 : bad ELF interpreter: No such file or directory No package glibc. i386 可用

Ansible:使用 YAML 文件中的键读取单个值

在 Ansible 中创建 MySQL 数据库

linux - SDL 音频在 Linux 上随机失真

c - 有没有比 select() 和 poll() 更快的非阻塞方法来检查数据?

azure - Ansible Azure 信用

docker - Ansible 不允许我通过 SSH 连接