使用 Ansible 远程安装 PSWindowsUpdate 模块到 Windows 服务器

本文将详细介绍如何使用 Ansible 在 Linux 服务器上远程安装 PSWindowsUpdate 模块到 Windows 服务器。

1. 在 Ansible 服务器上安装 PowerShell 模块

首先,在 Ansible 服务器上安装 PowerShell 模块:

sudo apt-get update
sudo apt-get install -y powershell

2. 编写 Ansible playbook

接下来,编写一个 Ansible playbook 用于安装 PSWindowsUpdate 模块:

- name: Install pswindowsupdate
  hosts: windows
  gather_facts: no
  tasks:
    - name: Install PowerShellGet module
      win_shell: Install-Module -Name PowerShellGet -Force
      register: output
      failed_when: 'failed' in output.stderr
      changed_when: 'changed' in output.stderr
    
    - name: Install NuGet provider
      win_shell: Install-PackageProvider -Name NuGet -Force
      register: output
      failed_when: 'failed' in output.stderr
      changed_when: 'changed' in output.stderr
    
    - name: Install pswindowsupdate module
      win_shell: Install-Module -Name PSWindowsUpdate -Force
      register: output
      failed_when: 'failed' in output.stderr
      changed_when: 'changed' in output.stderr

说明:

  • hosts: windows 指示 playbook 要执行的目标主机,这里需要定义一个名为 windows 的组,包含要安装 PSWindowsUpdate 模块的 Windows 服务器。
  • gather_facts: no 表示不需要收集目标主机的 facts 信息,可以提高效率。
  • win_shell 模块用于执行 Windows 命令。
  • register: output 用于将命令执行结果存储到 output 变量中,方便后续判断是否执行成功。
  • failed_whenchanged_when 用于根据命令执行结果判断 playbook 是否失败或发生变更。

3. 运行 playbook

最后,运行 playbook:

ansible-playbook playbook.yaml -u username -k -K
  • username 是远程 Windows 服务器的用户名。
  • -k 表示使用密码登录,需要输入密码。
  • -K 表示使用 sudo 运行 playbook,需要输入 sudo 密码。

总结

通过以上步骤,即可使用 Ansible 远程安装 PSWindowsUpdate 模块到 Windows 服务器,方便进行 Windows 更新管理。

Ansible 远程安装 PSWindowsUpdate 模块到 Windows 服务器

原文地址: https://www.cveoy.top/t/topic/nnpL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录