端くれプログラマの備忘録 Ansible [Ansible] registerモジュール – コマンドの結果を変数として登録する

[Ansible] registerモジュール – コマンドの結果を変数として登録する

registerモジュールを使うと、コマンドの結果を変数として登録できる。

- hosts: web_servers
  tasks:

     - shell: /usr/bin/foo
       register: foo_result
       ignore_errors:True

     - shell: /usr/bin/bar
       when: foo_result.rc == 5

結果はモジュールごとに異なるので、各モジュールのドキュメントのRETURNセクションを参照。

参考サイト

変数の使用 — Ansible Documentation
https://docs.ansible.com/ansible/2.9_ja/user_guide/playbooks_variables.html?highlight=register

Return Values — Ansible Documentation
https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html

Ansibleで実行結果の値を使って処理を変えたい〜registerの使い方〜 – Qiita
https://qiita.com/atsushi586/items/a591f1c6dee66773aaeb