LoginSignup
2
7

More than 5 years have passed since last update.

ansible でpowershellを使い windows を操作する(まだ途中です)

Last updated at Posted at 2019-04-11

windows 10 に移行するために、大量のアカウントをPCに設定するためには役立つ記事です。それ以外はあまり役立つ気はしません。

まず、こちらをよみましょう。AnsibleでWindowsを操作する準備をする

  • つっこみどころ満載ですので、間違いあったらぜひご指摘ください。

ansible から、windows を操作するため

操作マシン(linuxを想定)
  • version 確認: ansible --version
  • ansible には、pywinrm が必要
  • conda instal -c conda-forge pywinrm
  • 対象マシンには原則 administrator ではいります、はいれないときは、別の管理者アカウントではいってください。
  • 権限などの設定は、インベントリファイルのhosts に書いておきます。
  • hosts の例 windows という名前を付与してあります。
# hosts の例
# ansible 192.168.1.xx  -m win_ping  (設定したら ping でチェック)
[windows]
# ip か hostname 
192.168.1.xx
[windows:vars]
ansible_connection=psrp
ansible_port=5986
ansible_auth=basic
ansible_psrp_cert_validation=ignore
ansible_user=administrator
ansible_password=xxx ここにパスワードを書く xxx
対象Windows 設定
  • 動作環境: powershell 5.x 以上 (wiundows 7 は、2.xです。)
  • Powershellの実行ポリシーの変更 : Set-ExecutionPolicy RemoteSigned

- Ansible用WinRMモジュールのインストール 下記で説明

# とりあえず、作業用ディレクトリworkをつくる 
PS C:\> mkdir C:\work
PS C:\> cd .\work

PS C:\work> Invoke-WebRequest -Uri https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile ConfigureRemotingForAnsible.ps1

PS C:\work> powershell.exe .\ConfigureRemotingForAnsible.ps1

最初は,ping を試します。結果は、どりあえず、/tmp/temp.1 に出力します。

  • playbook のサンプル
  • ansible-playbook -i hosts ping.yml -e "host=192.168.1.xx"
# ファイル名は、ping.yml 
# host は、インベントリ(今回は、hosts) に指定があること 
- hosts: "{{ host }}"
  gather_facts: No
  tasks:
  - name: ping
    win_ping:
    register: result
  - local_action:
      copy content={{ result }} dest=/tmp/temp.1

powershell に引数を渡す

  • 作ったアカウントで、リモート接続できるようにするスクリプトです。
  • ansible から、powershell のスクリプトを呼び出します。
  • playbook から見て files/remote_user_add.ps1 とします。
# remote desktop user に加える
param
(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$remote_user
)
Add-LocalGroupMember -Group "Remote Desktop Users" -Member "$remote_user"
  • 上記 powershell の呼び出す ansible-playbook
- hosts: "{{ host }}"
  gather_facts: false
  tasks:
  - name: powershell
    script: files/remote_user_add.ps1 {{ remote_user }}
    register: result
  - local_action:
      copy content={{ result }} dest=/tmp/temp.1
  • playbook は引数をつけて実行
ansible-playbook -i hosts remote_user_add.yml  -e "host=192.168.1.xx remote_user=hogehoge"

  • これを実行するとリモートデスクトップでuser: hogehoge にアクセスできます。
  • リモートですぐにはいれるようになるので、ちょっと便利な程度です。たくさん設定するときは、便利でしょう。
2
7
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
2
7