Skip to content

Ansible Files from inventory

Purpose: The role should take files from inventory directories.

File's tree

inventory
├── test
     ├── group_vars
          └── all.yml
     ├── hosts.yml
     ├── host_vars
          └── srv.yml
     └── srv
         └── files
             ├── local_backup_shared.sh
             ├── movies-check.sh
             ├── movies-src.txt
             └── remote_rclone_backups.sh

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: "{{ inventory_dir }}/{{inventory_hostname}}/files/{{ 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 }}"