Windows配置Ansible Host

Windows端配置

​ (本文使用Ubuntu或CentOS作为管理机,Windows作为被管理机

  1. 配置 Windows Ansible 官方文档

  2. 跟着文档走一遍即可,关键部分在WinRM安装和设置以及 Win32-OpenSSH 的安装。

  3. 注意"设置 WinRM 侦听器"部分,CertificateThumbprint 要设置成自己powershell运行winrm enumerate winrm/config/Listener输出内容中的CertificateThumbprint 字段。

  4. 其中的 Win32-OpenSSH 我选择的是官方github wiki中的安装教程,使用的安装包链接

  5. 2021-01-26更新---------------------------------------------------

    命令安装 Openssh 即可,以上步骤可以全部省略。

    管理员打开 powershell ,运行以下命令:

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
    Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
    Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
    Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
    Start-Service sshd
    Set-Service -Name sshd -StartupType 'Automatic'
    Get-NetFirewallRule -Name *ssh*
    New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
    

    优化 Ansible 执行速度,管理员打开 powershell ,运行以下命令,来源:

    function Optimize-PowershellAssemblies {
      # NGEN powershell assembly, improves startup time of powershell by 10x
      $old_path = $env:path
      try {
        $env:path = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
        [AppDomain]::CurrentDomain.GetAssemblies() | % {
          if (! $_.location) {continue}
          $Name = Split-Path $_.location -leaf
          if ($Name.startswith("Microsoft.PowerShell.")) {
            Write-Progress -Activity "Native Image Installation" -Status "$name"
            ngen install $_.location | % {"`t$_"}
          }
        }
      } finally {
        $env:path = $old_path
      }
    }
    Optimize-PowershellAssemblies
    

Linux端配置Ansible测试

  1. 注意:

    • root 用户下或 sudo 运行之后的命令。
    • 环境:Python 2.7.5,pip install ansible==2.10.4
  2. 配置 /etc/anshible/hosts 文件:

    $sudo mkdir /etc/anshible
    $sudo vim /etc/anshible/hosts
    [test]
    192.168.10.9 ansible_connection=ssh ansible_shell_type=cmd ansible_ssh_user=admin ansible_ssh_pass=123456
    
    • 注意上面行首的 ip 和 ansible_ssh_user 以及 ansible_ssh_pass 要改成自己Windows主机的信息。
    • Win32-OpenSSH 版本早于外壳类型 v7.9.0.0p1-Beta 时 ansible_shell_type=powershell 不起作用,否则只能设置 ansible_shell_type=cmd。
    • 其次,Win32-OpenSSH 默认情况下Win32-OpenSSH将 cmd.exe 用作 shell,设置方法在文档末尾。
    • Windows 查看 openssh版本方法:
      1. cd to sshd directory (cd e:\Install\OpenSSH-Win64\OpenSSH-Win64)
      2. .\ssh.exe -V

    注意:这里使用了 ansible_ssh_pass ,所以后面要安装 sshpass ,以及 Windows 对应用户要设置登录密码(开机登陆密码)。

  3. 安装 sshpass:yum/apt install sshpass

  4. 测试 ansible,使用 win_ping 模块,输出如下表示配置成功:

    $sudo ansible all -m win_ping
    192.168.10.9 | SUCCESS => {
        "changed": false,
        "ping": "pong"
    }
    
  5. Ansible Windows 支持的所有模块见官方文档

Ansible配置免密(ssh公钥)

​ (主机A:Windows,主机B:Linux)

  1. ssh免密配置正常配置即可 (Windows 安装 OpenSSH),要注意的是主机A文件 C:\ProgramData\ssh\sshd_config 末尾应该向下面这样注释掉

    # Match Group administrators
    #       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
    
  2. 然后net stop sshd && net start sshd

posted @ 2020-12-28 17:47  Coodyzのblog  阅读(456)  评论(0编辑  收藏  举报