Playbook directory organisation

Hi,

This has probably been covered before but I can’t find any solutions.

My project pulls in roles from other projects via Maven. This means that we can have company wide roles reused.

This all works fine, all the roles are dropped into a subdirectory in roles and referenced in the playbooks.

I would like do something similar with playbooks. Whilst I can reference roles using …/roles/rolename inside a playbook in a subdirectory

None of the group_vars that are available to the top level playbooks are available to the playbook in the subdirectory

Has anybody solved this or is there a better way of organising playbooks.

Any help appreciated

Thanks

Hi James,

Have you set the ansible configuration file’s (ansible.cfg) roles_path default setting?
See: https://docs.ansible.com/intro_configuration.html#roles-path

If you are following the best practices documented (https://docs.ansible.com/intro_configuration.html#roles-path), it should be pulling in those group_vars variables, if the host(s) being applied to the plays/playbooks are within the defined groups that contain the variables.

Where is the group_vars folder relative to the playbooks that are not “seeing” the variables?

To further confirm, please post an example in pastebin.

Thanks,

Cruz

Hi,

Thanks for the info. I hadn’t looked at those configs, though we can only change the configuration on certain environments.

My layout is pretty straightforward

.
├── group_vars
│ └── all.yml

└── roles
│ ├── roleA
│ ├── roleB
│ └── shared_roles
│ ├── roleC
│ └── roleD

├── playbook1.yml <---- these pick up the group_vars
├── playbook2.yml
├── playbook3.yml
├── playbook_sub_dir
│ ├── playbook4.yml <---- these don’t pick up the group_vars
│ ├── playbook5.yml
│ └── playbook6.yml

I’ll try and knock together an example. I can’t include the actual project as there is a fair amount of sensitive information…

The issue is just with the playbooks in the sub directories. I can get them to hit the roles by referencing them with …/ its just the group_vars that aren’t getting picked up

Im just looking to organise the playbooks as im starting to get a lot in the root directory

Thanks

James

Group vars can sit next to your inventory or next to your playbook. Looks like your current groupvars are evaluated because they sit next to yout root-dir playbooks.

You can try to put them next to your inventory so that they’ll be evaluated for every pkaybook that’s run with that inventory.

Ok that makes sense but causes a problem for my layout. I have

.
├── group_vars
│ ├── all.yml
│ ├── groupA.yml
│ ├── groupB.yml
│ └── groupC.yml
├── inventory
│ └── ec2
│ ├── eu-west
│ │ ├── inventoryA
│ │ └── inventoryB
│ └── us-east
│ ├── inventoryA
│ └── inventoryB

There are many inventories. Currently I have all plays use the groups defined in the main group_vars. If I was to move some common groups_vars to the inventory then they would be duplicated for each inventory. Unless im missing something

I was hoping there would be a way of having ansible look at the playbook_dir instead of its current dir when evaluating the group_vars.

J

There was another thread about that yesterday. No good option that i know of. You can try to create a link to your var files inside each invetory, or write another inventory script that includes these files.

Ok,

I think i’d prefer more playbooks in the playbook_dir rather than resorting to symlinks. Less likely to confuse or go wrong.

Cheers for the info

J

Discussed here: https://groups.google.com/forum/#!topic/ansible-project/T9RWNQbLRWs

Interesting read though that seems to concern itself with the group_var / inventory layouts. I don't really have a problem there. Just the playbook sub directories not picking up the variables. If I ignore the idea of tidying playbooks by sub directory everything works well. Symlinks seem a bit dirty and error prone, though as mentioned they would be preserved by git which could work as long as they are relative.

I'll have a play. Thanks for all the advice

J

Have updated that thread with an inventory script that can pick up any
YAML vars files you'd like.

Interesting, thanks for the info will have a play with it.