下面是我打印文件的剧本。
我使用了几种方法,但文件没有按原样打印,即当 ansible 打印文件内容时,新行格式消失了。
- name: List startup files
shell: cat /tmp/test.txt
register: readws
- debug:
msg: "/tmp/test.txt on {{ inventory_hostname }} is: {{ readws.stdout_lines }}"
- debug:
msg: "/tmp/test.txt on {{ inventory_hostname }} is: {{ lookup('file', '/tmp/test.txt') }}"
cat /tmp/test.txt
i
m
good
预期 Ansible 输出:
TASK [debug] *****************************************************************************************
ok: [localhost] => {
"msg": "/tmp/test.txt on localhost is:
i
m
good
"
}
Ansible 输出:
TASK [List startup files] ******************************************************************
changed: [localhost]
TASK [debug] *****************************************************************************************
ok: [localhost] => {
"msg": "/tmp/test.txt on localhost is: [u'i', u'm ', u'good']"
}
TASK [debug] *****************************************************************************************
ok: [localhost] => {
"msg": "/tmp/test.txt on localhost is: i\nm \ngood"
}
你能推荐一下吗?
你无法真正得到你需要的东西(除非你改变输出回调插件......)。
您可以获得的最接近的结果是显示行列表,如下例所示:
- name: Show file content
vars:
my_file: /tmp/test.txt
msg_content: |-
{{ my-file }} on localhost is:
{{ lookup('file', my_file) }}
debug:
msg: "{{ msg_content.split('\n') }}"
这可能会对那些寻找更简单的方法在 ansible 中显示文件的人有所帮助。
stdout_lines 以合理的格式打印文件。
- name: display the file needed
shell: cat /tmp/test.txt
register: test_file
- debug:
var: test_file.stdout_lines
我认为最简洁的输出是使用 ansible.builtin.pause 的提示产生的(您可以使用零秒延迟)。例如
- name: Output
ansible.builtin.pause:
seconds: 0
prompt: "{{ lookup('file', '/tmp/test.txt') }}"You can use backslash-
您可以使用 在这些引号内添加额外的空白上方/下方的输出