0
1

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: shellモジュールで実行された複数行にわたるコマンドを改行された状態で表示

Posted at

要望

shellモジュールで実行された複数行にわたるコマンドを、後続のdebugモジュールにて、改行された状態で確認したい

##ソリューション

フィルターでは適当なものが見つからなかったが、pythonのsplitlines()にて可能

playbook 例

playbook.yml

---
- hosts: all
  gather_facts: false
  tasks:

    - shell: |
        echo a
          echo b
        echo c
      register: r

    - debug: { var: r.cmd }              # <= そのまま出力すると読みにくい
    
    - debug: { var: r.cmd.splitlines() } # <= splitlines()にて分離

    - debug: { var: r.stdout_lines }
    - debug: { var: r.stderr_lines }

実行例

$ ansible-playbook -i localhost, -c local playbook.yml

PLAY [all] *******************************************************************************************************

TASK [shell] *****************************************************************************************************
changed: [localhost]

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "r.cmd": "echo a\n  echo b\necho c\n" # <= そのまま出力した例
}

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "r.cmd.splitlines()": [
        "echo a",               # <= 行で分離して出力した例 
        "  echo b",              # <= 行で分離して出力した例
        "echo c"                # <= 行で分離して出力した例
    ]
}

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "r.stdout_lines": [
        "a",
        "b",
        "c"
    ]
}

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
    "r.stderr_lines": []
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

なお、(shellモジュールではなく、)commandモジュールでは、レジストリー変数に文字列として渡されないので使用できない。
fail時にも行で分離して表示できると嬉しいが。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?