LoginSignup
6
4

More than 3 years have passed since last update.

ansible-playbook でコマンド実行結果から条件分岐する方法

Posted at

概要

ansible-plyabookの条件分岐の方法がわからず調べていて、実際に実行して成功したのでアウトプットしました。

ansible-playbookを使用して、対象のコマンドの実行結果が成功であれば「成功」のメッセージを出すようにしています。

インベントリファイル

今回はテスト用で自端末を対象にします。

[localhost]
127.0.0.1 ansible_connection=local

playbook

記載内容はuptimeコマンドを実行する項目と、レジスタ内に入っている値の表示、そして実行結果が成功だったら「成功」を実行する項目の3つで構成しています。

- hosts: localhost
    tasks:
    - shell: uptime
      register: result

    - debug:
        msg: "{{ result }}"

    - debug:
        msg: "成功"
      when: result.rc == 0

実行結果

「inventory」にインベントリファイル、「conditionsTest.yml」にplaybookの内容を記述しています。

$ ansible-playbook -i inventory conditionsTest.yml

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [127.0.0.1]

TASK [shell] ***********************************************************************************************************************************************************************************************
changed: [127.0.0.1]

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": {
        "changed": true,
        "cmd": "uptime",
        "delta": "0:00:00.009835",
        "end": "2019-07-07 16:39:13.762565",
        "failed": false,
        "rc": 0,
        "start": "2019-07-07 16:39:13.752730",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "16:39  up 9 days, 17:47, 7 users, load averages: 1.62 2.08 2.11",
        "stdout_lines": [
            "16:39  up 9 days, 17:47, 7 users, load averages: 1.62 2.08 2.11"
        ]
    }
}

TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
    "msg": "成功"
}

PLAY RECAP *************************************************************************************************************************************************************************************************
127.0.0.1                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

コマンドの実行が成功しているので「"msg": "成功"」が出力されています。

補足

shellの実行結果をregisterで取得し、変数resultに格納しています。
「rc」に実行結果が格納されており、成功であれば0が格納されています。

そこでwhenを使用し、「uptimeの実行結果をregisterを使用してresult変数に格納、rcに0が格納されていれば成功を出力する」という条件にしています。

6
4
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
6
4