在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
但这不是用新行打印变量(对于第三个调试操作)?
调试模块支持数组,所以你可以这样做:
debug: msg: - "First line" - "Second line"
输出:
ok: [node1] => { "msg": [ "First line", "Second line" ] }
或者您可以使用此答案中的方法:
在YAML中,如何在多行中断字符串?
我发现使用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.
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..." ]