1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ansible Tips: shellモジュールにおけるデバックモード出力などの改行やタブを、\nや\tでなく、そのままログ画面に表示させる例

Last updated at Posted at 2020-12-14

追記 2021/07

以下の対応の方が容易かつ可読性が高いと思えます。

備忘

課題

shellモジュールにおけるデバックモード出力などの改行やタブは、stdoutを用いると\nや\tと表示されるし、stdout_linesを用いても\tは残る。
また、stdout_linesを用いる場合、"...",と表示されることになる。

例1 タブが\tなどと表示される例

tasks
  - name: Pacemaker status
    shell: |-
      set -x
      exec 2>&1
      crm status 
    register: r

  - name: Pacemaker status debug
    debug: { var: r.stdout_lines }

set -xにより実行コマンドをstderrに出力させる。
exec 2>&1により、stderrもstdoutに合わせて出力させる。

TASK [cm : Pacemaker status] ***************************************************
skipping: [cm2-rhel8]
skipping: [cm3-rhel8]
changed: [cm1-rhel8]

TASK [cm : Pacemaker status debug] *********************************************
skipping: [cm2-rhel8]
ok: [cm1-rhel8] => {
    "r.stdout_lines": [
        "+ crm status",
        "Cluster Summary:",
        "  * Stack: corosync",
        "  * Current DC: cm2-rhel8 (version 2.0.4-2.db2pcmk.el8-2deceaa3ae) - partition with quorum",
        "  * Last updated: Tue Jul  6 03:01:05 2021",
        "  * Last change:  Tue Jul  6 02:10:22 2021 by root via cibadmin on cm1-rhel8",
        "  * 2 nodes configured",
        "  * 8 resource instances configured",
        "",
        "Node List:",
        "  * Online: [ cm1-rhel8 cm2-rhel8 ]",
        "",
        "Full List of Resources:",
        "  * db2_cm1-rhel8_eth1\t(ocf::heartbeat:db2ethmon):\t Started cm1-rhel8",
        "  * db2_cm2-rhel8_eth1\t(ocf::heartbeat:db2ethmon):\t Started cm2-rhel8",
        "  * db2_cm1-rhel8_db2inst1_0\t(ocf::heartbeat:db2inst):\t Started cm1-rhel8",
        "  * db2_cm2-rhel8_db2inst1_0\t(ocf::heartbeat:db2inst):\t Started cm2-rhel8",
        "  * Clone Set: db2_db2inst1_db2inst1_MYDB-clone [db2_db2inst1_db2inst1_MYDB] (promotable):",
        "    * Masters: [ cm1-rhel8 ]",
        "    * Slaves: [ cm2-rhel8 ]",
        "  * db2_db2inst1_db2inst1_MYDB-primary-VIP\t(ocf::heartbeat:IPaddr2):\t Started cm1-rhel8",
        "  * db2_db2inst1_db2inst1_MYDB-standby-VIP\t(ocf::heartbeat:IPaddr2):\t Started cm2-rhel8"
    ]
}
skipping: [cm3-rhel8]

ソリューション

name: の内容は改行やタブが正しく展開されて表示される。これを利用すると、shellモジュールにおけるデバックモード出力を、そのままログ画面に表示できる。

例2 タブを展開して表示する例

  - loop: 
    - "{{ node1 }}"
    include_tasks: debug.yml
    vars:
      debug_content: |
        Pacemaker status debug at {{ item }}
        {{ hostvars[item].r.stdout }}

対象ノードが複数の場合、taskファイルを分けてinclude_tasksなどで読み込むことで、ノードごとにname:を変える事が可能。

debug.yml
---
- name: "{{ debug_content }}"
  assert:
    that: true
    quiet: true

assert: { that: true, quiet: true } は、余計な出力をしない、何もしないモジュールとして使用。

TASK [cm : include_tasks] ******************************************************
skipping: [cm2-rhel8] => (item=cm1-rhel8) 
skipping: [cm3-rhel8] => (item=cm1-rhel8) 
included: /home/hiroyukionodera/repo/db2cm_vagrant/roles/cm/tasks/debug.yml for cm1-rhel8 => (item=cm1-rhel8)

TASK [cm : Pacemaker status debug at cm1-rhel8
+ crm status
Cluster Summary:
  * Stack: corosync
  * Current DC: cm2-rhel8 (version 2.0.4-2.db2pcmk.el8-2deceaa3ae) - partition with quorum
  * Last updated: Tue Jul  6 03:01:05 2021
  * Last change:  Tue Jul  6 02:10:22 2021 by root via cibadmin on cm1-rhel8
  * 2 nodes configured
  * 8 resource instances configured

Node List:
  * Online: [ cm1-rhel8 cm2-rhel8 ]

Full List of Resources:
  * db2_cm1-rhel8_eth1  (ocf::heartbeat:db2ethmon):      Started cm1-rhel8
  * db2_cm2-rhel8_eth1  (ocf::heartbeat:db2ethmon):      Started cm2-rhel8
  * db2_cm1-rhel8_db2inst1_0    (ocf::heartbeat:db2inst):        Started cm1-rhel8
  * db2_cm2-rhel8_db2inst1_0    (ocf::heartbeat:db2inst):        Started cm2-rhel8
  * Clone Set: db2_db2inst1_db2inst1_MYDB-clone [db2_db2inst1_db2inst1_MYDB] (promotable):
    * Masters: [ cm1-rhel8 ]
    * Slaves: [ cm2-rhel8 ]
  * db2_db2inst1_db2inst1_MYDB-primary-VIP      (ocf::heartbeat:IPaddr2):        Started cm1-rhel8
  * db2_db2inst1_db2inst1_MYDB-standby-VIP      (ocf::heartbeat:IPaddr2):        Started cm2-rhel8] ***
ok: [cm1-rhel8]

検証環境

  • Fedora 34
  • ansible [core 2.11.2]
1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?