Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "slurp" multiple files with Ansible

Tags:

yaml

ansible

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

like image 306
Luca Avatar asked Sep 02 '25 15:09

Luca


1 Answers

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.

like image 58
guzmonne Avatar answered Sep 05 '25 11:09

guzmonne