ubuntu - 如何通过ansible playbook检查线路是否存在
问题描述
我有文件名测试
cat test
[tag1]
我想在测试中的 [tag1] 之后添加行我找到了这个解决方案
- ini_file:
path: test
section: "{{ item.section }}"
option: "{{ item.option }}"
allow_no_value: yes
loop:
- section: 'tag1'
option: '# This is line 1'
- section: 'tag1'
option: '# This is line 2'
但是,如果该行存在,我什么也不想做,如果不存在,则添加它们。我应该怎么办?
解决方案
Cat 文件并注册输出。如果文件中不存在“字符串”,则执行任务。
- shell: cat test
register: check
changed_when: false
- ini_file:
path: test
section: "{{ item.section }}"
option: "{{ item.option }}"
allow_no_value: yes
when: '# This is line 1' not in check.stdout
loop:
- section: 'tag1'
option: '# This is line 1'
- section: 'tag1'
option: '# This is line 2'
推荐阅读
- javascript - 在动态地球旋转中,垂直和水平方向的线太多了
- css - 打印表格时,表格的对齐方式显示不正确,细节被隐藏
- android - Lottie 动画在应用程序布局中无法正常工作
- node.js - 无法绑定到“上传器”,因为它不是“输入”的已知属性
- android - 在 MediaControllerCompat.TransportControls 中正确设置重复/随机播放选项
- python - 蝗虫中的动态GET参数
- r - 有没有人成功使用 Matomo 通过闪亮仪表板应用程序进行闪亮使用跟踪?
- matlab - 如何在matlab中将图像扭曲到3D平面
- animation - 有没有办法在 UWP 中转储表达式动画值?
- selenium - 5
我无法从代码中获取动态文本“5”。当我使用 gettext() 得到 0 而不是 5
<span _ngcontent-c3="" xpath="1">5</span>
我试过 xpath =
"//div[4]//div[1]//div[2]//div[1]//p[3]//span[2]"