Given the directory for testing
vault_dir: /tmp/ansible/vault/clients/
Create the files
shell> tree /tmp/ansible/vault/clients/
/tmp/ansible/vault/clients/
├── exchange
└── models
0 directories, 2 files
shell> cat /tmp/ansible/vault/clients/exchange
access_key: exchange
secret_key: exchange-password
shell> cat /tmp/ansible/vault/clients/models
access_key: models
secret_key: models-password
and encrypt them
shell> ansible-vault encrypt /tmp/ansible/vault/clients/exchange
Encryption successful
shell> ansible-vault encrypt /tmp/ansible/vault/clients/models
Encryption successful
shell> cat /tmp/ansible/vault/clients/exchange
$ANSIBLE_VAULT;1.1;AES256
35333063626236396534646161373832336231386561633331396465366439653364623433646433
3635393634386330393131653239346232656563623033390a343639333066633732306364613839
31663438643334386665383334386337353438613166633035653535656264323563383662303538
3037366337663465360a383733313964313936633834366234386163656531326161643833333933
34353763313863373864356634313163376138383130663461663565666165663236613538396537
36363032613662323861353136366362666465313661613061366530613330383763393532383938
623835633639356437663938323064343933
shell> cat /tmp/ansible/vault/clients/models
$ANSIBLE_VAULT;1.1;AES256
30323130633566353338313663343464313137643035353963646635616435353364633965633038
6266353366383735613336353238613261376164633166390a373966386330663366346563356263
35363931643230323232353130303434336139326433663434323731616635663434396238393438
6431363266373564360a666132363664663764306438353939623663333439643331396339373839
65393663393939616538623363633532386533393163373034356665643663613864616331643966
3363616531303038646234633163383363633733653136643562
Given the list client_names the below template
client_names: [exchange, models]
clients: |
{% filter from_yaml %}
{% for client in client_names %}
{{ client }}:
{{ lookup('file', vault_dir ~ client)|indent(2) }}
{% endfor %}
{% endfilter %}
gives
clients:
exchange:
access_key: exchange
secret_key: exchange-password
models:
access_key: models
secret_key: models-password
If you want to include all files the list of the clients is not necessary. The below template gives the same result
fileglob: "{{ vault_dir }}*"
clients: |
{% filter from_yaml %}
{% for path in q('fileglob', fileglob) %}
{{ path|basename }}:
{{ lookup('file', path)|indent(2) }}
{% endfor %}
{% endfilter %}
Example of a complete playbook for testing
- hosts: all
vars:
vault_dir: /tmp/ansible/vault/clients/
client_names: [exchange, models]
clients: |
{% filter from_yaml %}
{% for client in client_names %}
{{ client }}:
{{ lookup('file', vault_dir ~ client)|indent(2) }}
{% endfor %}
{% endfilter %}
fileglob: "{{ vault_dir }}*"
client2: |
{% filter from_yaml %}
{% for path in q('fileglob', fileglob) %}
{{ path|basename }}:
{{ lookup('file', path)|indent(2) }}
{% endfor %}
{% endfilter %}
tasks:
- debug:
var: clients
- debug:
var: client2