AnsibleをWindows上のWSLにインストール¶
はじめに¶
Windows 10 Anniversary Update(64bit版)から、開発者機能としてWindows Services for Linuxが利用可能になりました。Ubuntu環境が利用できるので、Ansibleをそこで動かすことができるとよいかと思います。
動作環境¶
WSLの環境を確認¶
Windows 10のバージョンは、[スタートメニュー] > [設定] > [システム] > [バージョン情報] で確認できます。
エディション Windows 10 Pro バージョン 1607 OSビルド 14393.222
Ubuntuバージョンは次で確認できます。2016年10月2日時点では次のようにUbuntu 14.04でした。
~$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
32bitか64bitかはarchコマンドで確認できます。
~$ arch x86_64
64bitの場合、x86_64となります。
pythonのバージョン確認
$ python --version Python 2.7.6
Ubuntu標準パッケージのAnsibleの確認¶
Ubuntu 14.04は2014年4月のリリースで、標準で用意されているAnsibleパッケージは1.5とAnsibleとしては相当古いものになります。
~$ apt list|grep -i ansible ansible/trusty 1.5.4+dfsg-1 all ansible-doc/trusty 1.5.4+dfsg-1 all ansible-fireball/trusty 1.5.4+dfsg-1 all ansible-node-fireball/trusty 1.5.4+dfsg-1 all
PPAパッケージのAnsibleの確認¶
Ubuntuでは、ディストリビューションの標準パッケージ以外に、Personal Package Archive(PPA)という追加パッケージや標準よりも新しいバージョンを利用できるリポジトリがあります。
~$ sudo add-apt-repository ppa:ansible/ansible Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications— automate in a language that approaches plain English, using SSH, with no agents to install on remote systems. http://ansible.com/ 詳しい情報: https://launchpad.net/~ansible/+archive/ubuntu/ansible [ENTER] を押すと続行します。ctrl-c で追加をキャンセルできます gpg: 鍵リング「/tmp/tmpu8ki_xb2/secring.gpg」ができました gpg: 鍵リング「/tmp/tmpu8ki_xb2/pubring.gpg」ができました gpg: 鍵7BB9C367をhkpからサーバkeyserver.ubuntu.comに要求 gpg: /tmp/tmpu8ki_xb2/trustdb.gpg: 信用データベースができました gpg: 鍵7BB9C367: 公開鍵"Launchpad PPA for Ansible, Inc."をインポートしました gpg: 処理数の合計: 1 gpg: インポート: 1 (RSA: 1) OK
リポジトリ情報を更新します。
~$ sudo apt update 無視 http://archive.ubuntu.com trusty InRelease 取得:1 http://ppa.launchpad.net trusty InRelease [16.0 kB] 取得:2 http://security.ubuntu.com trusty-security InRelease [65.9 kB] 取得:3 http://security.ubuntu.com trusty-security/main amd64 Packages [531 kB] :
パッケージリストを調べます。
~$ apt list | grep -i ansible WARNING: apt does not have a stable CLI interface yet. Use with caution in scripts. ansible/trusty 2.1.2.0-1ppa~trusty all ansible-doc/trusty 1.5.4+dfsg-1 all ansible-fireball/trusty 1.5.4+dfsg-1 all ansible-node-fireball/trusty 1.5.4+dfsg-1 all
インストール¶
~$ sudo apt install ansible パッケージリストを読み込んでいます... 完了0% 依存関係ツリーを作成しています 状態情報を読み取っています... 完了% 以下のパッケージが自動でインストールされましたが、もう必要とされていません: libfreetype6 os-prober これを削除するには 'apt-get autoremove' を利用してください。 以下の特別パッケージがインストールされます: libgmp10 python-crypto python-httplib2 python-jinja2 python-markupsafe python-paramiko python-setuptools sshpass 提案パッケージ: python-crypto-dbg python-crypto-doc python-jinja2-doc 以下のパッケージが新たにインストールされます: ansible libgmp10 python-crypto python-httplib2 python-jinja2 python-markupsafe python-paramiko python-setuptools sshpass アップグレード: 0 個、新規インストール: 9 個、削除: 0 個、保留: 68 個。 2,257 kB のアーカイブを取得する必要があります。 この操作後に追加で 13.7 MB のディスク容量が消費されます。 続行しますか? [Y/n]
ここでyキーを押すとインストールが開始されます。
インストール後確認をします。
~$ ansible --version ansible 2.1.2.0 config file = /etc/ansible/ansible.cfg configured module search path = Default w/o overrides
Windowsを管理ノードとするため、pywinrmをインストールします。
pywinrmは、ubuntuのパッケージには用意されていないので、pipでインストールします。
まず、pipをインストールします。
work$ ~$ sudo apt install python-pip パッケージリストを読み込んでいます... 完了0% 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下のパッケージが自動でインストールされましたが、もう必要とされていません: libfreetype6 os-prober これを削除するには 'apt-get autoremove' を利用してください。 以下の特別パッケージがインストールされます: binutils build-essential cpp cpp-4.8 dpkg-dev fakeroot g++ g++-4.8 gcc gcc-4.8 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan0 libatomic1 libc-dev-bin libc6-dev libcloog-isl4 libdpkg-perl libfakeroot libfile-fcntllock-perl libgcc-4.8-dev libgomp1 libisl10 libitm1 libmpc3 libmpfr4 libquadmath0 libstdc++-4.8-dev libtsan0 linux-libc-dev make manpages-dev python-chardet-whl python-colorama python-colorama-whl python-distlib python-distlib-whl python-html5lib python-html5lib-whl python-pip-whl python-requests-whl python-setuptools-whl python-six-whl python-urllib3-whl python-wheel python3-pkg-resources 提案パッケージ: binutils-doc cpp-doc gcc-4.8-locales debian-keyring g++-multilib g++-4.8-multilib gcc-4.8-doc libstdc++6-4.8-dbg gcc-multilib autoconf automake1.9 libtool flex bison gdb gcc-doc gcc-4.8-multilib libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan0-dbg libtsan0-dbg libquadmath0-dbg glibc-doc libstdc++-4.8-doc make-doc python-genshi python-lxml python3-setuptools 推奨パッケージ: python-dev-all 以下のパッケージが新たにインストールされます: binutils build-essential cpp cpp-4.8 dpkg-dev fakeroot g++ g++-4.8 gcc gcc-4.8 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan0 libatomic1 libc-dev-bin libc6-dev libcloog-isl4 libdpkg-perl libfakeroot libfile-fcntllock-perl libgcc-4.8-dev libgomp1 libisl10 libitm1 libmpc3 libmpfr4 libquadmath0 libstdc++-4.8-dev libtsan0 linux-libc-dev make manpages-dev python-chardet-whl python-colorama python-colorama-whl python-distlib python-distlib-whl python-html5lib python-html5lib-whl python-pip python-pip-whl python-requests-whl python-setuptools-whl python-six-whl python-urllib3-whl python-wheel python3-pkg-resources アップグレード: 0 個、新規インストール: 48 個、削除: 0 個、保留: 0 個。 40.9 MB のアーカイブを取得する必要があります。 この操作後に追加で 118 MB のディスク容量が消費されます。 続行しますか? [Y/n]
ここでyキーを押すとインストールが開始されます。
pipでwinrmをインストール
work$ sudo pip install pywinrm Downloading/unpacking pywinrm Downloading pywinrm-0.2.1-py2.py3-none-any.whl Downloading/unpacking xmltodict (from pywinrm) Downloading xmltodict-0.10.2.tar.gz Running setup.py (path:/tmp/pip_build_root/xmltodict/setup.py) egg_info for package xmltodict Downloading/unpacking requests>=2.9.1 (from pywinrm) Downloading requests-2.11.1-py2.py3-none-any.whl (514kB): 514kB downloaded Downloading/unpacking requests-ntlm>=0.3.0 (from pywinrm) Downloading requests_ntlm-0.3.0-py2.py3-none-any.whl Requirement already satisfied (use --upgrade to upgrade): six in /usr/lib/python2.7/dist-packages (from pywinrm) Downloading/unpacking python-ntlm3 (from requests-ntlm>=0.3.0->pywinrm) Downloading python_ntlm3-1.0.2-py2.py3-none-any.whl Installing collected packages: pywinrm, xmltodict, requests, requests-ntlm, python-ntlm3 Running setup.py install for xmltodict Found existing installation: requests 2.2.1 Not uninstalling requests at /usr/lib/python2.7/dist-packages, owned by OS Successfully installed pywinrm xmltodict requests requests-ntlm python-ntlm3 Cleaning up...
動作確認¶
自ホスト(Windows)への接続¶
自ホストへの接続による動作確認を行います。
まず、Windows 10のPowerShellを管理者権限で開きます(スタートメニュー > Windows PowerShell > Windows PowerShell を右クリックし、管理者として実行)。
PowerShellスクリプトの実行可否設定を確認し、デフォルト(Restricted)であればRemoteSignedに変更します。
PS C:\work> Get-ExecutionPolicy Restricted PS C:\work> Set-ExecutionPolicy RemoteSigned 実行ポリシーの変更 実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies のヘルプ トピック (http://go.microsoft.com/fwlink/?LinkID=135170) で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか? [Y] はい(Y) [A] すべて続行(A) [N] いいえ(N) [L] すべて無視(L) [S] 中断(S) [?] ヘルプ (既定値は "N"): y PS C:\work> Get-ExecutionPolicy RemoteSigned
AnsibleからWinRMで接続を受け付けるための初期設定スクリプトを入手・実行します。
PowerShellを管理者で実行します。
PS C:\work> wget https://github.com/ansible/ansible/raw/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1 PS C:\work> .\ConfigureRemotingForAnsible.ps1 Self-signed SSL certificate generated; thumbprint: D0927BBC8E35F0CEA0235285CCBE51F5158A5911 wxf : http://schemas.xmlsoap.org/ws/2004/09/transfer a : http://schemas.xmlsoap.org/ws/2004/08/addressing w : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd lang : ja-JP Address : http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous ReferenceParameters : ReferenceParameters OK
WinRMの接続には、HTTPとHTTPSがあります。デフォルトではHTTPが無効となっています。
次のコマンドで設定内容が確認できます。"AllowUnencrypted"がfalseになっていると、HTTPが無効です。
C:\WINDOWS\system32>winrm get winrm/config/service 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 = false 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
AllowUnencryptedを有効にします。コマンドプロンプトかPowerShellを管理者で実行し、次のコマンドを実行します。
C:\> 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
- 注)コマンドプロンプトで実行する際、
'
{AllowUnencrypted="true"}'@とシングルクォートを使うとエラーとなりました。
Bash(Windows Services for Linux)に戻って、自ホスト(ホスト名: whiskey、IPアドレス: 192.168.1.35)へWinRM接続(HTTP)するインベントリファイルを作成します。置き場所はどこでも可です。
[windows] whiskey ansible_host=192.168.1.35 [windows:vars] ansible_connection=winrm ansible_port=5985
Ansibleで接続確認を行います。
work$ ansible whiskey -i hosts -m win_ping -m win_ping whiskey | SUCCESS => { "changed": false, "ping": "pong" }
エラーと対処¶
- Windows側で初期設定スクリプト(ConfigureRemotingForAnsible.ps1)を実行し忘れたとき
whiskey | UNREACHABLE! => { "changed": false, "msg": "plaintext: HTTPConnectionPool(host='192.168.123.138', port=5985): Max retries exceeded with url: /wsman (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fbf6a1b5790>: Failed to establish a new connection: [Errno 111] Connection refused',))", "unreachable": true }
- Windows側でWinRMのAllowUnencryptedをfalseのままHTTP接続(ポート5985)をしたとき
whiskey | UNREACHABLE! => { "changed": false, "msg": "plaintext: the specified credentials were rejected by the server", "unreachable": true }
- Windows側でWinRM接続(SSL)のSSL証明書が自己証明書のとき
初期設定スクリプト(ConfigureRemotingForAnsible.ps1)は自己証明書を設置します。
インベントリにansible_winrm_server_cert_validation=ignore
を設定する等の回避手段をしていないとwhiskey | UNREACHABLE! => { "changed": false, "msg": "ssl: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed", "unreachable": true }
参考¶