Where to put files and templates that don't belong to a role

14 views
Skip to first unread message

Clayton Louden

unread,
Apr 11, 2018, 9:13:52 AM4/11/18
to Ansible Project
Hi everyone,

Where do you usually put files or templates that are just used in a certain play but are not part of a role? Currently I do have them just alongside my playbooks organised by play

someplay.yml
files
/
  someplay
/somefile
  someotherplay
/someotherfile
templates
  someplay
/sometemplate.j2
  someotherplay
/someothertemplate.j2

This however is a bit awkward when using it in a play since I always have to specify the play subfolder in files/ or templates/

    - name: Copy extensions.conf
      copy
: src=files/someplay/extensions.conf dest=/etc/extensions.conf

Now this can be easily solved by converting that particular playbook into a role and then just using 'templates/somethemplate.js' and 'files/somefile' without the 'play' subdirectory. That however has the side-effect that I'd have a rather stupid playbook that just includes a role, which I find rather annoying:

- name: Configure server
  hosts
: mysinglehost
  roles
:
   
- role: custom_configure_server_role

I've heard from other ansible guys that they treat 'everything as a role'. Maybe it's just me but I find that opening a playbook just to find out that it contains just one custom role a bit redundant. How do you guys usually do this?

Cheers!

Mike Garris

unread,
Apr 11, 2018, 9:25:36 AM4/11/18
to Ansible Project
I am in the boat of treating everything as a role. 

|____some_app
| |____site.yml
| |____roles
| | |____some_app_name
| | | |____vars
| | | |____tasks
| | | |____defaults
| | | |____files
| | | |____templates

Now you could, if needed, create a site.yml that would have variables to execute, or not execute, certain roles inside of the "roles" directory. 

site.yml

tasks:
- include_role:
    name: some_app_1
  when: some_app1_variable == "true"

- include_role:
    name: some_app_2
  when: some_app2_variable == "true"

This doesn't really get around your problem of having a generic location for files/templates. Just an opinion on the "everything as a role" comment. 

Happy Coding!!

Clayton Louden

unread,
Apr 11, 2018, 9:31:06 AM4/11/18
to ansible...@googlegroups.com
Hi Mike,

On 11.04.2018 15:25, Mike Garris wrote:
> I am in the boat of treating everything as a role. 
[...]
> Just an opinion on the "everything as a role" comment. 

An extra opinion is very welcome. If I'm the only person who doesn't treat "everything as a role" then maybe it's time to think about changing my ways ;)

Cheers!

Kai Stian Olstad

unread,
Apr 11, 2018, 1:48:04 PM4/11/18
to ansible...@googlegroups.com
On Wednesday, 11 April 2018 15.13.51 CEST Clayton Louden wrote:
> Hi everyone,
>
> Where do you usually put files or templates that are just used in a certain
> play but are not part of a role?

For the most part in a role.


> Currently I do have them just alongside my
> playbooks organised by play

Some file like ssh and gpg public keys is located on top level inside files/ directly.
The reason for this is that it make it easier for people since they don't need to go deep down into a role if they need to change their files or add files.


> This however is a bit awkward when using it in a play since I always have
> to specify the play subfolder in files/ or templates/
>
> - name: Copy extensions.conf
> copy: src=files/someplay/extensions.conf dest=/etc/extensions.conf
>
> Now this can be easily solved by converting that particular playbook into a
> role and then just using 'templates/somethemplate.js' and 'files/somefile'
> without the 'play' subdirectory.

When using template it automatically check the templates folder so you don't need to include templates/, somethemplate.js suffice.
The same are try for copy and the files/ directory in a role.


> That however has the side-effect that I'd
> have a rather stupid playbook that just includes a role, which I find
> rather annoying:
>
> - name: Configure server
> hosts: mysinglehost
> roles:
> - role: custom_configure_server_role

Personally i find this cleaner that having a task.
And if you have more than one host that need it, you only have one place to change the code if needed.

You can leave out role:

roles:
- custom_configure_server_role


> I've heard from other ansible guys that they treat 'everything as a role'.
> Maybe it's just me but I find that opening a playbook just to find out that
> it contains just one custom role a bit redundant. How do you guys usually
> do this?

If the config is very small I sometimes do someting like this since it fast and easy to grasp

- name: Runing check every minute
copy:
content: |
# Managed by Ansible
* * * * * root /usr/local/bin/check
dest: /etc/cron.d/hourly_check


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages