■ はじめに
Ansible には callback plugin という仕組みがあり、結果出力の形式を変更したりすることができます。 yaml に変更すると、改行コードごとに改行された形で標準出力されます。
本記事でも、callback plugin を yaml に変更する方法ど、出力例をご紹介します。
なお、本件は先日メンターとして参加させていただいた、Ansibleもくもく会 (サーバ編 & NW編)2019.07 での質疑応答でのやりとりの中で、参加者からいただいたコメントで気付きを得ました。 (いままで特に yaml に変更する用途が思い浮かびませんでた・・)
- 動作確認環境: Ansible 2.8.0
- 対象器機: Cisco DevNet Sandox 上の IOS-XE (とても便利です)
■ callback plugin を yaml に変更する
ansible.cfg や環境変数などで変更できます(参考: DEFAULT_STDOUT_CALLBACK)。今回は、ansible.cfg で変更します。
[defaults] stdout_callback = yaml
なお、ansible-playbook コマンドではなく、ansible コマンドでこの設定変更を有効にするためには、さらに bin_ansible_callbacks = True も必要です。
■ 出力例
Playbook
今回は、 ios_command モジュールを利用して、show version コマンドを実行し、出力結果の stdout を出力させます。
- hosts: ios gather_facts: no tasks: - name: test ios_command: commands: - show version register: result - name: debug debug: var: result.stdout
実行
$ ansible-playbook -i inventory ios_test.yml
PLAY [ios] ************************************************************************************************************
TASK [test] ***********************************************************************************************************
ok: [iosal1]
TASK [debug] **********************************************************************************************************
ok: [iosal1] =>
result.stdout:
- |-
Cisco IOS XE Software, Version 16.08.01
Cisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.8.1, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2018 by Cisco Systems, Inc.
Compiled Tue 27-Mar-18 13:32 by mcpre
(...略...)
3 Gigabit Ethernet interfaces
32768K bytes of non-volatile configuration memory.
16370384K bytes of physical memory.
7774207K bytes of virtual hard disk at bootflash:.
0K bytes of WebUI ODM Files at webui:.
Configuration register is 0x2102
PLAY RECAP ************************************************************************************************************
iosal1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
このように、改行コードが解釈された形で表示されます。
なお、
result.stdout: - |-
の | は YAML で複数行の値を示す Syntax です。
参考(デフォルトの場合)
参考までに、デフォルトの場合の出力結果を抜粋して掲載します。
ok: [iosal1] => {
"result.stdout": [
"Cisco IOS XE Software, Version 16.08.01\nCisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.8.1, RELEASE SOFTWARE (fc3)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2018 by Cisco Systems, Inc.\nCompiled Tue 27-Mar-18 13:32 by mcpre\(...略...)3 Gigabit Ethernet interfaces\n32768K bytes of non-volatile configuration memory.\n16370384K bytes of physical memory.\n7774207K bytes of virtual hard disk at bootflash:.\n0K bytes of WebUI ODM Files at webui:.\n\nConfiguration register is 0x2102"
]
}
このように、改行コード \n はそのまま表示されます。
デフォルトの場合でも、stdout ではなく、stdout_lines を表示するようにすると、1行1リストアイテムとして表示されるので見やすいです。ただしクォーテーションが入ります。
ok: [iosal1] => {
"result.stdout_lines": [
[
"Cisco IOS XE Software, Version 16.08.01",
"Cisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.8.1, RELEASE SOFTWARE (fc3)",
"Technical Support: http://www.cisco.com/techsupport",
"Copyright (c) 1986-2018 by Cisco Systems, Inc.",
"Compiled Tue 27-Mar-18 13:32 by mcpre",
(...略...)
"3 Gigabit Ethernet interfaces",
"32768K bytes of non-volatile configuration memory.",
"16370384K bytes of physical memory.",
"7774207K bytes of virtual hard disk at bootflash:.",
"0K bytes of WebUI ODM Files at webui:.",
"",
"Configuration register is 0x2102"
]
]
}
■ まとめ
callback plugin を yaml に変更して、改行コードごとに改行された形で標準出力されることをご紹介しました。
[2023/04/20]
この記事の4年後に、似たような記事をまた書いてしまったのでリンクを掲載します。以下の記事では、コレクションに移行後なので community.general.yaml という表記になっています。