2
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 1 year has passed since last update.

Ansibleのdebugモジュールにおいて\nを改行した状態で表示する

Posted at

備忘

課題: debugモジュールにおいて文字列中の改行コード\nをログにおいて実際に改行して表示したい。

debugモジュールにて、改行コードの入った文字列を表示すると、改行コードは\nとして表示され、改行は行われない。

playbook例

site.yml、
- hosts: localhost
  tasks:

  - debug:
      var: text
    vars:
      test: |
        foo
        bar
        baz

  - debug:
      msg: "{{ text }}"
    vars:
      text: |
        foo
        bar
        baz

出力例

$ ansible-playbook site.yml
...

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
    "text": "foo\nbar\nbaz"
}

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "foo\nbar\nbaz\n"
}

...

改行コードがそのまま\nとして表示されている。
ここで、改行コードを\nでなく実際に改行して表示したい。

ソリューション1: stdout_callback = community.general.yamlの利用

community.generalコレクションの導入

$ ansible-galaxy collection install community.general
...

~/.ansible/collections/ansible_collections/community/general/に導入される

ansible.cfgに、stdout_callbackとしてcommunity.general.yamlを使用する設定

ansible.cfg
[defaults]
#verbosity = 1
stdout_callback = community.general.yaml

実行結果

...

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => 
  text: |-
    foo
    bar
    baz

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => 
  msg: |-
    foo
    bar
    baz

...

改行されて出力された。
YAML表示の中のため、インデントされている。
|-の為、最後に改行コードが1つ以上存在してもそれらは出力されない。

ソリューション2: stdout_callback = ansible.posix.debugの利用

個人的にはこちらをよく使っている。

$ ansible-galaxy collection install ansible.posix
...
ansible.cfg
[defaults]
#verbosity = 1
stdout_callback = ansible.posix.debug

出力例

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {
    "text": "foo\nbar\nbaz\n"
}

TASK [debug] *********************************************************************************************************************************************************************************
ok: [localhost] => {}

MSG:

foo
bar
baz


...

var=の場合、改行コードは\nとして表示
msg=の場合、改行コードは改行され、インデントなし、最後の改行コードも省略されない。

2
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
2
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?