With Ansible, I need to extract content from multiple files. With one file I used slurp and registered a variable.
- 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 }}"
But now I have multiple files so I need to extract content from every files, register them one by one to be able to process them later with operations like sed, merging_list etc...
How can I do this in ansible?
I tried to use slurp with with_fileglob directive but I couldn't register the files...
- 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
You should just use the loop
option, configured with the list of files to slurp
. Check this example:
---
- 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"] }}'
I am slurping
each file, and then iterating through the results
to get its content.
I hope it helps.