当前位置:  开发笔记 > 编程语言 > 正文

Ansible - 打印消息 - 调试:msg ="line1 \n {{var2}} \n line3 with var3 = {{var3}}"

如何解决《Ansible-打印消息-调试:msg="line1\n{{var2}}\nline3withvar3={{var3}}"》经验,为你挑选了3个好方法。

在Ansible(1.9.4)或2.0.0中

我运行了以下操作:

- debug: msg="line1 \n {{ var2 }} \n line3 with var3 = {{ var3 }}"

$ cat roles/setup_jenkins_slave/tasks/main.yml

- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"
  tags:
    - koba

- debug: msg="1 == Slave properties = fsroot[ {{ slave_fsroot }} ], master[ {{ slave_master }} ], connectingToMasterAs[ {{ slave_user }} ], description[ {{ slave_desc }} ], No.Of.Executors[ {{ slave_execs }} ], LABELs[ {{ slave_labels }} ], mode[ {{ slave_mode }} ]"
  tags:
    - koba


- debug: msg="print(2 == Slave properties = \n\nfsroot[ {{ slave_fsroot }} ],\n master[ {{ slave_master }} ],\n connectingToMasterAs[ {{ slave_user }} ],\n description[ {{ slave_desc }} ],\n No.Of.Executors[ {{ slave_execs }} ],\n LABELs[ {{ slave_labels }} ],\n mode[ {{ slave_mode }} ])"
  tags:
    - koba

但这不是用新行打印变量(对于第三个调试操作)?



1> diabloneo..:

调试模块支持数组,所以你可以这样做:

debug:
  msg:
    - "First line"
    - "Second line"

输出:

ok: [node1] => {
    "msg": [
        "First line",
        "Second line"
    ]
}

或者您可以使用此答案中的方法:

在YAML中,如何在多行中断字符串?



2> maikel..:

我发现使用debug打印多行文本最方便的方法是:

- name: Print several lines of text
  vars:
    msg: |
         This is the first line.
         This is the second line with a variable like {{ inventory_hostname }}.
         And here could be more...
  debug:
    msg: "{{ msg.split('\n') }}"

它将消息拆分为一个数组,调试将每一行打印为字符串.输出是:

ok: [example.com] => {
    "msg": [
        "This is the first line.", 
        "This is the second line with a variable like example.com", 
        "And here could be more...", 
        ""
    ]
}

感谢jhutar.



3> Serge Stroob..:

抑制的最后一个空字符串apt[:-1]

---
- name: 'apt: update & upgrade'
  apt:
    update_cache: yes
    cache_valid_time: 3600
    upgrade: safe
  register: apt
- debug: msg={{ apt.stdout.split('\n')[:-1] }}

debug:一行由于导致了不错的换行.split('\n'),并且由于导致了最后一个空字符串被抑制[:-1]。当然,所有这些都是Python字符串操作。

"msg": [
    "Reading package lists...", 
    "Building dependency tree...", 
    "Reading state information...", 
    "Reading extended state information...", 
    "Initializing package states...", 
    "Building tag database...", 
    "No packages will be installed, upgraded, or removed.", 
    "0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.", 
    "Need to get 0 B of archives. After unpacking 0 B will be used.", 
    "Reading package lists...", 
    "Building dependency tree...", 
    "Reading state information...", 
    "Reading extended state information...", 
    "Initializing package states...", 
    "Building tag database..."
]

推荐阅读
贴进你的心聆听你的世界
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有