首页 > 解决方案 > 尝试删除文件时出现奇怪的行为

问题描述

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

- 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.html并且example-shibboleth2.xml被识别为不存在,而其他两个则不存在。

标签: ansible

解决方案


我终于意识到我做错了什么:该when: item is file行检查本地文件,而我想检查远程文件。以下变体使用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

推荐阅读