ansible - Touch log file with Ansible only when it does not start with __
问题描述
Using Ansible 2.7.13
, I'm trying to perform a touch of logfiles in a list only if the filename does start with __
. I cannot seem to get it running.
Here's my code:
# file touch.yml
- name: Touch
file:
path: "{{ item }}"
state: touch
mode: '0777'
when: not (item | basename | regex_search("^__"))
with_items:
- "{{ touch_files }}"
I call this with
touch_files:
- "{{ path }}/job_count.json"
- "{{ path }}/query_time.json"
- "{{ path }}/disk_usage.json"
- "{{ path }}/__revert__"
Which results in :
ERROR! 'when' is not a valid attribute for a Play[0m
The error appears to have been in '... /playbooks/touch.yaml': line 3, column 3, but may be elsewhere in the file depending on the exact syntax problem.
Expected result: files listed in the touch_files
list are touched. The __revert__ file is not touched.
What could be the problem here?
Many thanks in advance!
解决方案
Q: "Perform a touch of logfiles in a list only if the filename does start with __ "
A: There are problems in the code
when
shall be used outside theloop
touch_files
is a list; loop list of lists can't workwhen
condition is wrong
Try the task below
- name: Touch
file:
path: "{{ item }}"
state: touch
mode: '0777'
loop: "{{ touch_files }}"
when: "item|basename is regex('^__(.*)$')"
(not tested)
推荐阅读
- performance - 网页性能测试
- c - FFMPEG 用于从 yuv420p 转换为 rgb bmp 的所有操作是什么?
- javascript - SyntaxError:意外的令牌导入 - reactjs
- jmeter - 如何使 jmeter 报告仪表板显示超过三个百分位数?
- javascript - 如何将 url 参数与返回的数据链接?
- javascript - 使用 JavaScript 递归计算字符串中的元音
- php - 字段“caturl”没有默认值
- react-native - 使用 react-native-youtube api:youtube 视频不呈现
- android - 在 realm.close() 之后领域崩溃
- python - Ansible Tower - 如何获取环境变量列表