打印with_lines循环中条目的输出



在下面的剧本中,我需要打印从文本文件中执行的所有stdout_lines的输出。
我需要验证stdout_lines中是否存在某些字符串。

---
- hosts: '{{ host }}'
gather_facts: true
become: true
vars:
command_txt: '{{ txt_file }}'
tasks:
- name: apply patch on remote nodes
ansible.builtin.shell: some_command
register: output
with_lines: cat {{ command_txt }}
ignore_errors: yes   
- debug:
msg: "The patch apply status is::: {{ output }}" 

例如,给定用于测试的树

shell> tree /tmp/test
/tmp/test
├── commands.txt
├── file1.txt
├── patch1.txt
├── patch2.txt
└── patch3.txt
0 directories, 5 files
shell> cat /tmp/test/commands.txt 
patch file1.txt < patch1.txt
patch file1.txt < patch2.txt
patch file1.txt < patch3.txt
shell> cat /tmp/test/file1.txt 
shell> cat /tmp/test/patch1.txt 
--- file1.txt.orig  2022-12-06 06:54:57.349543101 +0100
+++ file1.txt   2022-12-06 06:52:13.493314972 +0100
@@ -0,0 +1 @@
+line 1
shell> cat /tmp/test/patch2.txt 
--- file1.txt.orig  2022-12-06 06:58:33.773844471 +0100
+++ file1.txt   2022-12-06 06:58:47.569863681 +0100
@@ -1 +1,2 @@
line 1
+line 2
shell> cat /tmp/test/patch3.txt 
--- file1.txt.orig  2022-12-06 06:59:36.633932008 +0100
+++ file1.txt   2022-12-06 06:59:48.049947906 +0100
@@ -1,2 +1,3 @@
line 1
line 2
+line 3

声明变量

my_path: /tmp/test
txt_file: /tmp/test/commands.txt

和迭代命令

- shell:
cmd: "{{ item }}"
chdir: "{{ my_path }}"
with_lines: "cat {{ txt_file }}"
register: output

补丁将被应用

shell> cat /tmp/test/file1.txt 
line 1
line 2
line 3

,输出将被注册为

output:
changed: true
msg: All items completed
results:
- ansible_facts:
discovered_interpreter_python: /usr/bin/python3
ansible_loop_var: item
changed: true
cmd: patch file1.txt < patch1.txt
delta: '0:00:00.003814'
end: '2022-12-06 07:25:24.057143'
failed: false
invocation:
module_args:
_raw_params: patch file1.txt < patch1.txt
_uses_shell: true
argv: null
chdir: /tmp/test
creates: null
executable: null
removes: null
stdin: null
stdin_add_newline: true
strip_empty_ends: true
warn: false
item: patch file1.txt < patch1.txt
msg: ''
rc: 0
start: '2022-12-06 07:25:24.053329'
stderr: ''
stderr_lines: []
stdout: patching file file1.txt
stdout_lines:
- patching file file1.txt
- ansible_loop_var: item
changed: true
cmd: patch file1.txt < patch2.txt
delta: '0:00:00.004013'
end: '2022-12-06 07:25:24.317642'
failed: false
invocation:
module_args:
_raw_params: patch file1.txt < patch2.txt
_uses_shell: true
argv: null
chdir: /tmp/test
creates: null
executable: null
removes: null
stdin: null
stdin_add_newline: true
strip_empty_ends: true
warn: false
item: patch file1.txt < patch2.txt
msg: ''
rc: 0
start: '2022-12-06 07:25:24.313629'
stderr: ''
stderr_lines: []
stdout: patching file file1.txt
stdout_lines:
- patching file file1.txt
- ansible_loop_var: item
changed: true
cmd: patch file1.txt < patch3.txt
delta: '0:00:00.003602'
end: '2022-12-06 07:25:24.582663'
failed: false
invocation:
module_args:
_raw_params: patch file1.txt < patch3.txt
_uses_shell: true
argv: null
chdir: /tmp/test
creates: null
executable: null
removes: null
stdin: null
stdin_add_newline: true
strip_empty_ends: true
warn: false
item: patch file1.txt < patch3.txt
msg: ''
rc: 0
start: '2022-12-06 07:25:24.579061'
stderr: ''
stderr_lines: []
stdout: patching file file1.txt
stdout_lines:
- patching file file1.txt
skipped: false

您可以看到列表results保留了迭代的项。打印任何您喜欢的信息。例如,

- debug:
msg: "{{ item.item }} rc: {{ item.rc }}"
loop: "{{ output.results }}"
loop_control:
label: "{{ item.item }}"

TASK [debug] *********************************************************************************
ok: [localhost] => (item=patch file1.txt < patch1.txt) => 
msg: 'patch file1.txt < patch1.txt rc: 0'
ok: [localhost] => (item=patch file1.txt < patch2.txt) => 
msg: 'patch file1.txt < patch2.txt rc: 0'
ok: [localhost] => (item=patch file1.txt < patch3.txt) => 
msg: 'patch file1.txt < patch3.txt rc: 0'

您可以将信息放入单个块中。例如,

- debug:
msg: |
{% for item in output.results %}
{{ item.item }} rc: {{ item.rc }}
{% endfor %}

TASK [debug] *********************************************************************************
ok: [localhost] => 
msg: |-
patch file1.txt < patch1.txt rc: 0
patch file1.txt < patch2.txt rc: 0
patch file1.txt < patch3.txt rc: 0

最新更新