'Ansible manage deployable files in multi environment project
In our projet, which has a local, dev and prod environment, we want to deploy a file ourKeystore.p12. This file must be present in those 3 environments, with the same name, but with a different content.
Here is how we manage our multiple environments :
/
- environments/
- local/
- group_vars/
- vault.yml
- vars.yml
- hosts
- dev/
- group_vars/
- vault.yml
- vars.yml
- hosts
- prod/
- group_vars/
- vault.yml
- vars.yml
- hosts
To solve our problem, we found a way to deploy the correct ourKeystore.p12 in the correct environment. The logic is found in the role which deploys ourKeystore.p12 :
/
- roles/
- our_role/
- files/
- local/
- ourKeystore.p12
- dev/
- ourKeystore.p12
- prod/
- ourKeystore.p12
- tasks/
- main.yml
main.yml (simplified) :
- name: Copy keystore
copy:
src: "{{ current_environment }}/ourKeystore.p12"
dest: path/to/ourKeystore.p12
It works, but it bothers us that environment dependent files are found in roles/ instead of environments/.
What is the best approach to move ourKeystore.p12 from roles/ to environments/ ?
Ideally, we would like to have this, but it doesn't work :
/
- environments/
- local/
- files/
- ourKeystore.p12
- group_vars/
- vault.yml
- vars.yml
- hosts
- dev/
- files/
- ourKeystore.p12
- group_vars/
- vault.yml
- vars.yml
- hosts
- prod/
- files/
- ourKeystore.p12
- group_vars/
- vault.yml
- vars.yml
- hosts
We didn't find anythong relevant in ansible documentation, neither on stackoverflow.
Best regards, Vlad
Solution 1:[1]
Use the special variable inventory_dir to read the files in the roles. Put the declaration into the group_vars/all. For example,
shell> tree .
.
??? environments
? ??? dev
? ? ??? files
? ? ? ??? ourKeystore.p12
? ? ??? group_vars
? ? ? ??? all
? ? ??? hosts
? ??? prod
? ??? files
? ? ??? ourKeystore.p12
? ??? group_vars
? ? ??? all
? ??? hosts
??? our-playbook.yml
??? roles
??? our_role
??? tasks
??? main.yml
shell> cat environments/dev/hosts
srv.dev.net
shell> cat environments/dev/group_vars/all
ourKeystore_path: "{{ inventory_dir }}/files/ourKeystore.p12"
shell> cat environments/dev/files/ourKeystore.p12
dev content of ourKeystore.p12
shell> cat environments/prod/hosts
srv.prod.net
shell> cat environments/prod/group_vars/all
ourKeystore_path: "{{ inventory_dir }}/files/ourKeystore.p12"
shell> cat environments/prod/files/ourKeystore.p12
prod content of ourKeystore.p12
shell> cat roles/our_role/tasks/main.yml
- debug:
msg: "{{ lookup('file', ourKeystore_path) }}"
shell> cat our-playbook.yml
- hosts: all
gather_facts: false
roles:
- our_role
shell> ansible-playbook our-playbook.yml -i environments/dev/hosts
PLAY [all] ***********************************************************************************
TASK [our_role : debug] **********************************************************************
ok: [srv.dev.net] =>
msg: dev content of ourKeystore.p12
PLAY RECAP ***********************************************************************************
srv.dev.net: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
shell> ansible-playbook our-playbook.yml -i environments/prod/hosts
PLAY [all] ***********************************************************************************
TASK [our_role : debug] **********************************************************************
ok: [srv.prod.net] =>
msg: prod content of ourKeystore.p12
PLAY RECAP ***********************************************************************************
srv.prod.net: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
