以干净的 Ansible 格式显示调试输出

SSF*_*SSF 3 ansible

我正在尝试在 Ansible 中以良好的格式显示调试命令的消息输出。目前输出如下:

TASK [stop : Report Status of Jenkins Process] *******************************************************************************************************************************
ok: [localhost] => {
    "msg": "Service Jenkins is Running.\nReturn code from `grep`:\n0\n"
}

TASK [stop : debug] **********************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "changed": false,
        "failed": false,
        "msg": "Service Jenkins is Running.\nReturn code from `grep`:\n0\n"
    }
}
Run Code Online (Sandbox Code Playgroud)

如何去掉 '\n' 字符并替换为新行?使用下面的代码split('\n')不起作用。

- name: Checking Jenkins Process
  shell: "ps -ef | grep -v grep | grep -v dhclient | grep jenkins"
  ignore_errors: yes
  register: jenkins_process

- debug:
    var: jenkins_process.rc

- name: Report Status of Jenkins Process
  fail:
    msg: |
      Service Jenkins is not found
      Return code from `grep`:
      {{ jenkins_process.rc }}
  when: jenkins_process.rc != 0
  register: report

- name: Report Status of Jenkins Process
  debug:
    msg: |
      Service Jenkins is Running.
      Return code from `grep`:
      {{ jenkins_process.rc }}
  when: jenkins_process.rc == 0
  register: report

- debug:
    msg: "{{ report.split('\n') }}"

- name: Stop Jenkins Service
  service:
    name: jenkins
    state: stopped
Run Code Online (Sandbox Code Playgroud)

有没有办法以漂亮的方式显示它?

seb*_*ert 6

您可以使用调试回调插件。

您可以在命令行上指定它:

ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook ...
Run Code Online (Sandbox Code Playgroud)

或者在您的配置文件default部分ansible.cfg

stdout_callback = debug
Run Code Online (Sandbox Code Playgroud)