====== AnsibleのTips ====== ===== DryRun時にスキップ ===== スキップしたいTASKに以下の条件を付与する when: not ansible_check_mode ===== json形式のstdoutを利用する ===== json形式で値を返すコマンドを利用した場合に結果をregisterで記録すると、テキストとしてエスケープして記憶されてしまう - name: get any json respoce register: responce local_action: shell /path/to/command option TASK: [debug var=responce] ****************************************************** ok: [localhost] => { "responce": { "changed": true, "cmd": "/path/to/command option", "delta": "0:00:05.268682", "end": "20xx-mm-dd hh:mm:ss.xxxxxx", "rc": 0, "start": "20xx-mm-dd hh:mm:ss.xxxxxx", "stderr": "", "stdout": ""{\n \"key\": [\n {\n \"key2\": \"value1\", \n \"key3\": \"value2",.......... "stdout_lines": [ "{", " \"_meta\": {", " \"key\": {", " \"key2\": value1, ", " \"key3\": \"value2\", .......... .......... 以下のようにするとデータとして保持できる - name: get any json respoce register: responce local_action: shell /path/to/command option - name: set facts set_fact: responce_json: "{{ responce.stdout|from_json }}" TASK: [debug var=responce_json] ****************************************************** ok: [localhost] => { "responce_json": { "key": { "key2": value1, "key3": "value2", .......... .......... ===== Log設定 ===== 設定ファイル((ansible.cfg))に記述 [defaults] log_path=/path/to/logfile ===== プレイブックの実行対象となるホスト ===== プレイブックの実行対象となるホスト名の配列は play_hosts で得られる コマンドラインで確認するには $ ansible-playbook exsample-book.yml --list-host ===== ファイルの有無を確認 ===== # task - find: path=/path/to/folder register: checked_dir - fail: msg='this directory is not empty. file exists' when: checked_dir.matched > 0 - debug: msg: 'this directory is empty.' ===== ファイルの存在確認 ===== # task - stat: path=/path/to/anyfile register: checked_file - name: skip if file is found. fail: msg='file not found' when: checked_file.stat.md5 is not defined ## OR - name: skip if file is found. fail: msg='file not found' when: not checked_file.stat.exists with_itemsで複数のチェックを行うと若干変わるので注意 # task - stat: path=/path/to/anyfile register: is_exist with_items: - anyfile0 - anyfile1 - name: skip if anyfile0 is found. fail: msg='anyfile0 not found' when: is_exist.results[0].stat.md5 is not defined - name: skip if anyfile1 is found. fail: msg='anyfile1 not found' when: is_exist.results[1].stat.md5 is not defined ===== ファイルの比較 ===== - stat: path=/path/to/anyfile1 register: is_exist1 - stat: path=/path/to/anyfile2 register: is_exist2 - debug: msg="compare" when: (is_exist1.stat.md5 == is_exist2.stat.md5) ===== ディレクトリ作成 ===== - name: make directory file: path=/path/to/making/dir state=directory owner=root group=root mode=0755 ===== Ansible Vault ===== 秘密鍵のようなそのまま置いておきたくない情報を暗号化する。 ===== WinRMでHTTPSアクセスするとエラー ===== windowsにアクセスすると以下のようなエラーが出る。 FAILED => 500 WinRMTransport. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581) 原因はPython 2.7.9以降で入ったBUG自己署名証明書に対しての[[https://www.python.org/dev/peps/pep-0476/|仕様変更]]。Ansible以外でも各所で悲鳴が上がってるが仕様なので治ることはないと思う。 以下のようなコールバックルーチンを使う import ssl if hasattr(ssl, '_create_default_https_context') and hasattr(ssl, '_create_unverified_context'): ssl._create_default_https_context = ssl._create_unverified_context class CallbackModule(object): pass これで以下のようにすればOK ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ansible windows -i [inventory] -m win_ping OR env ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ansible windows -i [inventory] -m win_ping ANSIBLE_LOAD_CALLBACK_PLUGINS設定はansible.cfgの**bin_ansible_callbacks**でも設定できる。\\ プラグインロードのDefaultは以下の通り callback_plugins = ~/.ansible/plugins/callback_plugins/:/usr/share/ansible_plugins/callback_plugins こちらもansible.cfgで設定可能 ===== WinRMでAD認証するとエラー ===== windowsにアクセスすると以下のようなエラーが出る。\\ GSSError: (('Unspecified GSS failure. Minor code may provide more information', 851968) ActiveDirectoryを利用しているとき、Inventoryのansible_ssh_passが使用されていない kinit user@REALM これでとりあえず動くけど… **【追記】2015.11.05** どうやら、**kerberos認証の場合はキャッシュを使わないと動かない**模様。\\ いろいろ試したが、そもそもユーザ名が渡っておらず、ユーザ名に@がある場合にkerberosキャッシュを使っているみたい。\\ ドキュメントを読むとユーザ名に「@」がある場合にAD認証(kerberos)をすると書いてあるので、以下の設定でアクセス ユーザ名:'@' パスワード:'' ちゃんとつながりましたとさ...orz PowerSehllがsshサポートするので、2.0からsshに移行するみたいなので中途半端なままなのか? ===== 参考 ===== * [[http://qiita.com/myaaaaa_chan/items/f3473ae27f8e1b2abcac|Ansibleちょっとしたメモ]] * [[http://sharknet.us/2013/12/13/276/|How to Create Xen VMs Using Ansible]] * [[http://blog.keshi.org/hogememo/2015/12/07/exploiting-ansible-jinja2|Ansible の Jinja2 を活用する]] * [[https://gist.github.com/justinhennessy/28e82c2ec05f9081786a|Parsing JSON with Ansible ...]]