使用 Ansible,我需要从多个文件中提取内容。在一个文件中,我使用了 slurp 并注册了一个变量。
- name: extract nameserver from .conf file
slurp:
src: /opt/test/roles/bind_testing/templates/zones/example_file.it.j2
delegate_to: 127.0.0.1
register: file
- debug:
msg: "{{ file['content'] | b64decode }}"
Run Code Online (Sandbox Code Playgroud)
但现在我有多个文件,所以我需要从每个文件中提取内容,将它们一一注册,以便以后能够使用 sed、merging_list 等操作来处理它们......
我怎样才能在ansible中做到这一点?
我尝试将 slurp 与 with_fileglob 指令一起使用,但无法注册文件......
- name: extract nameserver from .conf file
slurp:
src: "{{ item }}"
with_fileglob:
- "/opt/test/roles/bind9_domain/templates/zones/*"
delegate_to: 127.0.0.1
register: file
Run Code Online (Sandbox Code Playgroud)
您应该只使用该loop选项,配置文件列表到slurp. 检查这个例子:
---
- hosts: local
connection: local
gather_facts: no
tasks:
- name: Find out what the remote machine's mounts are
slurp:
src: '{{ item }}'
register: files
loop:
- ./files/example.json
- ./files/op_content.json
- debug:
msg: "{{ item['content'] | b64decode }}"
loop: '{{ files["results"] }}'
Run Code Online (Sandbox Code Playgroud)
我是slurping每个文件,然后迭代results以获取其内容。
我希望它有帮助。
| 归档时间: |
|
| 查看次数: |
3832 次 |
| 最近记录: |