ansible - 如果文件有模式,如何在 Ansible 中跳过模板副本?
问题描述
如果目标文件中没有字符串,则尝试仅复制 Nginx 配置文件。
我认为这会起作用:
- name: Copy nginx config file
template:
src: templates/nginx.conf
dest: /etc/nginx/sites-enabled/default
validate: grep -l 'managed by Certbot' %s
但是,如果文件中没有“由 Certbot 管理”并停止 playbook 运行,则此任务将失败。
如果目标文件已经具有该模式,我该如何跳过模板副本?也许有更好的方法来获得相同的结果?
解决方案
灵感来自另一个答案
您可以使用检查模式下的lineinfile
模块检查文件中是否存在内容。然后,您可以将结果用作模板任务的条件。in条件是为了default
应对文件不存在且found
属性不在注册结果中的情况。
---
- name: Check for presence of "managed by Certbot" in file
lineinfile:
path: /etc/nginx/sites-enabled/default
regexp: ".*# managed by Certbot.*"
state: absent
check_mode: yes
changed_when: false
register: certbot_managed
- name: Copy nginx config file when not certbot managed
template:
src: templates/nginx.conf
dest: /etc/nginx/sites-enabled/default
when: certbot_managed.found | default(0) == 0
推荐阅读
- javascript - 如何按日期对蚂蚁表列进行排序?
- r - 使用 geom_tile 时,如何控制 ggplot2 中分类数据的 y 轴比例?
- r - R中的所有参数必须具有相同的长度e1071 svm
- c - 是否可以一次性在线确定对象的类型,包括宏?
- javascript - Bootstrap 按钮插件与 Vue 复选框单击事件冲突
- flutter - 如何让用户以最安全的方式保持登录状态?
- android - 如何在 kotlin 的 Android 中的 Dialog Fragment 中覆盖 onCancel?
- android - 如何从android本机活动中绘制像素数组
- typescript - TypeScript React Hooks,自定义钩子类型推断问题
- python - 在 SQL [Python/SQLite] 中高效搜索子字符串