LoginSignup
14
20

More than 5 years have passed since last update.

Ansible のregister ,with_items,stdout_linesについて

Posted at

軽く調べたので復習がてら軽くまとめる

register

何らかの処理結果を変数に格納し、再利用するための機構。

with_items

ループを構築するための機構、種々の言語におけるforeachのようなもの。

stdout_lines

stdoutの亜種
stdoutはregisterで登録した変数の標準出力にアクセスするためのコマンド
stdout_lineはstdoutの出力結果を一行ずつに分割してくれるもの

commandとshellについて

どちらもシェルコマンドを実行するための機構だが、一部性質が異なる。

shell は実行マシンにおけるbin/sh を経由して実行され、
commandは bin/shを経由せずに実行される。

このため、commandにおいてはパイプ処理や環境変数の参照などが行えない。
環境によらない処理となるため、冪等性などの観点ではcommandの方が優秀と言える。
逆に環境変数などを利用したいときはshellを使うべき。

http://docs.ansible.com/ansible/command_module.html#command
http://docs.ansible.com/ansible/shell_module.html
https://bacchi.me/ansible/tips-2/

まとめ

これらをまとめて使うことで、下記の公式のplaybookのように、特定のコマンド結果を利用したループ処理などが構築できる。

14
20
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
14
20