ansible - 如何在ansible中读取文件的特定部分
问题描述
我有一个包含以下数据的文件
!
乘以 4 和 5
乘以 5 和 6
!
加 3 到 4
加 8 到 4
!
从 6 分 3
从 5 分 9
!
!
格 6 乘 2
格 8 乘 1
我只想从文件中读取添加命令。
使用查找插件,我能够读取整个文件的数据。
但我不知道如何从文件中只读取特定的添加命令
这是我编写的代码。
---
- name: Extracting the Add commands from the File
hosts: 127.0.0.1
connection: local
vars:
contents: "{{lookup('file', 'file1.txt')}}"
tasks:
- name: Printing the Add commands of the File
debug:
var: contents
我被困在这一点上。任何人都可以帮助我解决如何在ansible中读取文件的特定部分。
解决方案
使用with_lines。下面的戏
- hosts: localhost
vars:
my_data_file: "{{ playbook_dir }}/data.txt"
my_commands: []
tasks:
- set_fact:
my_commands: "{{ my_commands + [ item ] }}"
with_lines: "cat {{ my_data_file }}"
when: item is search('^add')
- debug:
var: my_commands
给
"my_commands": [
"add 3 to 4",
"add 8 to 4"
]
推荐阅读
- c++ - C/C++ 中的指针实际上是如何存储地址的?
- linux - 轻松交换 terraform 版本的工具
- python - 如何在 Django-rest-Framework 中获取相关对象的完整列表
- logstash - 如何在 Filebeat 中收集标准输出生成的多个 python 程序日志
- android - 我可以使用 Flutter 项目 APK 更新 Google Play Store 上的应用吗?
- swift - 隐藏 TabBar 时,ScrollView 与 VStack 中的文本重叠
- swift - 快速灵活的变量始终为零
- reactjs - ReactJS 中的 net::ERR_CONNECTION_REFUSED
- javascript - Javascript 从 JSON 伪造数据数组并使用 ApexChart 将它们加载到图表中
- testing - 使用动态 url (params) Testcafe 创建 RequestMock