How to define a base_dir var so you can use {{ base_dir }}/playbooks etc?

Hi,

Ansible 2.1.1.0.

Situation: Tim and Bob both deploy servers. Tim checks out the central ansible git repo to /home/tim/ansible while Bob checks it out to /home/bob/ansible. No special ansible.cfg is used by either.

Problem: paths differ (/home/tim/ansible vs /home/bob/ansible) so you can not use static paths in the playbooks etc.

Question: how do you solve that? How do you specify something like a base_dir variable which is read early on so that all static paths can be replaced.

<example based on Google findings which I could not make work>

$ grep base_dir /home/tim/ansible/inventory/group_vars/all
base_dir: "{{ lookup('pipe', 'git rev-parse --show-toplevel') }}"

In Tim's case base_dir should be "/home/tim/ansible".

$ cat /home/tim/ansible/vars/global_vars.yml

roles: "{{ base_dir}}/roles"
handlersdir: "{{ base_dir }}/handlers"
tasksdir: "{{ base_dir }}/tasks"
playbooksdir: "{{ base_dir }}/playbooks"
includedir: "{{ base_dir }}/playbooks/include"
manualdir: "{{ base_dir }}/playbooks/manual"
varsdir: "{{ base_dir }}/vars"

$ cat /home/tim/ansible/playbooks/groups/dns

- name: setup dns servers
   hosts: dns
   user: root
   gather_facts: True

   vars_files:
  - "{{ base_dir }}/vars/global_vars.yml

   roles:
  - base
  - dns

TIA,
Patrick

Hi,

Ansible 2.1.1.0.

Situation: Tim and Bob both deploy servers. Tim checks out the central
ansible git repo to /home/tim/ansible while Bob checks it out to
/home/bob/ansible. No special ansible.cfg is used by either.

This is a very common way to do it.
You can also put the ansible.cfg in this folder so everybody shares the same ansible.cfg.

Problem: paths differ (/home/tim/ansible vs /home/bob/ansible) so you
can not use static paths in the playbooks etc.

This is not a problem you always use relative paths and not absolute(static) path.

Question: how do you solve that? How do you specify something like a
base_dir variable which is read early on so that all static paths can
be replaced.

Relative path and Ansible has a directory layout that it understand, I recommend reading this page that explain the concept.
https://docs.ansible.com/ansible/playbooks_best_practices.html

So when you run ansible or ansible-playbook Tim and Bob will always run them from within ~/ansible

<example based on Google findings which I could not make work>

$ grep base_dir /home/tim/ansible/inventory/group_vars/all
base_dir: "{{ lookup('pipe', 'git rev-parse --show-toplevel') }}"

In Tim's case base_dir should be "/home/tim/ansible".

$ cat /home/tim/ansible/vars/global_vars.yml

roles: "{{ base_dir}}/roles"
handlersdir: "{{ base_dir }}/handlers"
tasksdir: "{{ base_dir }}/tasks"
playbooksdir: "{{ base_dir }}/playbooks"
includedir: "{{ base_dir }}/playbooks/include"
manualdir: "{{ base_dir }}/playbooks/manual"
varsdir: "{{ base_dir }}/vars"

$ cat /home/tim/ansible/playbooks/groups/dns

- name: setup dns servers
  hosts: dns
  user: root
  gather_facts: True

  vars_files:
  - "{{ base_dir }}/vars/global_vars.yml

  roles:
  - base
  - dns

It looks like a very strange concept, I do recommend picking up a Ansible book to get basic knowledge of how Ansible works.

Hi Kai,

Thank you for your feedback.

Hi,

Ansible 2.1.1.0.

Situation: Tim and Bob both deploy servers. Tim checks out the central
ansible git repo to /home/tim/ansible while Bob checks it out to
/home/bob/ansible. No special ansible.cfg is used by either.

This is a very common way to do it.
You can also put the ansible.cfg in this folder so everybody shares the
same ansible.cfg.

Got it.

Problem: paths differ (/home/tim/ansible vs /home/bob/ansible) so you
can not use static paths in the playbooks etc.

This is not a problem you always use relative paths and not
absolute(static) path.

Great.

Question: how do you solve that? How do you specify something like a
base_dir variable which is read early on so that all static paths can
be replaced.

Relative path and Ansible has a directory layout that it understand, I
recommend reading this page that explain the concept.
https://docs.ansible.com/ansible/playbooks_best_practices.html

Will read that.

So when you run ansible or ansible-playbook Tim and Bob will always run
them from within ~/ansible

[snip]
>

It looks like a very strange concept, I do recommend picking up a
Ansible book to get basic knowledge of how Ansible works.

The concept is sorta used by the Fedora Infrastructure team. Looking at their setup I wondered about defining paths via vars hence my question to the list.

Thanks again for your help!

Best,
Patrick