4
3

More than 5 years have passed since last update.

ansibleを使ってinventory内の任意のグループにコマンドを発行する

Posted at

ansibleは、playbookを用いて、任意のサーバにプロビジョニングを行うことができます。
実は、それ以外にも、inventoryファイルで管理しているサーバに対して、任意のコマンドを発行することができます。
実際に運用していると、使う機会も出てくるので、覚えておくととても便利です。

例で使用するlocal/inventoryファイル

[app_servers]
192.168.50.11
192.168.50.12

よく使われるping

疎通例として出てくるので、よくみるping送信

ansible -i local/inventory app_servers -m ping

# 実行結果
192.168.50.12 | success >> {
    "changed": false,
    "ping": "pong"
}

192.168.50.11 | success >> {
    "changed": false,
    "ping": "pong"
}

任意のコマンドを送る

ansible -i inventoryファイル inventory内の任意のグループ -u ユーザ名 -m shell -a "任意のコマンド"

コマンドの実行

pwdでログインしたユーザのホームのPathを取得する例です。

ansible -i local/inventory app_servers -u vagrant -m shell -a "pwd"

# 実行結果
192.168.50.11 | success | rc=0 >>
/home/vagrant

192.168.50.12 | success | rc=0 >>
/home/vagrant

使い時

  • 障害時の調査
  • 設定の確認 など
4
3
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
4
3