仅当在已注册变量中找到字符串时,Ansible任务继续
我有下面的代码仅当在已注册变量中找到字符串时,Ansible任务继续,ansible,Ansible,我有下面的代码 - name: Try to delete file shell: rm -rf /tmp/"{{ file_Del }}" register: result ignore_errors: True - name: Display result debug: var: result.stdout result.stdout可以是 “result.stderr”:“服务器错误(未找到):未找到文件/tmp/somefile” 或 “result.s
- name: Try to delete file
shell: rm -rf /tmp/"{{ file_Del }}"
register: result
ignore_errors: True
- name: Display result
debug:
var: result.stdout
result.stdout可以是“result.stderr”:“服务器错误(未找到):未找到文件/tmp/somefile”
或
“result.stderr”:“
rm
不是很健谈。看见但是可以使用find
和xargs
。比如这出戏
- hosts: localhost
vars:
file_del: xxx
tasks:
- shell: "find /tmp/{{ file_del }} | xargs /bin/rm"
ignore_errors: True
register: result
- meta: end_play
when: result.rc == 123
- debug:
msg: /tmp/{{ file_del }} found and deleted. Continue play.
给出(当找到并删除/tmp/{{file_del}}}时)
并在未找到文件result.rc==123
时结束播放
注释
- 确保在未找到文件时报告rc 123
- 可以测试result.stderr
如果可能,编写一个自定义命令。比如说
$ cat my_custom_command.sh
#!/bin/sh
if [ "$#" -eq "0" ]; then
echo "[ERR 123] No files found.";
exit 123;
fi
echo "Here I can do whatever I want with $@";
exit 0;
当你看这个问题的时候,看起来你的工作有点混乱,并且试图以这种方式继续下去。我认为最好不要朝这个方向走得更远,而是以更专业的方式工作 现在;更优雅的解决方案,仅使用Ansible模块
---
- hosts: local_test
# gather_facts: False
vars:
file_Del: test
tasks:
- name: find some files at a location, recursively
find:
paths: "/tmp/{{ file_Del }}"
recurse: true
register: found_files
- name: display files found, to be deleted, could be empty
debug:
msg: "Files found are {{ item.path }}"
with_items:
- "{{ found_files.files }}"
- name: delete files when found
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ found_files.files | default ([]) }}"
- name: display files found, to be deleted, could be empty
debug:
msg: "{{ found_files }}"
- name: end play when no files are found
meta: end_play
when: found_files.matched == 0
- name: this task is skipped when there are no files found, but executed when files were deleted
shell: echo hi
关于您的问题,请使用“未找到rm-rf命令”
;这看起来很不寻常,例如,您同时在Linux和Windows主机上执行playbook,而任务在Windows主机上会失败。我知道的每台Linux主机都预装了rm
,即使是大多数Docker容器和OpenBSD主机
将这些任务按环境分开可能是明智的
也许一点背景知识可以帮助我们,最终帮助你走出困境。你只想在这里使用end_play,还是可以使用任何其他逻辑?你的“何时”状态对比赛结束不起作用吗?忽略错误不是很流行,不是吗?不能简单地在一个任务中找到文件,然后在下一个任务中,在找到文件时删除文件。不,不是。除非有充分的理由。例如,处理错误代码。@Kevin C:是什么阻止您发布自己的答案?实际的shell命令不是“rm”,而是我们的自定义命令。所以,这就是为什么我需要使用“find”来查找我在注册变量中拥有的内容,并在除了
“result.stderr”之外还有其他内容时终止Ansible:“来自服务器的错误(NotFound):名称空间\“mynamespace \“NotFound”或result.stderr==”
,如果使用自定义命令,效果会更好。是什么阻止您在自定义命令中设置返回码?
$ cat my_custom_command.sh
#!/bin/sh
if [ "$#" -eq "0" ]; then
echo "[ERR 123] No files found.";
exit 123;
fi
echo "Here I can do whatever I want with $@";
exit 0;
---
- hosts: local_test
# gather_facts: False
vars:
file_Del: test
tasks:
- name: find some files at a location, recursively
find:
paths: "/tmp/{{ file_Del }}"
recurse: true
register: found_files
- name: display files found, to be deleted, could be empty
debug:
msg: "Files found are {{ item.path }}"
with_items:
- "{{ found_files.files }}"
- name: delete files when found
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ found_files.files | default ([]) }}"
- name: display files found, to be deleted, could be empty
debug:
msg: "{{ found_files }}"
- name: end play when no files are found
meta: end_play
when: found_files.matched == 0
- name: this task is skipped when there are no files found, but executed when files were deleted
shell: echo hi