在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
但这不是用新行打印变量(对于第3次调试操作)?
答案 0 :(得分:51)
调试模块支持数组,所以你可以这样做:
debug:
msg:
- "First line"
- "Second line"
输出:
ok: [node1] => {
"msg": [
"First line",
"Second line"
]
}
或者您可以使用此答案中的方法:
答案 1 :(得分:41)
我发现通过调试打印多行文本最方便的方法是:
- 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。
答案 2 :(得分:3)
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..."
]
答案 3 :(得分:2)
我在@Bruce P上通过sed了解了关于管道输出的问题,这就是我的目标:
ansible-playbook [blablabla] | sed 's/\\n/\n/g'
如果有人有兴趣。
答案 4 :(得分:1)
这是discussed here。简而言之,您需要通过sed管道输出以将\n
转换为实际换行符,或者您需要编写一个回调插件来为您执行此操作。
答案 5 :(得分:0)
作为解决方法,我使用了 with_items ,这对我有用。
- debug: msg="Installing swarm slave = {{ slave_name }} at {{ slaves_dir }}/{{ slave_name }}"
- debug: msg="Slave properties = {{ item.prop }} [ {{ item.value }} ]"
with_items:
- { prop: 'fsroot', value: "{{ slave_fsroot }}" }
- { prop: 'master', value: "{{ slave_master }}" }
- { prop: 'connectingToMasterAs', value: "{{ slave_user }}" }
- { prop: 'description', value: "{{ slave_desc }}" }
- { prop: 'No.Of.Executors', value: "{{ slave_execs }}" }
- { prop: 'LABELs', value: "{{ slave_labels }}" }
- { prop: 'mode', value: "{{ slave_mode }}" }
tags:
- koba
答案 6 :(得分:0)
我发现以格式显示消息的最方便,最简单的方法(例如:换行,制表符...)是使用“ pause”模块而不是“调试”模块:
- pause:
seconds: 1
prompt: |
======================
line_1
line_2
======================
您还可以在提示中包括一个包含格式(换行,制表符...)的变量,该变量将按预期显示:
- name: test
hosts: all
vars:
line3: "\n line_3"
tasks:
- pause:
seconds: 1
prompt: |
/////////////////
line_1
line_2 {{ line3 }}
/////////////////
-
如果要显示命令的输出,而不必运行额外的任务来运行命令并注册输出,则可以直接在提示中使用管道查找并一次完成该工作:
- pause:
seconds: 1
prompt: |
=========================
line_1
{{ lookup('pipe', 'echo "line_2 with \t tab \n line_3 "') }}
line_4
=========================
-
如果您有多个主机,请注意“ pause”任务将运行 仅针对主机列表中的第一台主机。
这意味着,如果要显示的变量仅存在于 部分主机,第一个主机不包含该变量 那么你会得到一个错误。
为避免此类问题,请使用{{hostvars ['my_host'] ['my_var']}} 而不是{{my_var}}
将“暂停”与“何时”条件结合使用可能会跳过任务!为什么? 因为任务只会针对第一个主机运行一次 可能不符合规定的“何时”条件。
为避免这种情况,请不要使用限制数量的条件 主持人!因为您也不需要它,因为您知道任务将 无论如何只运行一次,也请使用上述hostvars来确保 无论所选择的主机是什么,您都会获得所需的变量。
示例:
不正确:
- name: test
hosts: host1,host2
vars:
display_my_var: true
tasks:
- when: inventory_hostname == 'host2'
set_fact:
my_var: "hi there"
- when:
- display_my_var|bool
- inventory_hostname == 'host2'
pause:
seconds: 1
prompt: |
{{ my_var }}
此示例将跳过暂停任务,因为它将仅选择第一个主机“ host1”,然后开始评估条件,当它发现“ host1”不符合第二个条件时,将跳过任务。 / p>
正确:
- name: test
hosts: host1,host2
vars:
display_my_var: true
tasks:
- when: inventory_hostname == 'host2'
set_fact:
my_var: "hi there"
- when: display_my_var|bool
pause:
seconds: 1
prompt: |
{{ hostvars['host2']['my_var'] }}
另一个显示消息的内容取决于主机的示例:
- set_fact:
my_var: "hi from {{ inventory_hostname }}"
- pause:
seconds: 1
prompt: |
{% for host in ansible_play_hosts %}
{{ hostvars[host]['my_var'] }}
{% endfor %}
答案 7 :(得分:0)
您可以使用寄存器变量的stdout_lines
:
- name: Do something
shell: "ps aux"
register: result
- debug: var=result.stdout_lines
答案 8 :(得分:0)
我对要打印到控制台的日志文件有类似的问题。 split("\n")
可以正常工作,但是会在每行中添加可见的\n
,所以我找到了更好的方法
tasks:
- name: Read recent lines from logfile for service {{ appName }}
shell: tail -n 1000 {{ logFile }}
register: appNameLogFile
- debug:
msg: "This is a stdout lines"
with_items: "{{ appNameLogFile.stdout }}"
它遍历appNameLogFile
的每一行,并且副作用是将此行打印到控制台中。您可以将其更新为
msg: "This is a stdout lines: {{ item }}"
但是在我的情况下并不需要