Skip to content

Ansible Files from inventory

Purpose: The role should take files from inventory directories.

Inventory tree

├── test
   ├── group_vars
      └── all.yml
   ├── host_data
      └── srv
          ├── docker-compose-filebeat.yml
          ├── local_backup_shared.sh
          ├── ...
          ├── remote_rclone_backups.sh
          └── webhook_scripts
              └── immich-byone.sh
   ├── hosts.yml
   └── host_vars
       └── srv.yml

File inventory/test/group_vars/all.yml

inv_host_data: "{{ inventory_dir }}/host_data/{{ inventory_hostname }}"
...

File inventory/test/host_vars/srv.yml

file:
  - file: local_backup_shared.sh
    path: /root/scripts
    mode: "0700"

  - file: remote_rclone_backups.sh
    path: /root/scripts
    mode: "0700"

Task example: roles/file/tasks/tasks.yml

- name: Upload file or directory
  copy:
    backup: "{{ item.backup | default(false) }}"
    src: "{{ inv_host_data }}/{{ item.src | default(item.file) }}"
    dest: "{{ item.path }}/{{ '' if item.related is defined and (item.related|bool) else item.file }}"
    owner: "{{ item.owner | default('root') }}"
    group: "{{ item.group | default('root') }}"
    mode: "{{ item.mode | default('0644') }}"
    directory_mode: "{{ item.directory_mode | default('755') }}"
  loop: "{{ file | default([]) }}"
  loop_control:
    label: "{{item.file}} -> {{ item.path }}"