以纯格式显示调试输出Ansible

时间:2019-03-01 03:40:25

标签: 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"
    }
}

如何摆脱'\ 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

有没有一种很好的显示方式?

1 个答案:

答案 0 :(得分:1)

您可以使用debug回调插件。

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

ANSIBLE_STDOUT_CALLBACK=debug ansible-playbook ...

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

stdout_callback = debug