1
4

More than 5 years have passed since last update.

Ansibleでリモートホスト先でコマンド実行後JSON形式で返す

Last updated at Posted at 2018-03-01

Ansibleでリモート先でコマンド実行もいいけど

コマンドの実行結果ってほしいですよね。
Playbookを読み込んで実行させても、コマンド実行はしてくれるがどのような結果になるかはわからないのがちょっと残念でしたので、JSONで返してくれるといろんなことに使えそうじゃんと思いました。

hosts(inventry)作成

hosts
[localhost]
192.168.1.202

[virtualhosts]
192.168.33.11

あまりきれいではないのであんま参考にしなくてよいです

playbookの作成

virtual.yml
---
- name: virtual #お好きな名前
  hosts: all #hostsに対して指定あれば特にないならallで
  remote_user: hoge #ログイン先のユーザ名
  gather_facts: no

  tasks: #実際の操作
    - name: ping #お好きな名前
      command: "free -m" #コマンド実行

JSON形式で出力するようにコマンド実行

実行結果.js
[neruneru@localhost playbooks]$ ANSIBLE_CONFIG=/home/neruneru/ansible/examp
son_ansible.cfg ansible-playbook --ask-pass -i /home/neruneru/ansible/examples/hosts virtual.yml
SSH password:
{
    "plays": [
        {
            "play": {
                "id": "00199976-cf1f-a591-0634-000000000007",
                "name": "virtual"
            },
            "tasks": [
                {
                    "hosts": {
                        "192.168.1.202": {
                            "_ansible_no_log": false,
                            "_ansible_parsed": true,
                            "changed": true,
                            "cmd": [
                                "free",
                                "-m"
                            ],
                            "delta": "0:00:00.007026",
                            "end": "2018-03-01 00:22:32.043448",
                            "invocation": {
                                "module_args": {
                                    "_raw_params": "free -m",
                                    "_uses_shell": false,
                                    "chdir": null,
                                    "creates": null,
                                    "executable": null,
                                    "removes": null,
                                    "stdin": null,
                                    "warn": true
                                }
                            },
                            "rc": 0,
                            "start": "2018-03-01 00:22:32.036422",
                            "stderr": "",
                            "stderr_lines": [],
                            "stdout": "              total        used        fr                                                                                                                      buff/cache   available\nMem:           3951        1126         234          46                                                                                                                          2496\nSwap:          4093           7        4086",
                            "stdout_lines": [
                                "              total        used        free                                                                                                                         cache   available",
                                "Mem:           3951        1126         234                                                                                                                          2589        2496",
                                "Swap:          4093           7        4086"
                            ]
                        },
                        "192.168.33.11": {
                            "_ansible_no_log": false,
                            "_ansible_parsed": true,
                            "changed": true,
                            "cmd": [
                                "free",
                                "-m"
                            ],
                            "delta": "0:00:00.029999",
                            "end": "2018-02-28 15:22:32.312896",
                            "invocation": {
                                "module_args": {
                                    "_raw_params": "free -m",
                                    "_uses_shell": false,
                                    "chdir": null,
                                    "creates": null,
                                    "executable": null,
                                    "removes": null,
                                    "stdin": null,
                                    "warn": true
                                }
                            },
                            "rc": 0,
                            "start": "2018-02-28 15:22:32.282897",
                            "stderr": "",
                            "stderr_lines": [],
                            "stdout": "              total        used        fr                                                                                                                      buff/cache   available\nMem:            993          90         658          13                                                                                                                           749\nSwap:          2047           0        2047",
                            "stdout_lines": [
                                "              total        used        free                                                                                                                         cache   available",
                                "Mem:            993          90         658                                                                                                                           243         749",
                                "Swap:          2047           0        2047"
                            ]
                        }
                    },
                    "task": {
                        "id": "00199976-cf1f-a591-0634-000000000009",
                        "name": "ping"
                    }
                }
            ]
        }
    ],
    "stats": {
        "192.168.1.202": {
            "changed": 1,
            "failures": 0,
            "ok": 1,
            "skipped": 0,
            "unreachable": 0
        },
        "192.168.33.11": {
            "changed": 1,
            "failures": 0,
            "ok": 1,
            "skipped": 0,
            "unreachable": 0
        }
    }
}

では実行コマンドを1個づつ見てみましょう

■ANSIBLE_CONFIG=/home/neruneru/ansible/examp
son_ansible.cfg
⇒cfgファイルは以下の順番で優先して実行されます

■ansible.cfg の読み込み順序
以下の順番で設定が読み込まれます。最初に存在する設定が有効で、以降の設定ファイルは無視されます。
ANSIBLE_CONFIG(環境変数) で指定
./ansible.cfg
~/ansible.cfg
/etc/ansible/ansible.cfg

★今回は、ANSIBLE_CONFIGを指定しています

ちなみにjson_ansible.cfgの中身は

json_ansible.cfg
[defaults]
stdout_callback = json

●ansible-playbook --ask-pass -i /home/neruneru/ansible/examples/hosts virtual.yml

 --ask-passはSSHでアクセス拒否にならないようにするオプションコマンド
-iはhostsの指定
virtual.ymlでplaybookを指定

実行すると、SSH passwordを聞かれるので出力

【参考文献】
--ask-passについて
JSONで出力
playbook,inventry

今後

・SSHを今は毎回打っているので、もう自動で打ってくれるようにする
・hostsの中身がいけていない
・JSONから配列処理をするように・・

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