0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsをAnsibleで操作する - 初心者向けセットアップガイド

Posted at

概要

このガイドでは、Ansibleを使用してWindowsマシンを自動化するための基本的な設定と手順を解説します。インフラストラクチャ・アズ・コード(IaC)の実践的な導入例として、Windows Serverの自動化設定方法を詳しく説明します。

前提条件

操作対象マシン(Windows)

  • Windows Server 2019
  • 新規インストール状態
  • 管理者権限

Ansible実行環境(Linux)

  • Ansible Navigatorを使用(実行環境ごとに独立した環境を作成)
  • Dockerがインストール済み
  • Python 3.x

セットアップ手順

1. Windows側の設定

WinRMの設定スクリプト

HTTPS経由でのセキュアなアクセスを実現するため、以下のPowerShellスクリプトを作成します。

# 自己署名証明書の作成
$certParams = @{
    CertStoreLocation = 'Cert:\LocalMachine\My'
    DnsName           = $env:COMPUTERNAME
    NotAfter          = (Get-Date).AddYears(1)
    Provider          = 'Microsoft Software Key Storage Provider'
    Subject           = "CN=$env:COMPUTERNAME"
}
$cert = New-SelfSignedCertificate @certParams

# HTTPSリスナーの設定
$httpsParams = @{
    ResourceURI = 'winrm/config/listener'
    SelectorSet = @{
        Transport = "HTTPS"
        Address   = "*"
    }
    ValueSet = @{
        CertificateThumbprint = $cert.Thumbprint
        Enabled               = $true
    }
}
New-WSManInstance @httpsParams

# ファイアウォールルールの設定(ポート5986を開放)
$firewallParams = @{
    Action      = 'Allow'
    Description = 'Inbound rule for Windows Remote Management via WS-Management. [TCP 5986]'
    Direction   = 'Inbound'
    DisplayName = 'Windows Remote Management (HTTPS-In)'
    LocalPort   = 5986
    Profile     = 'Any'
    Protocol    = 'TCP'
}
New-NetFirewallRule @firewallParams

2. Ansible環境の構築

実行環境の設定

以下のパッケージとコレクションをインストールします:

version: 3

images:
  base_image:
    name: ghcr.io/ansible-community/community-ee-base:2.18.1-1

dependencies:
  ansible_core:
    package_pip: ansible-core
  ansible_runner:
    package_pip: ansible-runner
  system:
  - unzip
  python:
  - pywinrm
  - pypsrp
  galaxy:
    collections:
    - name: ansible.windows
    - name: community.windows

インベントリの設定

WinRM接続用
[win]
114.51.48.10

[win:vars]
ansible_user=administrator
ansible_password=nandemo
ansible_connection=winrm
ansible_winrm_transport=ntlm
ansible_winrm_server_cert_validation=ignore
PSRP接続用
[win]
114.51.48.10

[win:vars]
ansible_user=administrator
ansible_password=nandemo
ansible_connection=psrp
ansible_psrp_auth=ntlm
ansible_psrp_cert_validation=ignore

動作確認用Playbook

---
- hosts: all
  gather_facts: false

  tasks:
    - name: コマンド実行
      win_command: whoami
      register: return_whoami
    - name: 結果表示
      debug:
        msg: "{{ return_whoami }}"

Ansible Navigator設定

---
ansible-navigator:
  ansible:
    inventory:
      help: False
      entries:
        - ./inventories/hosts

  execution-environment:
    image: local/ansible4win:2.18.1-1
  logging:
    level: critical
  mode: stdout
  time-zone: Japan

実行手順

  1. 実行環境のビルド
ansible-builder build --tag local/ansible4win:2.18.1-1
  1. Playbookの実行
ansible-navigator run --ep playbooks/cmd.yml

よくある質問(FAQ)

Q1: WinRMとPSRPの違いは何ですか?

A1: WinRMは従来のWindows Remote Managementプロトコルで、PSRPはPowerShell Remoting Protocolの略称です。PSRPはより新しいプロトコルで、PowerShellの機能をより効果的に活用できます。

Q2: セキュリティ上の注意点はありますか?

A2: 本番環境では、自己署名証明書ではなく、信頼できる認証局の証明書を使用することを推奨します。また、強力なパスワードポリシーと適切なアクセス制御を設定してください。

参考リンク

本記事について

上記リンクの記事に対して、Cursorで以下の指示を出してリライトしてもらいました。

You are an editor. Improve clarity, tone, and structure.
You are an SEO expert. Add relevant keywords and optimize structure for SEO.
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?