IBM Cloud上のAnsibleから同クラウド上のWindowsと疎通するまでのメモ


IBMクラウドでVSIをオーダーする

IBM Cloudに以下環境を用意し、Ansibleの実行環境を用意してみました。
・Windows Server 2016 1台(今回はvirtualserver01)
・RHEL 7.7 1台(今回はvirtualserver02)

$cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.7 (Maipo)

今回はRHEL側にAnsible AWXを導入した後、Windows Server側の疎通確認が出来る所までを記載します。

1.RHEL側の環境確認(環境に応じて書き換えが必要)

・/etc/hostsに宛先Windowsの記載があることを確認する。

123.456.789.123 virtualserver02.hogehoge.cloud virtualserver02
123.456.789.124 virtualserver01

2. /etc/ansible/hostsに以下を追記する

[windows]
virtualserver01
[windows:vars]
ansible_user=Administrator
ansible_passwordhogehoge
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

[rhel7]
virtualserver02
[rhel7:vars]
#ansible_ssh_user=ansible_ssh
ansible_user=ansible_ssh
#ansible_ssh_private_key_file=/home/ansible_ssh/.ssh/id_rsa.pub
#ネットの事例だと、id_rsa.pubなど共通鍵ファイルを記載しているものもあるが失敗する
ansible_ssh_private_key_file=/home/ansible_ssh/.ssh/id_rsa
#秘密鍵ファイルをきちんと記載する

3. ユーザansible_ssh作成

$useradd ansible_ssh #ユーザー作成
$groups ansible_ssh #グループに追加
$su - ansible_ssh #ansible_sshにsu
$cd ~/
$ssh-keygen -t rsa #秘密鍵の作成(よしなに作る)

4. 鍵認証用設定

$cd ~/.ssh
$cat id_rsa.pub > authorized_keys
$chmod 600 authorized_keys

5. sudoers設定

$cp -p /etc/sudoers /etc/sudoers.bak
$visudo
## Allow root to run any commands anywhere
hoge1   ALL=(ALL)       ALL
hoge2    ALL=(ALL)       ALL
ansible_ssh     ALL=(ALL)       ALL #追記
## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL
%ansible_ssh    ALL=(ALL)       NOPASSWD: ALL #追記

6 sshd_config設定

$cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.org
$vi /etc/ssh/sshd_config
[末尾に下記を追記]
Match User ansible_ssh
        PubkeyAuthentication yes
        PasswordAuthentication no

$systemctl restart sshd

※[ansible_ssh] $ ssh ansible_ssh@virtualserver02 で鍵認証ログインできることと、sudo -iでパスワードを求められないことを確認

ここまでの作業で、RHELからWindowsまでの疎通はできるようになっている。
※実行元での秘密鍵ファイルへのアクセス権がないユーザーでは実行不可(ansible_sshでは実行できる)

windowsの設定

※事前確認
.NET Framework のバージョンが4.0 がインストールされていること

ansible/hosts へ Windowsサーバーを追記

[windows]
virtualserver01
[windows:vars]
ansible_user=Administrator
ansible_password=xxxxxxxxxxxxxxx
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

pipのインストール

easy_install pip

pywinrm をインストール

python2 -m pip install pywinrm

これでWindows側の設定はおわり

疎通確認

ansible windows -m win_ping -i /etc/ansible/hosts
/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  from cryptography.exceptions import InvalidSignature
virtualserver01 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

・テスト用のplaybookを実行してみる。
cat winserver.yml
- hosts: virtualserver01
gather_facts: false
tasks:
- name: Touch a file
win_file:
path: C:\hoge.txt
state: touch

ansible-playbook winserver.yml
で実行する

実行できたらWindows上にファイルができてるはず。。。
とりあえずここまで。

・WinRM の設定

・以下のコマンドでWinRMリスナーの確認をする
winrm enumerate winrm/config/litener

・pywinrm をインストール