Collection: bundling a txt file

Hi all,

I’ve a collection where I would like to externalize an array of strings. At run time, the collection will read their content and create an array based on the file. The idea being that this list can change without building again the collection.

I just wonder where, within my collection directory tree, to put this data file (it can be txt, csv, or even yml) and which internal API use to parse it.

Any tips?

Thanks!

Hi,

I've a collection where I would like to externalize an array of
strings. At run time, the collection will read their content and
create an array based on the file. The idea being that this list can
change without building again the collection.

if the file is included in the collection, how can you update the file
without building the collection?

I just wonder where, within my collection directory tree, to put this
data file (it can be txt, csv, or even yml) and which internal API
use to parse it.

There's no internal API. You can place it whereever you want in the
tree, there is no canonical place for it. I'd suggest to add some
module_utils code for reading it, it can use os.path.dirname(__file__)
to get hold of the module_utils directory path and load the file
relative from there.

I did something like that in one of my collections:
https://github.com/felixfontein/ansible-tools/blob/main/plugins/filter/domain_suffix.py#L10
https://github.com/felixfontein/ansible-tools/blob/main/plugins/public_suffix_list.dat

Cheers,
Felix

Thanks for the tip.

Actually, I over simplify for the sake of brevity. The goal is to be able to add data to this file without changing the python code, because we need to duplicate source code for the sake of making it work on Windows :frowning: So we try to reduce as much as possible the need to change the (duplicated) source file.