这是我的剧本:
---
- hosts: all
gather_facts: no
ignore_unreachable: true
tasks:
- command: "{{ item }}"
loop:
- "ls"
- "df -h"
register: files
ignore_errors: true
- name: delegate
command: ls
register: files2
delegate_to: "{{item}}"
ignore_errors: true
with_items:
- "{{groups['test_servers1']}}"
- lineinfile:
line: "{{ item.stdout }}"
path: /tmp/list.log
create: yes
when:
- item.changed is defined
- item.changed
loop: "{{ files.results +
files2.results}}"
测试 1:
使用此广告资源:
192.168.153.31
#[test_servers1]
输出:
TASK [command] **************************************************************************************************
changed: [192.168.153.31] => (item=ls)
changed: [192.168.153.31] => (item=df -h)
TASK [delegate] **************************************************************************************************
fatal: [192.168.153.31]: FAILED! => {"msg": "'dict object' has no attribute 'test_servers1'"}
...ignoring
TASK [lineinfile] **************************************************************************************************
skipping: [192.168.153.31]
TEST2:
使用此广告资源:
192.168.153.31
[test_servers1]
输出:
TASK [command] *************************************************************************************************
changed: [192.168.153.31] => (item=ls)
changed: [192.168.153.31] => (item=df -h)
TASK [delegate] *************************************************************************************************
TASK [debug] *************************************************************************************************
ok: [192.168.153.31] => {
"files2": {
"changed": false,
"results": [],
"skipped": true,
"skipped_reason": "No items in the list"
}
}
TASK [lineinfile] ************************************************************************************************************************************************************************************************
changed: [192.168.153.31] => (item={'start': '2021-07-18 02:11:13.080046', 'stderr': '', 'rc': 0, 'invocation': {'module_args': {'_raw_par..........
问题 1:
由于第一个测试中的清单不包含 test_servers1
组,因此忽略该任务。但是为什么即使 lineinfile
包含 file
也会跳过 results
?
问题 2:
在 TEST2 中,我的 delegate
任务只包含一个命令,即 ls
为什么有 file2
results
?据我所知,循环多个命令时仅 register variable contains results
。
问题 3:
是否可以检查 files2
是否包含 results
模块中的 lineinfile
,如果是检查 files2.results
是否不为空,则只循环遍历它,否则跳过它?
答案 0 :(得分:1)
问题第一部分的答案是,files2
不包含字段 results
,因为它来自的任务抛出了错误,但您尝试连接 {{1} } 和 files.results
失败,因为 files2.results
不存在。如果您将此添加到您的剧本中:
files2.results
您会看到错误:
- name: files2
debug:
msg: "{{ files2 }}"
- name: files + files2
debug:
msg: "{{ files.results + files2.results }}"
所以 TASK [files2] ************************************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => {
"msg": {
"failed": true,
"msg": "'dict object' has no attribute 'test_servers1'"
}
}
TASK [files + files2] ****************************************************************************************************************************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'results'\n\nThe error appears to be in '/home/tommy/test/a.yml': line 23, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"{{ files2 }}\"\n - name: files + files2\n ^ here\n"}
的结果不存在,任务被跳过。
问题第二部分的答案是,带有循环的任务的输出将始终具有这种结构,无论您循环列表中的 0、1 或 100 个项目。它总是有 "{{ files.results + files2.results }}"
,但如果您遍历一个没有元素的列表,results
将为空。
第三部分的答案是,不要使用复杂的 results
条件,您应该为每个要写入文件的列表添加一个 when
任务,如下所示:
lineinfile