2. Ansible2.0 新機能
l 内部構造を書き直した
l その他いっぱいありすぎ
l Windows対応
l 140以上の新規モジュールの追加
l openstack, docker, zabbix, vmwareなど
l inventory, connection pluginも追加
l serf, consul, dockerなどからインベントリ情報を取ってこれる
l などなど
l 詳しくはこちら
l https://raw.githubusercontent.com/ansible/ansible/stable-2.0/CHANGELOG.md
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 2
3. 使ってみた機能
l Block
l Expect
l taskレベルで変数の定義、上書き
l Windowsモジュール
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 3
7. expectの例
l 前提条件:python >= 2.6, pexpect >= 3.3
-------
- hosts: localhost
connection: local
tasks:
- name: expect
expect:
command: ssh root@192.168.1.1 echo HOST NAME IS `hostname` ; uname –a
responses:
(?i)password: "Password”
register: get_hostname
- debug: var=get_hostname.stdout_lines
Expectブロックを抜けると、
sshもクローズする
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 7
8. expectの例実⾏結果
# ansible-playbook test.yml -l localhost
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [expect] ******************************************************************
changed: [localhost]
TASK [debug var=get_hostname.stdout_lines] *************************************
ok: [localhost] => {
"changed": false,
"get_hostname.stdout_lines": [
"root@192.168.1.1's password: ",
"HOST NAME IS example-server",
"Linux example-server 2.6.32-573.3.1.el6.x86_64 #1 SMP Thu Aug 13 22:55:16 UTC
2015 x86_64 x86_64 x86_64 GNU/Linux”
]
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 8
9. Registerで取得できる値の例
l モジュールによって異なる
l “changed”: false | true:実⾏結果
l “cmd”: ”…”:実⾏モジュールと内容
l “rc”: 0 | 1 | …:リターンコード
l “stdout”: ”…”:標準出⼒(改⾏⽂字がエスケープされる)
l “stdout_lines”: […]:標準出⼒(splitされ複数⾏になる)
l 条件分岐
l when: result | succeeded, failedではモジュール実⾏結果なので、
shell/commandなどのリターンコードで分岐させたい場合は
l when: result.rc !=0 と使える
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 9
10. 動きが変わったモジュールの例
l Serviceモジュール
- name: Check pacemaker
service: name=pacemaker enabled=yes
register: result_heartbeat
ignore_errors: True
l と書くと、パッケージがインストールされていない場合は、エラーとなったが、2.0からは、インストールされてい
ない場合は、エラーにならない。
l パッケージがインストールされている場合は、今までどおりの起動、停⽌、再起動、⾃動起動させるかを⾏える。
l Packageモジュール(NEW)
l serviceモジュールでインストールパッケージの確認が⾏えなくなったため、2.0以降はpackageモジュールにて⾏う
必要がある
- name: Check pacemaker
package: name=pacemaker
register: result_heartbeat
ignore_errors: True
l Copyモジュールのオプション
l remote_src=Trueを指定することにより、Ansible実⾏ホスト→リモートホストへファイルのコピーではなく、リモー
トホストのファイルをリモートホストの別の場所にコピーを⾏える。
- name: Copy tomcat dir
copy: src=/tmp/apache-tomcat-7.0.65/LICENSE dest=/opt/tomcat remote_src=True directory_mode=yes
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 10
11. Windowsサポート
l win_acl (E):ファイル/ディレクトリのアクセス権限設定
l win_chocolatey (E):yum/apt-getのようなcliパッケージマ
ネージャ(https://chocolatey.org)を利⽤するパッケージ操
作
l win_copy:ファイル/ディレクトリの転送
l win_dotnet_ngen (E):.NETネイティブ イメージ サービス
を任意に実⾏
l win_environment (E):.NET環境変数の作成変更
l win_feature :Windows Roles/Featuresのインストール/
削除
l win_file :ファイル/ディレクトリの作成/削除
l win_firewall_rule (E):ファイアウオールルールの操作
l win_get_url:URLからのファイルダウンロード
l win_group:ローカルグループの操作
l win_iis_virtualdirectory (E):IIS仮想ディレクトリの操作
l win_iis_webapplication (E:IIS Webアプリケーションの操
作
l win_iis_webapppool (E):IIS Webアプリケーションプール
の操作
l win_iis_webbinding (E) :IIS Webバインドの操作
l win_iis_website (E) :IIS Webサイトの操作
l win_lineinfile:ファイル内の⾏操作
l win_msi:Windows MSIのインストール/削除
l win_nssm (E):NSSM(http://nssm.cc/)を利⽤して任意
のアプリをサービスとして起動する
l win_package (E):ファイル/urlからのインストーラーを
使ったインストール/削除
l win_ping:Windowsへのping
l win_regedit (E):レジストリの操作
l win_scheduled_task (E):タスクの操作
l win_service:Windowsサービスの操作
l win_stat:Windows上のファイルstat
l win_template:Python⽤のテンプレートエンジンjinja2のテ
ンプレートをWindowsに転送
l win_unzip (E):Windows上のzipファイルを解凍
l win_updates (E):Windows更新プログラムの操作
l win_user:ローカルユーザの操作
l win_webpicmd (E):WebPICMD(Web Platform Installer
command-line)を利⽤したパッケージのインストール
http://docs.ansible.com/ansible/list_of_windows_modules.html
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 11
12. Windows前提条件
l Ansible2.0では、WinRM経由でWindowsに接続する
l PowerShell 3.0<が必要
l Windows 7 SP1, Windows Server 2008 SP1以上
l Windows Server 2008での例
l 以下のファイルをダウンロードし実⾏する
l Microsoft .NET Framework 4.5.2
l http://www.microsoft.com/en-us/download/details.aspx?id=42643
l ファイル名:NDP452-KB2901954-Web.exe
l Windows Management Framework 4.0
l http://www.microsoft.com/ja-jp/download/details.aspx?id=40855
l ファイル名:Windows Server 2008 R2 SP1 x64: Windows6.1-KB2819745-x64-
MultiPkg.msu
l Windows Server 2012, Windows 8以降ではデフォルトで
PowerShell 4.0<
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 12
13. 事前準備
l Powershellの確認
PS C:¥Users¥Administrator> $PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
l ネットワークプロファイルの変更
l ネットワークプロファイルがパブリックならばプライベートにする。ドメインであったり元々プライベートであったりするなら
ば変更の必要はない。
l Windows側の準備を⾏ってくれるスクリプトが公式に⽤意されているので、⽤意されたスクリプトをダウンロード
し、以下のコマンドを実⾏する。
PS > mkdir C:¥work
PS > cd C:¥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 –ExecutionPolicy RemoteSigned .¥ConfigureRemotingForAnsible.ps1
PS > Set-ExecutionPolicy RemoteSigned –Force
PS > winrm set winrm/config/service/auth ‘@{Basic=“true”}’
PS > winrm set winrm/config/service ‘@{AllowUnencrypted=“true”}’
PS > Set-ExecutionPolicy –Scope CurrentUser RemoteSigned
l Ansible側(Linux)での設定
$ pip install pywinrm
2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 13
14. 実⾏例
l hostsファイルの設定
[windows]
win2008 #windwosのホスト名、IP
[windows:vars]
ansible_ssh_user=Administrator #ユーザ
ansible_ssh_pass=password #パスワード
ansible_ssh_port=5986
ansible_connection=winrm
l 動作確認
$ ansible windows -i hosts -m win_ping –vvvv
<win2008> ESTABLISH WINRM CONNECTION FOR USER: Administrator on PORT 5986 TO win2008
<win2008> REMOTE_MODULE win_ping
<win2008> EXEC (New-Item -Type Directory -Path $env:temp -Name "ansible-tmp-1427882816.58-
246867798460713").FullName | Write-Host -Separator '';
<win2008> PUT /tmp/tmpAiMUZj TO C:¥Users¥Administrator¥AppData¥Local¥Temp¥ansible-tmp-
1427882816.58-246867798460713¥¥win_ping<win2008> PUT /tmp/tmpBEeRpO TO
C:¥Users¥Administrator¥AppData¥Local¥Temp¥ansible-tmp-1427882816.58-
246867798460713¥¥arguments<win2008> EXEC &
C:¥Users¥Administrator¥AppData¥Local¥Temp¥ansible-tmp-1427882816.58-
246867798460713¥¥win_ping.ps1 C:¥Users¥Administrator¥AppData¥Local¥Temp¥ansible-tmp-
1427882816.58-246867798460713¥¥arguments; Remove-Item
"C:¥Users¥Administrator¥AppData¥Local¥Temp¥ansible-tmp-1427882816.58-246867798460713¥" -
Force -Recurse;
win2008 | success >> {
"changed": false,
"ping": "pong"
}2016/2/1 Copyright 2016(C) OSS Laboratories Inc. All Rights Reserved 14