Ansible集合附加引号和break exec

Ansible集合附加引号和break exec,ansible,yaml,Ansible,Yaml,我创建剧本(当然,这只是第一个命令,而不是打断我的执行) 并有一个错误: changed: [hdp86] => (item=touch ~/memlog_hdp86.txt) failed: [hdp86] (item=if [ -f ~/memlog_hdp86.txt ]; then rm ~/memlog_hdp86.txt; fi) => {"changed": false, "cmd": "if '[' -f /home/karmatskiyrg/memlog_hdp86

我创建剧本(当然,这只是第一个命令,而不是打断我的执行)

并有一个错误:

changed: [hdp86] => (item=touch ~/memlog_hdp86.txt)
failed: [hdp86] (item=if [ -f ~/memlog_hdp86.txt ]; then rm ~/memlog_hdp86.txt; fi) => {"changed": false, "cmd": "if '[' -f /home/karmatskiyrg/memlog_hdp86.txt '];' then rm '/home/karmatskiyrg/memlog_hdp86.txt;' fi", "item": "if [ -f ~/memlog_hdp86.txt ]; then rm ~/memlog_hdp86.txt; fi", "msg": "[Errno 2] No such file or directory", "rc": 2}
我明白了:“物品”是正确的——这正是我需要的命令。 但在“cmd”属性中有额外的引号。我想,它打破了我的PB执行

为什么会这样?它是如何修复的

看。问题的结构如下: 1.我有命令。 2.通过命令模块执行它。 3.它会因任何错误而中断。
4.如何修复此错误。

您不应该将命令模块用于如此简单的任务。Ansible有更好的模块来实现这一点。若并没有更好的模块,那个么该命令只是一个后备命令

如果文件存在,您的命令应该删除它,对吗

只需使用:


请参阅:

我找到了一个解决方案。它不能解决同样的问题,但应该有助于避免它

使用bash脚本:将其放入bash文件中,然后运行它。
所以,我尝试了另一个命令:“对于/var/log/sa/sa[0-9]*;do sadf-d$file---r>~/memlog{{inventory\u hostname}}.txt中的文件,它设置了引号,并给出了相同的错误:“[Errno 2]没有这样的文件或目录”可能是重复的不,看起来不像。。。我有具体的问题,我需要具体的解决方案。如果我问如何做得更好?我需要解决当前的问题,这只是一个例子。我需要执行另外一个命令,它也有同样的问题。也许你应该用不同的方式问你的问题。如果你在问题中提供了这些信息,我们不知道你在寻找什么。我怎样才能“以不同的方式提问”?如果有具体的问题。你不能从两个方面理解它。这并不能回答这个问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-是的。它运行命令-这意味着它解决了一个问题。不是解决“如何修复命令字符串”,而是解决“如何使用ansible运行bash命令”
changed: [hdp86] => (item=touch ~/memlog_hdp86.txt)
failed: [hdp86] (item=if [ -f ~/memlog_hdp86.txt ]; then rm ~/memlog_hdp86.txt; fi) => {"changed": false, "cmd": "if '[' -f /home/karmatskiyrg/memlog_hdp86.txt '];' then rm '/home/karmatskiyrg/memlog_hdp86.txt;' fi", "item": "if [ -f ~/memlog_hdp86.txt ]; then rm ~/memlog_hdp86.txt; fi", "msg": "[Errno 2] No such file or directory", "rc": 2}
name: delete file if exists
file:
  path: "~/memlog_{{ inventory_hostname }}.txt"
  state: absent