テクノロジー DEVOPS Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

Ubuntu 上で実行されている Ansible から Windows サーバーに接続する方法を簡単に説明します。

以下の手順に従うには、両方のシステムに Python 3.x と Ansible がインストールされている必要があります。サポートが必要な場合は、以下の記事を参照してください。

Ubuntu に Ansible をインストールして構成するにはどうすればよいですか?

Windows に Ansible をインストールするにはどうすればよいですか?

以下は、私が使用している両方のサーバーの詳細です。

  • Ansible コントローラー – 192.168.0.108
  • Windowsサーバー – 192.168.0.102
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

ステップ 1: Ansible Windows ユーザーを作成する

Ansible Windows 接続セットアップ用の新しいユーザーを作成します。

  • Windows システムで [コンピュータの管理] を開き、[ローカル ユーザーとグループ] に移動します。
  • 「ユーザー」を右クリックし、新しいユーザーを作成します。
  • 「パスワードを無期限にする」チェックボックスを選択し、「作成」をクリックします。

アンシブルユーザー
アンシブルユーザー
アンシブルユーザー

  • 使用可能なグループの中から、Administrators グループを右クリックし、プロパティをクリックします。
  • 「追加」をクリックし、オブジェクト名に「ansible」と入力します。
  • [名前の確認] オプションをクリックし、[OK] をクリックします。

ansible グループ
ansible グループ
ansible グループ

これで、Windows マシン上の Ansible ユーザーの準備が整いました。

Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

ステップ 2: ライブラリと WinRM をセットアップする

Ansible コントローラー マシンに移動して更新し、以下で説明するライブラリをインストールします。

 @:~$ sudo apt-get update
@:~$ sudo apt-get install gcc python-dev
@:~$ sudo apt install python3-pip

WinRM は Windows リモート管理の略です。これにより、リモート Windows システム上で管理タスクを実行できるようになります。 Windows システムへの接続を確立するために使用される Python クライアントである python3-winrm をインストールします。

 @:~$ sudo apt-get install python3-winrm
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gyp libc-ares2 libhttp-parser2.8 libjs-async libjs-inherits libjs-is-typedarray libjs-node-uuid libuv1 libuv1-dev node-abbrev node-ajv
node-ansi node-ansi-color-table node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archy node-are-we-there-yet node-async

node-validate-npm-package-license node-wcwidth.js node-which node-which-module node-wide-align node-wrap-ansi node-wrappy node-y18n
node-yallist node-yargs node-yargs-parser nodejs nodejs-doc
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
python3-kerberos python3-ntlm-auth python3-requests-kerberos python3-requests-ntlm python3-xmltodict
The following NEW packages will be installed:
python3-kerberos python3-ntlm-auth python3-requests-kerberos python3-requests-ntlm python3-winrm python3-xmltodict
0 upgraded, 6 newly installed, 0 to remove and 231 not upgraded.
Need to get 84.8 kB of archives.
After this operation, 442 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-kerberos amd64 1.1.14-1build1 [16.8 kB]
Get:2 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-ntlm-auth all 1.1.0-1 [19.6 kB]
Get:3 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-requests-kerberos all 0.11.0-2 [10.1 kB]
Get:4 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-requests-ntlm all 1.1.0-1 [6,004 B]
Get:5 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-xmltodict all 0.11.0-2 [10.6 kB]
Get:6 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-winrm all 0.3.0-2 [21.7 kB]
Fetched 84.8 kB in 1s (70.3 kB/s)
Selecting previously unselected package python3-kerberos.
(Reading database ... 244430 files and directories currently installed.)
Preparing to unpack .../0-python3-kerberos_1.1.14-1build1_amd64.deb ...
Unpacking python3-kerberos (1.1.14-1build1) ...
Selecting previously unselected package python3-ntlm-auth.
Selecting previously unselected package python3-xmltodict.
Preparing to unpack .../4-python3-xmltodict_0.11.0-2_all.deb ...
Unpacking python3-xmltodict (0.11.0-2) ...
Selecting previously unselected package python3-winrm.
Preparing to unpack .../5-python3-winrm_0.3.0-2_all.deb ...
Unpacking python3-winrm (0.3.0-2) ...
Setting up python3-kerberos (1.1.14-1build1) ...
Setting up python3-winrm (0.3.0-2) ...
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

ステップ 3: Ansible インベントリ ファイルを更新する

ここで、Windows システムの IP アドレスを使用して ansible hosts ファイルを編集します。これで、ansible はどの Windows システムに接続する必要があるかを認識できるようになります。

 @:~$ sudo gedit /etc/ansible/hosts

[win]
192.168.0.102

ステップ 4: Ansible グループ変数を更新する

Windows システムに接続するために必要な変数を配置するためのディレクトリを作成します。

 @:~$ mkdir /etc/ansible/group_vars
@:~$ sudo chmod -R 777 /etc/ansible/

ファイル win.yaml を作成し、最初のステップで作成したユーザーの詳細と、Windows システムへの接続に必要な変数をいくつか追加します。

 @:~$ gedit /etc/ansible/group_vars/win.yaml
 ---

ansible_user: ansible

ansible_password: ansible

ansible_connection: winrm

ansible_winrm_server_cert_validation: ignore

ansible_winrm_transport: basic

ansible_winrm_port: 5985

ansible_python_interpreter: C:\Users\\AppData\Local\Programs\Python\Python37\python

ステップ 5: 管理する Windows サーバーを構成する

Windows Power Shell を開いてアップグレードします。 Windows マシン上に Powershell 3.0 および .NET Framework 4.0 が存在する必要があります。

 PS C:\WINDOWS\system32> $url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Upgrade-PowerShell.ps1"
PS C:\WINDOWS\system32> $file = "$env:temp\Upgrade-PowerShell.ps1"
PS C:\WINDOWS\system32> $username = "ansible"
PS C:\WINDOWS\system32> $password = "ansible"
PS C:\WINDOWS\system32> (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
PS C:\WINDOWS\system32> Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
PS C:\WINDOWS\system32> &$file -Version 5.1 -Username $username -Password $password -Verbose

ansible を使用して Windows システム上で WinRM を構成するために、リモート構成スクリプトが ansible によって提供されています。 PowerShell でスクリプトを実行します。

 PS C:\WINDOWS\system32> $url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
PS C:\WINDOWS\system32> $file = "$env:temp\ConfigureRemotingForAnsible.ps1"
PS C:\WINDOWS\system32> (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
PS C:\WINDOWS\system32> powershell.exe -ExecutionPolicy ByPass -File $file
PS C:\WINDOWS\system32> winrm enumerate winrm/config/Listener

Listener
Address = *
Transport = HTTP
Port = 5985
Hostname
Enabled = true
URLPrefix = wsman

CertificateThumbprint
ListeningOn = 127.0.0.1, 169.254.8.240, 169.254.36.9, 169.254.102.217, 169.254.215.170, 192.168.0.102, ::1, fe80::3131:c6d7:9ef5:8f0%7, fe80::51b7:9134:550d:d7aa%22, fe80::88f1:1229:e1dd:2409%16, fe80::99cf:5796:4f8e:f5c1%15, fe80::fd77:c19d:e0f2:66d9%9

Listener
Address = *
Transport = HTTPS
Port = 5986
Hostname = DESKTOP-2L8QMI6
Enabled = true
URLPrefix = wsman

CertificateThumbprint = C83B3FC8B274D0B650F0FD647DC7AC129BBE3FA0
ListeningOn = 127.0.0.1, 169.254.8.240, 169.254.36.9, 169.254.102.217, 169.254.215.170, 192.168.0.102, ::1, fe80::3131:c6d7:9ef5:8f0%7, fe80::51b7:9134:550d:d7aa%22, fe80::88f1:1229:e1dd:2409%16, fe80::99cf:5796:4f8e:f5c1%15, fe80::fd77:c19d:e0f2:66d9%9

HTTP トラフィックを許可するように winrm を設定します。

 PS C:\WINDOWS\system32> winrm set winrm/config/service '@{AllowUnencrypted="true"}'
Service
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)

MaxConcurrentOperations = 4294967295

MaxConcurrentOperationsPerUser = 1500

EnumerationTimeoutms = 240000
MaxConnections = 300

MaxPacketRetrievalTimeSeconds = 120
AllowUnencrypted = true
Auth
Basic = true
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false

CbtHardeningLevel = Relaxed
DefaultPorts
HTTP = 5985
HTTPS = 5986
IPv4Filter = *
IPv6Filter = *

EnableCompatibilityHttpListener = false

EnableCompatibilityHttpsListener = false

CertificateThumbprint
AllowRemoteAccess = true

認証をwirmでbasicに設定します。

 PS C:\WINDOWS\system32> winrm set winrm/config/service/auth '@{Basic="true"}'
Auth
Basic = true
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false
CbtHardeningLevel = Relaxed

ステップ 6: Windows サーバーへの接続をテストする

これでマシン上のすべての手順が完了しました。 ansible コントローラー マシンに移動し、 win_ping ansible モジュールを使用して Windows サーバー マシンに ping を実行します。

 @:~$ ansible win -m win_ping
192.168.0.102 | SUCCESS => {

"changed": false,
"ping": "pong"
}

成功メッセージは、接続が確立されたことを示します。これで、Ubuntu 上で実行されている Ansible から Windows システムをリモートで管理する準備が整いました。

「 Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?」についてわかりやすく解説!絶対に観るべきベスト2動画

【Linuxエンジニアになる!】UbuntuのGUIをWSLを使って、Windows上で走らせる WSLはWindowsのオプション機能でLinuxがWindows上で直接走るようになります
【環境構築】Windows環境にWSL2でUbuntuを動かす!Windows TerminalとVS Codeの設定もするよ

Ubuntu 上で実行されている Ansible から Windows サーバーに接続する方法を簡単に説明します。

以下の手順に従うには、両方のシステムに Python 3.x と Ansible がインストールされている必要があります。サポートが必要な場合は、以下の記事を参照してください。

Ubuntu に Ansible をインストールして構成するにはどうすればよいですか?

Windows に Ansible をインストールするにはどうすればよいですか?

以下は、私が使用している両方のサーバーの詳細です。

  • Ansible コントローラー – 192.168.0.108
  • Windowsサーバー – 192.168.0.102
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

ステップ 1: Ansible Windows ユーザーを作成する

Ansible Windows 接続セットアップ用の新しいユーザーを作成します。

  • Windows システムで [コンピュータの管理] を開き、[ローカル ユーザーとグループ] に移動します。
  • 「ユーザー」を右クリックし、新しいユーザーを作成します。
  • 「パスワードを無期限にする」チェックボックスを選択し、「作成」をクリックします。

アンシブルユーザー
アンシブルユーザー
アンシブルユーザー

  • 使用可能なグループの中から、Administrators グループを右クリックし、プロパティをクリックします。
  • 「追加」をクリックし、オブジェクト名に「ansible」と入力します。
  • [名前の確認] オプションをクリックし、[OK] をクリックします。

ansible グループ
ansible グループ
ansible グループ

これで、Windows マシン上の Ansible ユーザーの準備が整いました。

Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

ステップ 2: ライブラリと WinRM をセットアップする

Ansible コントローラー マシンに移動して更新し、以下で説明するライブラリをインストールします。

 @:~$ sudo apt-get update
@:~$ sudo apt-get install gcc python-dev
@:~$ sudo apt install python3-pip

WinRM は Windows リモート管理の略です。これにより、リモート Windows システム上で管理タスクを実行できるようになります。 Windows システムへの接続を確立するために使用される Python クライアントである python3-winrm をインストールします。

 @:~$ sudo apt-get install python3-winrm
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gyp libc-ares2 libhttp-parser2.8 libjs-async libjs-inherits libjs-is-typedarray libjs-node-uuid libuv1 libuv1-dev node-abbrev node-ajv
node-ansi node-ansi-color-table node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archy node-are-we-there-yet node-async

node-validate-npm-package-license node-wcwidth.js node-which node-which-module node-wide-align node-wrap-ansi node-wrappy node-y18n
node-yallist node-yargs node-yargs-parser nodejs nodejs-doc
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
python3-kerberos python3-ntlm-auth python3-requests-kerberos python3-requests-ntlm python3-xmltodict
The following NEW packages will be installed:
python3-kerberos python3-ntlm-auth python3-requests-kerberos python3-requests-ntlm python3-winrm python3-xmltodict
0 upgraded, 6 newly installed, 0 to remove and 231 not upgraded.
Need to get 84.8 kB of archives.
After this operation, 442 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-kerberos amd64 1.1.14-1build1 [16.8 kB]
Get:2 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-ntlm-auth all 1.1.0-1 [19.6 kB]
Get:3 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-requests-kerberos all 0.11.0-2 [10.1 kB]
Get:4 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-requests-ntlm all 1.1.0-1 [6,004 B]
Get:5 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-xmltodict all 0.11.0-2 [10.6 kB]
Get:6 http://old-releases.ubuntu.com/ubuntu cosmic/universe amd64 python3-winrm all 0.3.0-2 [21.7 kB]
Fetched 84.8 kB in 1s (70.3 kB/s)
Selecting previously unselected package python3-kerberos.
(Reading database ... 244430 files and directories currently installed.)
Preparing to unpack .../0-python3-kerberos_1.1.14-1build1_amd64.deb ...
Unpacking python3-kerberos (1.1.14-1build1) ...
Selecting previously unselected package python3-ntlm-auth.
Selecting previously unselected package python3-xmltodict.
Preparing to unpack .../4-python3-xmltodict_0.11.0-2_all.deb ...
Unpacking python3-xmltodict (0.11.0-2) ...
Selecting previously unselected package python3-winrm.
Preparing to unpack .../5-python3-winrm_0.3.0-2_all.deb ...
Unpacking python3-winrm (0.3.0-2) ...
Setting up python3-kerberos (1.1.14-1build1) ...
Setting up python3-winrm (0.3.0-2) ...
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?
Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?

ステップ 3: Ansible インベントリ ファイルを更新する

ここで、Windows システムの IP アドレスを使用して ansible hosts ファイルを編集します。これで、ansible はどの Windows システムに接続する必要があるかを認識できるようになります。

 @:~$ sudo gedit /etc/ansible/hosts

[win]
192.168.0.102

ステップ 4: Ansible グループ変数を更新する

Windows システムに接続するために必要な変数を配置するためのディレクトリを作成します。

 @:~$ mkdir /etc/ansible/group_vars
@:~$ sudo chmod -R 777 /etc/ansible/

ファイル win.yaml を作成し、最初のステップで作成したユーザーの詳細と、Windows システムへの接続に必要な変数をいくつか追加します。

 @:~$ gedit /etc/ansible/group_vars/win.yaml
 ---

ansible_user: ansible

ansible_password: ansible

ansible_connection: winrm

ansible_winrm_server_cert_validation: ignore

ansible_winrm_transport: basic

ansible_winrm_port: 5985

ansible_python_interpreter: C:\Users\\AppData\Local\Programs\Python\Python37\python

ステップ 5: 管理する Windows サーバーを構成する

Windows Power Shell を開いてアップグレードします。 Windows マシン上に Powershell 3.0 および .NET Framework 4.0 が存在する必要があります。

 PS C:\WINDOWS\system32> $url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Upgrade-PowerShell.ps1"
PS C:\WINDOWS\system32> $file = "$env:temp\Upgrade-PowerShell.ps1"
PS C:\WINDOWS\system32> $username = "ansible"
PS C:\WINDOWS\system32> $password = "ansible"
PS C:\WINDOWS\system32> (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
PS C:\WINDOWS\system32> Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
PS C:\WINDOWS\system32> &$file -Version 5.1 -Username $username -Password $password -Verbose

ansible を使用して Windows システム上で WinRM を構成するために、リモート構成スクリプトが ansible によって提供されています。 PowerShell でスクリプトを実行します。

 PS C:\WINDOWS\system32> $url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
PS C:\WINDOWS\system32> $file = "$env:temp\ConfigureRemotingForAnsible.ps1"
PS C:\WINDOWS\system32> (New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
PS C:\WINDOWS\system32> powershell.exe -ExecutionPolicy ByPass -File $file
PS C:\WINDOWS\system32> winrm enumerate winrm/config/Listener

Listener
Address = *
Transport = HTTP
Port = 5985
Hostname
Enabled = true
URLPrefix = wsman

CertificateThumbprint
ListeningOn = 127.0.0.1, 169.254.8.240, 169.254.36.9, 169.254.102.217, 169.254.215.170, 192.168.0.102, ::1, fe80::3131:c6d7:9ef5:8f0%7, fe80::51b7:9134:550d:d7aa%22, fe80::88f1:1229:e1dd:2409%16, fe80::99cf:5796:4f8e:f5c1%15, fe80::fd77:c19d:e0f2:66d9%9

Listener
Address = *
Transport = HTTPS
Port = 5986
Hostname = DESKTOP-2L8QMI6
Enabled = true
URLPrefix = wsman

CertificateThumbprint = C83B3FC8B274D0B650F0FD647DC7AC129BBE3FA0
ListeningOn = 127.0.0.1, 169.254.8.240, 169.254.36.9, 169.254.102.217, 169.254.215.170, 192.168.0.102, ::1, fe80::3131:c6d7:9ef5:8f0%7, fe80::51b7:9134:550d:d7aa%22, fe80::88f1:1229:e1dd:2409%16, fe80::99cf:5796:4f8e:f5c1%15, fe80::fd77:c19d:e0f2:66d9%9

HTTP トラフィックを許可するように winrm を設定します。

 PS C:\WINDOWS\system32> winrm set winrm/config/service '@{AllowUnencrypted="true"}'
Service
RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)

MaxConcurrentOperations = 4294967295

MaxConcurrentOperationsPerUser = 1500

EnumerationTimeoutms = 240000
MaxConnections = 300

MaxPacketRetrievalTimeSeconds = 120
AllowUnencrypted = true
Auth
Basic = true
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false

CbtHardeningLevel = Relaxed
DefaultPorts
HTTP = 5985
HTTPS = 5986
IPv4Filter = *
IPv6Filter = *

EnableCompatibilityHttpListener = false

EnableCompatibilityHttpsListener = false

CertificateThumbprint
AllowRemoteAccess = true

認証をwirmでbasicに設定します。

 PS C:\WINDOWS\system32> winrm set winrm/config/service/auth '@{Basic="true"}'
Auth
Basic = true
Kerberos = true
Negotiate = true
Certificate = false
CredSSP = false
CbtHardeningLevel = Relaxed

ステップ 6: Windows サーバーへの接続をテストする

これでマシン上のすべての手順が完了しました。 ansible コントローラー マシンに移動し、 win_ping ansible モジュールを使用して Windows サーバー マシンに ping を実行します。

 @:~$ ansible win -m win_ping
192.168.0.102 | SUCCESS => {

"changed": false,
"ping": "pong"
}

成功メッセージは、接続が確立されたことを示します。これで、Ubuntu 上で実行されている Ansible から Windows システムをリモートで管理する準備が整いました。

「 Ubuntu から Windows 上の Ansible に接続するにはどうすればよいですか?」についてわかりやすく解説!絶対に観るべきベスト2動画

【Linuxエンジニアになる!】UbuntuのGUIをWSLを使って、Windows上で走らせる WSLはWindowsのオプション機能でLinuxがWindows上で直接走るようになります
【環境構築】Windows環境にWSL2でUbuntuを動かす!Windows TerminalとVS Codeの設定もするよ