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.html
并且example-shibboleth2.xml
被识别为不存在,而其他两个则不存在。
解决方案
我终于意识到我做错了什么:该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
推荐阅读
- deno - 检查脚本中启用/禁用了哪些 deno“允许”标志
- dart - 可选参数的默认值必须是常量 - 颜色
- c# - 如何使用 SqlKata 从存储过程中查询?
- python - Python - 将字符串列表转换为浮点数 - 方括号和小数点导致问题
- bash - 管道 awk printf - 在(awk)格式修饰符上查找替换
- arrays - 比较两个对象列表 A 和 B 并存储在 dart 中的另一个列表 C 中
- parsing - 将一种 DSL 转换为另一种
- python-3.x - AttributeError:“LevelingSystem”对象没有属性“作者”
- sql - 从 select 语句中的列计算
- performance - 有什么方法可以估计应用程序的吞吐量(每秒处理的位数)?