Hello Ansible user ! It is very nice to see you. :3 Today I’m going to cover something that would, in theory, be super simple but I struggled to quickly find documentation that accurately showed me what I wanted. And you know that’s what I like, quick documentation. I resorted to asking an LLM (gasp!)
I know, I’m sorry, but no longer shall I use this excuse for this specific use case, here is how you do it.
What the YAML looks like
Say you have a directory structure that looks like this:
repo folder/
├── file store
│ ├── file1.md
│ ├── file2.txt
│ └── file3.md
└── playbook.yml
In your playbook, you can iterate through each file in file store using the with_fileglob key at the root of a task. You can pair this with this funky lookup function: "{{ lookup('ansible.builtin.file', item) }}"
---
- name: Glob through files and display their contents
hosts: all
become: true
tasks:
- name: Debug message contents of files.
ansible.builtin.debug:
msg: "{{ lookup('ansible.builtin.file', item) }}"
with_fileglob:
- "file store/"
If for any reason you’re allergic to any file that isn’t markdown (I understand and do not pass judgement, don’t worry. ;3), you can always use wildcards in the with_fileglob key like so:
# ...cut
with_fileglob:
- "file store/*.md"
As you’ve probably guessed, the with_fileglob key can take multiple inputs, and so you can chain these however you like.
Why does it work ?
Hey ! You’ve arrived at this section of the documentation and for that I’m proud of your curiosity ! <3 The reason this works is twofold:
1. We’re using a loop
Indeed, Ansible has support for loops ! This is probably not a surprise for you, especially if you’re doing something like this. Loops exposes the item variable, which represents the item that is currently being iterated over. A simple example of this working is as follows:
# ...cut
tasks:
- name: Name my favorite cartoon canines
ansible.builtin.debug:
msg: "{{ item }}"
loop:
- "Brandy (Bluey)"
- "Diane (The Bad Guys)"
- "Nomi (Dogs in Space)"
Looking through Ansible’s loops documentation reveals multiple types of loops. In our case, the with_fileglob key creates a loop that… well loops over a glob, returning a list of file paths.
2. We’re pairing it with lookup()
Ansible has these functions that extends the Jinja2 templating language called Lookup Plugins, they enable users to… look up data in different places like files, APIs, and more. Here’s another super simple example of how this works.
# ...cut
tasks:
- name: Name my favorite cartoon canines
ansible.builtin.debug:
msg: "{{ lookup('ansible.builtin.file', 'path/to/file.ext') }}"
Here, we’re doing a simple lookup to access the contents of a file, and substituting the Jinja template statement with it. It really is that simple.
Conclusion ?
Not sure if this needs a conclusion. I’m sure you can put two and two together to create simple yet powerful Ansible playbooks and roles. Remember to drink water, and tell your loved ones that you, in fact, love them.
That’s it for me today, I wish you the best, darling. Good luck out there. :3
Comments
No comments yet. Be the first to react!