patternMinor
Playbook without relative path to vars
Viewed 0 times
withoutvarspathplaybookrelative
Problem
I had already bunch of playbooks, so I started to organize them in playbooks dir. It cause there must be relative paths to vars files.
Actual playbook:
Is there any way to keep the dir structure, but avoid the lochness family (
I know for roles You can specify path to them, but I did not find anything similar for vars_files
Project conditions:
Actual playbook:
---
- hosts: aws_instance.jenkins-agents
user: ec2-user
vars_files:
- ../../../vars/main.yaml
- ../../../vars/vault.yaml
...Is there any way to keep the dir structure, but avoid the lochness family (
../../../)?I know for roles You can specify path to them, but I did not find anything similar for vars_files
Project conditions:
- We need to run the playbooks from the root of the project
- Both vars and playbooks dir are in root of the project
- This project doesn't have static inventory/hosts, but it is generated on the fly when You run some playbook
Solution
You'll notice that the official recommendations for directory layout put all of the playbooks at the root level. This is intentional, as Ansible doesn't handle other schemes well.
You can put playbooks in a subdirectory, as you've started to do, but that will require (as you've already found) the use of relative paths from the playbooks to any other resources they use; Ansible uses the playbook path to start its search, that's just how it's written. For the most part, that's just what you get to deal with when you make the choice to move where playbooks live.
However, you seem to have more nesting than you should:
Why are you navigating up three directories? I'd expect a layout that's like the official one, but with one subfolder for playbooks; then you'd end up with just
which is a much more minor change.
Secondly, it's pretty rare that you actually want to use
Personally, I think it's best to have as little as possible in your playbooks, but delegate everything out to roles. Here's an example entire playbook in your setup:
This gives you far more flexibility for re-use.
And if you find yourself not wanting to use relative paths for roles, you can override
You can put playbooks in a subdirectory, as you've started to do, but that will require (as you've already found) the use of relative paths from the playbooks to any other resources they use; Ansible uses the playbook path to start its search, that's just how it's written. For the most part, that's just what you get to deal with when you make the choice to move where playbooks live.
However, you seem to have more nesting than you should:
---
- hosts: aws_instance.jenkins-agents
user: ec2-user
vars_files:
- ../../../vars/main.yaml
- ../../../vars/vault.yaml
...Why are you navigating up three directories? I'd expect a layout that's like the official one, but with one subfolder for playbooks; then you'd end up with just
---
- hosts: aws_instance.jenkins-agents
user: ec2-user
vars_files:
- ../vars/main.yaml
- ../vars/vault.yaml
...which is a much more minor change.
Secondly, it's pretty rare that you actually want to use
vars_files. Most variables used in Ansible either vary based on the host (and thus should go in group_vars/host_vars in the project root) or are role-specific (and thus should go in vars/defaults in the role directory). A few documentation links:- https://docs.ansible.com/ansible/intro_inventory.html#splitting-out-host-and-group-specific-data
- https://docs.ansible.com/ansible/playbooks_roles.html#roles
- https://docs.ansible.com/ansible/playbooks_roles.html#role-default-variables
Personally, I think it's best to have as little as possible in your playbooks, but delegate everything out to roles. Here's an example entire playbook in your setup:
- hosts: aws_instance.jenkins-agents
roles:
- ../roles/jenkins_agentThis gives you far more flexibility for re-use.
And if you find yourself not wanting to use relative paths for roles, you can override
roles_path in an ansible.cfg in the root directory where you run your Ansible commands from.Code Snippets
---
- hosts: aws_instance.jenkins-agents
user: ec2-user
vars_files:
- ../../../vars/main.yaml
- ../../../vars/vault.yaml
...---
- hosts: aws_instance.jenkins-agents
user: ec2-user
vars_files:
- ../vars/main.yaml
- ../vars/vault.yaml
...- hosts: aws_instance.jenkins-agents
roles:
- ../roles/jenkins_agentContext
StackExchange DevOps Q#1353, answer score: 7
Revisions (0)
No revisions yet.