9. Troubleshooting, Tips, and Tricks
Ansibleは、モジュールから生成した実行可能なPythonプログラムを、コントロールノードから
マネージドノードに転送して実行します。
Ansibleの仕組みと問題が発生するポイント(2)
Control Node Managed Node
Module
Python
Program
9
10. Managed node
Remote tmp directory
Remote tmp directory
Troubleshooting, Tips, and Tricks
Ansibleの仕組みと問題が発生するポイント(3)
Control node
Playbook
Inventory
AnsiballZ_setup.py
AnsiballZ_ping.py
AnsiballZ_setup.py
AnsiballZ_ping.py
Plugin
2)
1)
3)
4-b)
5-b)
4-d)
4-d,e,f)
5-d)
5-d,e,f)
A
C
C
4-a,c,g)
5-a,c,g)
B
B
B
B
10
11. Troubleshooting, Tips, and Tricks
前出のサンプルPlaybook実行時の処理の流れは以下の通りです。
1) インベントリファイル(./inventories/testing/hosts)をロードする
2) cache/connection/become/shellプラグインをロードする
3) Strategy(デフォルトのlinearストラテジー)にしたがってマネージドノードに対する実行計画を生成
4) Setupモジュールの実行
a) マネージドノード上に一時的なワーキングディレクトリ(remote tmp directory)を作成
b) ANSIBALLZ形式でhost_vars、モジュールオプションパラメータを含めて実行プログラムのアーカイブを作成する =>
AnsiballZ_setup.py
c) マネージドノードのPythonインタプリタのパスをディスカバリ
d) 4.2で生成したAnsiballZ_setup.pyファイルをマネージドノードにsftpで転送
e) SSH経由でマネージドノード上に転送したAnsiballZ_setup.pyを実行
f) Callbackプラグインでモジュールの実行結果を標準出力(JSON形式)で受け取ってストア
g) 4.1で作成したディレクトリ配下を削除
5) Pingモジュールの実行 ※詳細な流れは4-a...4-gと同様 (AnsiballZ_ping.py)
6) PLAY RECAPを出力してPlaybookの実行を終了
Ansibleの仕組みと問題が発生するポイント(4)
11
18. Troubleshooting, Tips, and Tricks
❏ Playbookの構文エラー
Playbook実行前処理で発生するエラーの多くは、PlaybookやInventoryファイルの書式に関す
るものです。以下のような誤った記述のPlaybookを実行しようとした場合に発生します。
Playbook実行前処理 - (A)
---
- hosts: all
gather_facts: false
tasks:
name: Ping test to managed nodes
- shell: hostname
register: result
name: Ping test to managed nodes
- debug:
var: result.stdout
疲れてくると、この手のエラーが
多くなりがちです。
明日の朝頑張ることにして、もう
ビール飲んで寝ちゃいましょう。
18
19. Troubleshooting, Tips, and Tricks
--syntax-checkオプションで、Playbook実行前にチェックして対応しておきましょう。
Playbook実行前処理 - (A)
$ ansible-playbook -i hosts sample00.yml --syntax-check
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)
Syntax Error while loading YAML.
expected <block end>, but found '-'
The error appears to be in '/Users/hsaito/work/testing/ansible/playbook/error/sample00/main.yml':
line 7, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
name: Ping test to managed nodes
- shell: hostname
^ here
--syntax-checkでPlaybookの
構文エラーを解決したら
ansible-lintによる構文規約の
チェックを行いましょう
19
45. Troubleshooting, Tips, and Tricks
❏モジュールが必要とするライブラリやコマンドがインストールされていることを確認する
Ansibleのモジュールは数多くありますが、利用に際しては必ずドキュメントを読みましょう。
特にRequirementsには、モジュールが動作するのに必要なライブラリやコマンドの情報が書か
れています。
ANSIBALLZファイルの実行処理 - (C)
例: gitモジュール
Requirements
The below requirements are needed on the host that executes this module.
●git>=1.7.1 (the command line tool) これらは、
「ANSIBALLZファイルを実行するノード」
にインストールする必要があります!
45