运行 Ansible playbook,是否有解决方案可以打印文件内容而无需任何额外字符?就像 Linux 上的一只简单的猫。
有简单的文本文件:
cat /tmp/report-2023-10-19.html.txt
server1 : The D:\ drive size is WRONG! Expected drive size: '1009'. Current size is: 100
server2 : The D:\ drive size is WRONG! Expected drive size: '1009'. Current size is: 100
server3 : The C:\ drive size is WRONG! Expected drive size: '12899'. Current size is: 126.51
server4 : The C:\ drive size is WRONG! Expected drive size: '12899'. Current size is: 126.51
我想用 Ansible 显示文件内容:
- name: Display all findings
debug:
var: "file_content"
vars:
file_content: "{{ lookup('file', '/tmp/{{ reportfile }}.txt').splitlines() }}"
delegate_to: localhost
run_once: true
when: num_lines.stdout | int > 0
tags: always
结果:
ok: [vmstfsh1 -> localhost] => {
"file_content": [
" server1 : The D:\\ drive size is WRONG! Expected drive size: '1009'. Current size is: 100 ",
"",
" server2 : The D:\\ drive size is WRONG! Expected drive size: '1009'. Current size is: 100 ",
"",
" server3 : The C:\\ drive size is WRONG! Expected drive size: '12899'. Current size is: 126.51 ",
"",
" server4 : The C:\\ drive size is WRONG! Expected drive size: '12899'. Current size is: 126.51"
]
}
有没有办法打印文件内容而不带任何额外的字符(“,)?就像Linux上的一只简单的猫一样。 我找到了 ANSIBLE_STDOUT_CALLBACK 变量,但我只能使用此选项(yam 或 json)更改格式。
对于Linux PC,请使用shell命令cat。
对于 Windows PC,请使用 ansible.windows.win_command powershell.exe Get-Content。
对于 Linux 客户端 PC:
- name: Display all findings
hosts: linux
tasks:
- name: Display file content
shell: cat /tmp/{{ reportfile }}.txt
register: file_content
changed_when: false # Do not mark as changed
- name: Display file content without extra characters
debug:
var: file_content.stdout_lines
when: file_content.stdout_lines is defined
对于 Windows 客户端 PC:
- name: Display all findings
hosts: win
become_method: runas
tasks:
- name: Read the file
ansible.windows.win_command: powershell.exe Get-Content C:\\tmp\{{ reportfile }}.txt
register: file_content
changed_when: false # Do not mark as changed
- name: Display file content line by line
debug:
var: item
loop: "{{ file_content.stdout_lines }}"
when: file_content.stdout_lines is defined