即使我的条件字符串满足要求,并且可以在stdout中看到此字符串,Ansible也会跳过任务的执行。
我尝试使用复制模块中的content选项,其中我用{{ansible_host}}替换了{{inventory_hostname}}
我尝试用result.stdout替换result.stdout_lines,因为stdout输出是一个字典。
---
- name: Detecting NXOS 5K devices which fail the 'copy run start' from device inventory
hosts: nxos-5k
gather_facts: false
connection: local
tasks:
- name: Gather device specific facts
nxos_facts:
gather_subset: config
register: net_facts
- name: Executing 'copy run start' on NXOS 5K chassis
nxos_command:
commands:
- 'copy run start'
register: result
ignore_errors: True
- debug: var=result.stdout_lines
- name: Copy the device name to failed device list
copy:
content: "{{inventory_hostname}}"
dest: /home/saurasar/ansibledev/results/failed_nxos_devices.txt
when: "'Configuration update aborted: request was aborted' in result.stdout_lines"
预期结果:字符串搜索应该成功,因为从-vvv可以看到result.stdout包含字符串。
"stdout_lines": [[0m
[0;32m [[0m
[0;32m "[# ] 1%", [0m
[0;32m "[# ] 2%", [0m
[0;32m "[## ] 3%", [0m
[0;32m "[## ] 4%", [0m
[0;32m "[### ] 5%", [0m
[0;32m "[### ] 6%", [0m
[0;32m "[### ] 7%", [0m
[0;32m "[#### ] 8%", [0m
[0;32m "[##### ] 10%", [0m
[0;32m "[##### ] 11%", [0m
[0;32m "[##### ] 12%", [0m
[0;32m "[###### ] 13%", [0m
[0;32m "[###### ] 14%", [0m
[0;32m "[####### ] 15%", [0m
[0;32m "[####### ] 16%", [0m
[0;32m "[####### ] 17%", [0m
[0;32m "[######## ] 19%", [0m
[0;32m "[######### ] 20%", [0m
[0;32m "[######### ] 21%", [0m
[0;32m "[######### ] 22%", [0m
[0;32m "[########## ] 23%", [0m
[0;32m "[########## ] 24%", [0m
[0;32m "[########### ] 25%", [0m
[0;32m "[########### ] 26%", [0m
[0;32m "[############ ] 28%", [0m
[0;32m "[############ ] 29%", [0m
[0;32m "[############# ] 30%", [0m
[0;32m "[############# ] 31%", [0m
[0;32m "[############# ] 32%", [0m
[0;32m "[############## ] 33%", [0m
[0;32m "[############## ] 34%", [0m
[0;32m "[############### ] 35%", [0m
[0;32m "[############### ] 37%", [0m
[0;32m "[################ ] 38%", [0m
[0;32m "[################ ] 39%", [0m
[0;32m "[################# ] 40%", [0m
[0;32m "[################# ] 41%", [0m
[0;32m "[################# ] 42%", [0m
[0;32m "[################## ] 43%", [0m
[0;32m "[################## ] 44%", [0m
[0;32m "[################### ] 46%", [0m
[0;32m "[################### ] 47%", [0m
[0;32m "[#################### ] 48%", [0m
[0;32m "[#################### ] 49%", [0m
[0;32m "[##################### ] 50%", [0m
[0;32m "[##################### ] 51%", [0m
[0;32m "[##################### ] 52%", [0m
[0;32m "[###################### ] 53%", [0m
[0;32m "[####################### ] 55%", [0m
[0;32m "[####################### ] 56%", [0m
[0;32m "[####################### ] 57%", [0m
[0;32m "[######################## ] 58%", [0m
[0;32m "[######################## ] 59%", [0m
[0;32m "[######################### ] 60%", [0m
[0;32m "[######################### ] 61%", [0m
[0;32m "[######################### ] 62%", [0m
[0;32m "[########################## ] 64%", [0m
[0;32m "[########################### ] 65%", [0m
[0;32m "[########################### ] 66%", [0m
[0;32m "[########################### ] 67%", [0m
[0;32m "[############################ ] 68%", [0m
[0;32m "[############################ ] 69%", [0m
[0;32m "[############################# ] 70%", [0m
[0;32m "[############################# ] 71%", [0m
[0;32m "[############################## ] 73%", [0m
[0;32m "[############################## ] 74%", [0m
[0;32m "[############################### ] 75%", [0m
[0;32m "[############################### ] 76%", [0m
[0;32m "[############################### ] 77%", [0m
[0;32m "[################################ ] 78%", [0m
[0;32m "[################################ ] 79%", [0m
[0;32m "[################################# ] 80%", [0m
[0;32m "[################################# ] 82%", [0m
[0;32m "[################################## ] 83%", [0m
[0;32m "[################################## ] 84%", [0m
[0;32m "[################################### ] 85%", [0m
[0;32m "[################################### ] 86%", [0m
[0;32m "[################################### ] 87%", [0m
[0;32m "[#################################### ] 88%", [0m
[0;32m "[#################################### ] 89%", [0m
[0;32m "[##################################### ] 91%", [0m
[0;32m "[##################################### ] 92%", [0m
[0;32m "[###################################### ] 93%", [0m
[0;32m "[###################################### ] 94%", [0m
[0;32m "[####################################### ] 95%", [0m
[0;32m "[####################################### ] 96%", [0m
[0;32m "[####################################### ] 97%", [0m
[0;32m "[########################################] 98%", [0m
[0;32m "[########################################] 100%", [0m
[0;32m "Copy complete, now saving to disk (please wait)..."[0m
实际结果:
TASK [Copy the device name to failed device list] ***********************************************************************************************************************************************************************
task path: /home/saurasar/ansibledev/failure.detection.2.yml:22
skipping: [Device hostname] => {
"changed": false,
"skip_reason": "Conditional result was False"
}
META: ran handlers
META: ran handlers
答案 0 :(得分:0)
经过更多尝试,我找到了另一种方法,看起来像是解决了该问题!我只是在stdout的末尾添加了[0],就可以了。时间:“ result.stdout_lines [0]中的“配置更新已中止:请求已中止””