Bootstrap FreeKB - Ansible - Role Files
Ansible - Role Files

Updated:   |  Ansible articles

Role Files are similar to Role Templates and the copy module. Each are used to copy a file from the control node (that's your Ansible server) to managed nodes (e.g. target system). The big difference between is that template files can contain jinja2 references, role files cannot.

Role Files are part of roles. Role Files are placed in the role files directory. For example, let's say you've the following files in the role files directory.



Since role files are part of a role, lets say you have a master playbook that invokes fooRole, like this.

- hosts: all
    - role: fooRole


And let's say /etc/ansible/roles/fooRole/tasks/main.yml contains the following. Running this play will copy foo.txt on the control node to /tmp/foo.txt on the managed node.

- name: copy foo.txt to /tmp/foo.txt
    src: foo.txt
    dest: /tmp/foo.txt


While not required, I try to always include the relative directory to the file, as this prevent issues when some other role includes fooRole.

- name: copy foo.txt to /tmp/foo.txt
    src: roles/fooRole/files/foo.txt
    dest: /tmp/foo.txt


Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 164715 in the box below so that we can be sure you are a human.