尝试删除文件时出现奇怪的异常行为

时间:2019-05-13 11:42:38

标签: ansible

我在剧本中具有以下步骤,以删除一些不需要的默认配置文件,同时保留备份以防万一:

- name: remove some shibboleth config files if present
  shell: "mv {{ item }} {{ item }}.orig"
  loop:
    - /etc/shibboleth/attrChecker.html
    - /etc/shibboleth/protocols.xml
    - /etc/shibboleth/security-policy.xml
    - /etc/shibboleth/example-shibboleth2.xml
  when: item is file

输出如下:

TASK [backend : remove some shibboleth config files if present]
****************************************************************************************************************
skipping: [192.168.60.28] => (item=/etc/shibboleth/attrChecker.html)
failed: [192.168.60.28] (item=/etc/shibboleth/protocols.xml) => {"changed": true, "cmd": "mv /etc/shibboleth/protocols.xml /etc/shibboleth/protocols.xml.orig", "delta": "0:00:00.006012", "end": "2019-05-13 13:28:51.886074", "item": "/etc/shibboleth/protocols.xml", "msg": "non-zero return code", "rc": 1, "start": "2019-05-13 13:28:51.880062", "stderr": "mv: stat '/etc/shibboleth/protocols.xml' sikertelen: Nincs ilyen fájl vagy könyvtár", "stderr_lines": ["mv: stat '/etc/shibboleth/protocols.xml' sikertelen: Nincs ilyen fájl vagy könyvtár"], "stdout": "", "stdout_lines": []}
failed: [192.168.60.28] (item=/etc/shibboleth/security-policy.xml) => {"changed": true, "cmd": "mv /etc/shibboleth/security-policy.xml /etc/shibboleth/security-policy.xml.orig", "delta": "0:00:00.005486", "end": "2019-05-13 13:28:52.205756", "item": "/etc/shibboleth/security-policy.xml", "msg": "non-zero return code", "rc": 1, "start": "2019-05-13 13:28:52.200270", "stderr": "mv: stat '/etc/shibboleth/security-policy.xml' sikertelen: Nincs ilyen fájl vagy könyvtár", "stderr_lines": ["mv: stat '/etc/shibboleth/security-policy.xml' sikertelen: Nincs ilyen fájl vagy könyvtár"], "stdout": "", "stdout_lines": []}
skipping: [192.168.60.28] => (item=/etc/shibboleth/example-shibboleth2.xml)     to retry, use: --limit @/home/pallinger/mtmt2/install/ansible/all.retry

因此,似乎意识到其中两个文件不存在,但是却没有意识到另外两个文件也不存在!实际上,它们都不存在:

root@192.168.60.28:~# ls -l /etc/shibboleth/attrChecker.html /etc/shibboleth/protocols.xml /etc/shibboleth/security-policy.xml /etc/shibboleth/example-shibboleth2.xml
ls: cannot access '/etc/shibboleth/attrChecker.html': No such file or directory 
ls: cannot access '/etc/shibboleth/protocols.xml': No such file or directory
ls: cannot access '/etc/shibboleth/security-policy.xml': No such file or directory
ls: cannot access '/etc/shibboleth/example-shibboleth2.xml': No such file or directory

有人知道此行为的可能解决方法吗?

我尝试将所有循环值都放在引号中并更改循环中的项目顺序,但结果始终相同:attrChecker.htmlexample-shibboleth2.xml被认为是不存在的,而其他两个是不是。

1 个答案:

答案 0 :(得分:1)

我终于意识到我做错了什么:when: item is file行检查 local 文件,而我想检查 remote 文件。以下变体使用命令模块的removes指令解决了该问题:

- name: remove some shibboleth config files if present
  command: removes="{{ item }}" "mv {{ item }} {{ item }}.orig"
  loop:
    - /etc/shibboleth/attrChecker.html
    - /etc/shibboleth/example-shibboleth2.xml
    - /etc/shibboleth/protocols.xml
    - /etc/shibboleth/security-policy.xml