Many times I find the ansible.builtin methods for dealing with files a bit obtuse. In particular, they require grouping by operation (template, file, directory, etc), and also there’s a lot of boilerplate involved if you need to set up a directory, template a config file in it, and remove a file, for example.
For several years I’ve been playing with this idea of a “filesystem mega module”, having implemented it both as modules and as roles I can import. I’ve been pretty happy with the ergonomics of this module, so I wanted to wrap it up in a bow and spank it on the bottom and send it out into the world.
The primary goal is to allow the grouping of operations logically, so it’s clear that you’re creating a directory, then putting a file in it, triggering a notify, removing a file, without having to search around other tasks in the file and making sure that they stay in order (easier because of their proximity).
See Ansible Galaxy for the docs and links to the github and the docs site.
Here’s an example:
Overview: Settings you specify after the “fsbuilder:” line are the defaults. The default state is “template”, and the default “src” for a template is “basename(dest) + ‘.j2’”.
- name: Deploy myapp - comprehensive example with loop
linsomniac.fsbuilder.fsbuilder:
owner: root
group: myapp
mode: a=rX,u+w
loop:
- dest: /etc/myapp/conf.d
state: directory
- dest: /etc/myapp/config.ini
validate: "myapp --check-config %s"
backup: true
notify: Restart myapp
- dest: /etc/myapp/version.txt
content: "version={{ app_version }}"
- dest: /etc/myapp/static.dat
state: copy
- dest: /etc/myapp/current
src: /opt/myapp/releases/v2.1
state: link
- dest: /etc/myapp/.last-deploy
state: touch
- dest: /etc/myapp/legacy.conf
state: absent
I use this extensively in our playbooks, I’ve got around 50 usages that I run on a daily basis (I respin half of our dev and staging environment every morning). So it’s at least a little battle-tested.
Thanks for any feedback you care to give.